import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal, constants
from scipy.optimize import curve_fit
from glob import glob
import pickle
from uncertainties import ufloat, unumpy

class spulen:
    def __init__(self):
        self.spulen = {
            "sweep": {"radius": 16.39e-2, "N": 11, "R": 1.0},
            "horizontal": {"radius": 15.79e-2, "N": 154, "R": 0.5},
            "vertical": {"radius": 11.735e-2, "N": 20, "R": 0.5},
        }

    def calc_b_helmoltz(self, spule, U):
        return (
            constants.mu_0
            * 8
            * (U / self.spulen[spule]["R"])
            * self.spulen[spule]["N"]
            / (np.sqrt(125) * self.spulen[spule]["radius"])
        )

    def calc_b_gesamt(self, U_sweep, U_horiz):
        B_Sweep = self.calc_b_helmoltz("sweep", U_sweep)
        B_Horiz = self.calc_b_helmoltz("horizontal", U_horiz)
        return B_Sweep + B_Horiz

class static_experiment:
    def __init__(self):
        self.nu, self.p1_s, self.p1_h, self.p2_s, self.p2_h = np.genfromtxt(
            "data/task_c.txt", unpack=True
        )
        self.nu *= 1e3
        self.spu = spulen()

    def calc_earth_b_static(self):
        s = self.spu.calc_b_helmoltz("sweep", 0.32)
        v = self.spu.calc_b_helmoltz("vertical", 0.12)
        print("Magnetfeld der Erde")
        print("Verticale komponente : ", v, "T")
        print("Horizontale komponente : ", s, "T")
        return s, v

    def frequenz_B_fit(self):
        B1 = self.spu.calc_b_gesamt(self.p1_s, self.p1_h)
        B2 = self.spu.calc_b_gesamt(self.p2_s, self.p2_h)

        df = pd.DataFrame(
            {
                r"$\nu$ / \si{\kilo\hertz}": self.nu / 1e3,
                r"$B_{85}$ / \si{\micro\tesla}": np.round(B1 * 1e6, 2),
                r"$B_{87}$ / \si{\micro\tesla}": np.round(B2 * 1e6, 2),
            }
        )
        with open("build/b_nu.tex", "w") as tf:
            tf.write(
                df.to_latex(
                    column_format="S[zero-decimal-to-integer=true,table-format=4.0]SS",
                    escape=False,
                    index=False,
                    sparsify=True,
                )
            )
        self.params = []
        self.std = []

        def f(x, m, b):
            return m * x + b

        for B, nu in zip([B1, B2], [self.nu, self.nu]):
            popt, pcov = curve_fit(f, nu, B)
            cov = np.sqrt(np.diag(pcov))
            self.std.append(cov)
            self.params.append(popt)
        df = pd.DataFrame(
            {
                r"a / Ts": [self.params[0][0], self.params[1][0]],
                r"$\Delta$ a / Ts": [self.std[0][0], self.std[1][0]],
                r"b / T": [self.params[0][1], self.params[1][1]],
                r"$\Delta$ b / T": [self.std[0][1], self.std[1][1]],
            }
        )
        with open("build/lin_params.tex", "w") as tf:
            tf.write(
                df.to_latex(
                    column_format="SSS[scientific-notation =true]S",
                    escape=False,
                    index=False,
                    sparsify=True,
                )
            )
        return nu, [B1, B2], self.params, self.std

    def horizontal_E_Feld(self):
        self.frequenz_B_fit()
        B_Erde = []
        for x in range(2):
            B_Erde.append(ufloat(self.params[x][1], self.std[x][1]))
        print("Das Magnetfeld der Erde beträgt: ", np.mean(B_Erde), " T")
        return np.mean(B_Erde)

    def lande_Faktoren(self):
        self.frequenz_B_fit()
        self.lande = []
        for x in range(2):
            quot = ufloat(self.params[x][0], self.std[x][0])
            self.lande.append(4 * np.pi * constants.m_e / (constants.e * quot))
        print("Die Landefaktoren betragen:")
        print("Rb^87: ", self.lande[0])
        print("Rb^85: ", self.lande[1])
        df = pd.DataFrame(
            {
                "": [r"Rb$_{87}$", r"Rb$_{85}$"],
                r"$g_\text{theo}$": [r"$\sfrac{1}{2}$", r"$\sfrac{1}{3}$"],
                r"$g_\text{exp}$": [
                    self.lande[0].nominal_value,
                    self.lande[1].nominal_value,
                ],
                r"$\Delta g_\text{exp}$": [
                    self.lande[0].std_dev,
                    self.lande[1].std_dev,
                ],
            }
        )
        with open("build/lande.tex", "w") as tf:
            tf.write(
                df.to_latex(
                    column_format="SS[table-format=2.0]S[round-mode=places,round-precision=3,table-format=0.3]S[round-mode=places,round-precision=3,table-format=0.3]",
                    escape=False,
                    index=False,
                    sparsify=True,
                )
            )

        return self.lande[0], self.lande[1]

    def kernspins(self):
        self.J = 1 / 2
        self.g_j = 2.0023

        def kernspin(lande):
            return self.J * (self.g_j / lande - 1)

        lande_87, lande_85 = self.lande_Faktoren()
        I_85 = kernspin(lande_85)
        I_87 = kernspin(lande_87)
        print("Der Kernspin ist: ", I_85, " und ", I_87)
        df = pd.DataFrame(
            {
                "": [r"Rb$_{87}$", r"Rb$_{85}$"],
                r"$I_\text{theo}$": [r"$\sfrac{3}{2}$", r"$\sfrac{5}{2}$"],
                r"$I_\text{exp}$": [I_85.nominal_value, I_87.nominal_value],
                r"$\Delta I_\text{exp}$": [I_85.std_dev, I_87.std_dev],
            }
        )
        with open("build/kernspin.tex", "w") as tf:
            tf.write(
                df.to_latex(
                    column_format="SS[table-format=2.0]S[round-mode=places,round-precision=3,table-format=0.3]S[round-mode=places,round-precision=3,table-format=0.3]",
                    escape=False,
                    index=False,
                    sparsify=True,
                )
            )
        return I_85, I_87

    def isotopen_ratio(self):
        self.amplitude_85 = 13
        self.amplitude_87 = 7
        self.ratio = np.round(self.amplitude_85 / self.amplitude_87, 2)
        print("Das Verhätniss 85/87 ist: ", self.ratio)
        return self.ratio

    def squared_zee(self):
        delta_E = 2e-24
        B = 1e-3
        M_F = 1
        lin_zee = self.lande[0] * constants.physical_constants["Bohr magneton"][0] * B
        squared_zee = lin_zee ** 2 * (1 - 2 * M_F) / delta_E
        print("Zeemann linear: ", lin_zee)
        print("Zeemann squared: ", squared_zee)
        print("Zeemann quotient: ", 100 * squared_zee / lin_zee)
        return lin_zee, squared_zee

class larmor_sweep:
    def __init__(self, csvPath, spulenU, upperTLim=2.5e-3, lowerTLim=-1e-5):
        df = pd.read_csv(csvPath)
        data = pd.DataFrame(df.loc[18:].values[:, 3:5], columns=["time", "voltage"])
        # self.timescale = float(df.loc[10].values[1])
        # data.time = data.time.values / self.timescale
        self.mask = (data.time > lowerTLim) & (data.time < upperTLim)
        self.data = data[self.mask].reset_index()
        self.lowerTLim = lowerTLim
        self.upperTLim = upperTLim
        self.spulenU = spulenU

    def find_peaks(self, distance=None):
        if distance == None:
            distance = sum(self.mask) / 8
        self.uMean = np.mean(self.data["voltage"])
        peaks, _ = signal.find_peaks(
            self.data["voltage"], height=self.uMean, distance=sum(self.mask) / 20
        )
        self.tPeaks = self.data["time"][peaks]
        self.uPeaks = self.data["voltage"][peaks]
        self.tDist = self.tPeaks.values[1:] - self.tPeaks.values[:-1]
        self.tDist = ufloat(np.mean(self.tDist), np.std(self.tDist))
        return self.tDist.nominal_value, self.tDist.std_dev

    def save_info(self, path, find_peaks_dict={}):
        # self.find_peaks(*find_peaks_dict)
        df = {
            "lowerTLim": self.lowerTLim,
            "upperTLim": self.upperTLim,
            "uMean": self.uMean,
            "tPeaks": self.tPeaks,
            "uPeaks": self.uPeaks,
            "tDist": self.tDist,
            "time": self.data.time.values,
            "voltage": self.data.voltage,
            "spulenU": self.spulenU,
        }
        pickle.dump(df, open(path + str(int(self.spulenU)) + ".pkl", "wb"))

class plotter:
    def __init__(self):
        self.df = None
        pass

    def load_data(self, path):
        self.df = pickle.load(open(path, "rb"))

    def plot_nu_b(self, nu, b, params, figPath="build/static_B.pdf"):
        def f(x, m, b):
            return m * x + b

        fig = plt.figure()
        ax = fig.add_subplot(111)
        x_sample = np.linspace(0, max(nu), 3)
        label = [r"Rb$^{87}$", r"Rb$^{85}$"]
        for x in range(2):
            ax.plot(nu/1e3, b[x]*1e6, 'x')
            ax.plot(x_sample/1e3, f(x_sample, *params[x])*1e6, label=label[x])
        ax.set_xlim(xmin=0)
        # ax.set_ylim(0.05*min(b.flatten()), 1.05*max(b.flatten()))
        ax.set_xlabel(r'$\nu$ / kHz')
        ax.set_ylabel('B / $\si{\micro \\tesla}$')
        ax.legend(loc='best')
        fig.savefig(figPath)
        plt.close()

    def plot_peaks(self, figPath):
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.plot(self.df["time"], self.df["voltage"])
        ax.axhline(self.df["uMean"], linestyle="--", color="r")
        ax.scatter(self.df["tPeaks"], self.df["uPeaks"], label="detected peak")
        ax.set_xlim(self.df["lowerTLim"], self.df["upperTLim"])
        ax.set_xlabel("Zeit / s")
        ax.set_ylabel("Spannung / U")
        ax.set_title("T = " + str(self.df["tDist"] * 1e3) + " ms")
        plt.legend(loc="best")
        fig.savefig(figPath)
        plt.close()

    def f_hyp(self, U, a, b, c):
        return a + b / (U + c)

    def fit_hyperbel(self, U, T):
        print("U: ", U)
        print("T: ", T)
        popt, pcov = curve_fit(self.f_hyp, U, T)
        return popt, np.sqrt(np.diag(pcov))

    def plot_U_T(self, U, T, figPath):
        mT = np.array(T) * 1000
        popt, cov = self.fit_hyperbel(U, mT)
        fig = plt.figure(figsize=(3,2))
        ax = fig.add_subplot(111)
        ax.plot(U, mT, "x")
        ax.plot(np.linspace(1, 10, 50), self.f_hyp(np.linspace(1, 10, 50), *popt))
        ax.set_xlabel("U / V")
        ax.set_ylabel("T / ms")
        plt.tight_layout()
        fig.savefig(figPath)
        plt.close()
        return popt, cov

def statisch():
    stc = static_experiment()
    stc.calc_earth_b_static()
    nu, B, params, stds = stc.frequenz_B_fit()
    stc.horizontal_E_Feld()
    # stc.lande_Faktoren()
    stc.kernspins()
    stc.isotopen_ratio()
    stc.squared_zee()

    plttr = plotter()
    plttr.plot_nu_b(nu, B, params)

def sweep(isotope, TLim, selectedU):
    plttr = plotter()
    csvPath = "data/" + isotope + "/"
    T = []
    Std_t = []
    for U in selectedU:
        uStr = str(int(U))

        print("Processing U=: ", U)
        lam = larmor_sweep(csvPath + uStr + "V.CSV", U, upperTLim=TLim[U - 1])
        t, std_t = lam.find_peaks()
        T.append(t)
        Std_t.append(std_t)
        lam.save_info(csvPath)

        print("Plotting U=: ", U)
        plttr.load_data(csvPath + uStr + ".pkl")
        plttr.plot_peaks("build/" + isotope + "_" + uStr + ".png")
    print("T: ", T)
    print("Std_t: ", Std_t)
    popt, cov = plttr.plot_U_T(selectedU, T, "build/" + isotope + ".pdf")
    params = unumpy.uarray(popt, cov)
    print("Params", params)
    print("Done")

    return selectedU, T, params

def main():
    statisch()
    firstTLim = np.array([10.0, 10.0, 1.5, 2.5, 2.5, 1.5, 1.5, 2.5, 2.5, 1.5]) * 1e-3
    secondTLim = np.array([5, 2, 2.1, 5, 3, 3, 3, 3, 3, 3]) * 1e-3
    selectedU = [1, 2, 4, 5, 6, 7, 8, 9, 10]
    U1, T1, params1 = sweep("firstPeak", firstTLim, selectedU)
    U2, T2, params2 = sweep("secondPeak", secondTLim, range(3, 11))

    df = pd.DataFrame({"U / V": U1, "T / s": T1})
    with open("build/T1.tex", "w") as tf:
        tf.write(
            df.to_latex(
                column_format="S[zero-decimal-to-integer=true,table-format=2.0]S[round-mode=figures,round-precision=3,table-format=1.2e2,scientific-notation=true]",
                escape=False,
                index=False,
                sparsify=True,
            )
        )

    df = pd.DataFrame({"U / V": U2, "T / s": T2})
    with open("build/T2.tex", "w") as tf:
        tf.write(
            df.to_latex(
                column_format="S[zero-decimal-to-integer=true,table-format=2.0]S[round-mode=figures,round-precision=3,table-format=1.2e2,scientific-notation=true]",
                escape=False,
                index=False,
                sparsify=True,
            )
        )

    print("b1/b2: ", params2[1] / params1[1])

if __name__ == "__main__":
    main()


# In this exercise, we will create a window and draw a circle in it.
# But this time, we will define a function so we can easily draw any circle at any point with any side size.
# Then we will handle input (in terms of key presses) to redraw the circle left/right/up/down respectively.

# Let's start with importing the 'tkinter' library again.
from tkinter import *

TITLE = 'Handling keyboard input to draw circles'
WIDTH = 800
HEIGHT = 600

# Create the Window.
win = Tk()
win.title(TITLE)
win.geometry(str(WIDTH) + "x" + str(HEIGHT))

# Use 'x' and 'y' to be the circle center. Initialize them to be at the center of the window.
x = WIDTH / 2
y = HEIGHT / 2
# Circle radius and color.
radius = 100

# Create and pack the canvas.
canvas = Canvas(win, width=WIDTH, height=HEIGHT)
canvas.pack()

# Draw the circle at (x,y).
canvas.create_oval(x-radius, y-radius, x+radius, y+radius)  # draw the circle
print('Use the arrow keys to draw the circle elsewhere in the window!')

# In the following, we define functions to handle the events of pressing the arrow keys on the keyboard.
# The 'left' function handles the left arrow key, etc.
# Each of these functions takes an argument 'event' which is required but not used.
def left(event):
    global x  # The 'global keyword here means: Do not define a new value. Reuse the existing one from the outer scope.
    x = x - 10  # Move the circle 10 pixels to the left.
    canvas.create_oval(x-radius, y-radius, x+radius, y+radius)  # draw the circle

def right(event):
    global x
    x = x + 10  # Move the circle 10 pixels to the right.
    canvas.create_oval(x-radius, y-radius, x+radius, y+radius)  # draw the circle

def up(event):
    global y
    y = y - 10  # Move the circle 10 pixels up.
    canvas.create_oval(x-radius, y-radius, x+radius, y+radius)  # draw the circle

def down(event):
    global y
    y = y + 10  # Move the circle 10 pixels down.
    canvas.create_oval(x-radius, y-radius, x+radius, y+radius)  # draw the circle

# Now, bind the corresponding events (i.e. arrow key presses) to a named function.
# The functions must be defined 'before' they can be mentioned. That is why we define the functions first, then bind
# them to the corresponding key-press events.
win.bind('<Left>', left)  # Note how the 'left' arrow key is encoded as '<Left>'. Similarly for the other arrow keys.
win.bind('<Right>', right) # The second argument of the 'bind' is the function to be called
win.bind('<Up>', up)
win.bind('<Down>', down)

win.mainloop()  # Always, the last command must be the 'mainloop()' to run the GUI.


import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import xml
import os
import sys
import PIL

class Box:

    def __init__(self, xmin, xmax, ymin, ymax):
        self.xmin = xmin
        self.xmax = xmax
        self.ymin = ymin
        self.ymax = ymax

    @property
    def width(self):
        return max(self.xmax - self.xmin, 0)

    @property
    def height(self):
        return max(self.ymax - self.ymin, 0)

    @property
    def xcentre(self):
        return 0.5 * (self.xmin + self.xmax)

    @property
    def ycentre(self):
        return 0.5 * (self.ymin + self.ymax)

    @property
    def area(self):
        return self.width * self.height

# Image size is (160, 160). We superimpose a 5x5 grid of cells, where each cell has size (32, 32).
# On each grid cell we place anchors of sizes (32, 32), (64, 64), (96, 96) and (128, 128).

def make_anchor_boxes():
    anchor_boxes = []
    for y_id in range(5):  # first dimension in image array is y, not x
        for x_id in range(5):
            for anchor_size in [32, 64, 96, 128]:
                xmin = (x_id + 0.5) * 32 - 0.5 * anchor_size
                xmax = (x_id + 0.5) * 32 + 0.5 * anchor_size
                ymin = (y_id + 0.5) * 32 - 0.5 * anchor_size
                ymax = (y_id + 0.5) * 32 + 0.5 * anchor_size
                anchor_boxes.append(Box(xmin, xmax, ymin, ymax))
    return anchor_boxes

def calc_iou(box1, box2):
    intersection_box = Box(xmin=max(box1.xmin, box2.xmin), xmax=min(box1.xmax, box2.xmax),
                           ymin=max(box1.ymin, box2.ymin), ymax=min(box1.ymax, box2.ymax))
    intersection_area = intersection_box.area
    union_area = box1.area + box2.area - intersection_area
    return intersection_area / union_area

def calc_xywh(real_box, anchor_box):
    xcentre_offset = (real_box.xcentre - anchor_box.xcentre) / 32
    ycentre_offset = (real_box.ycentre - anchor_box.ycentre) / 32
    log_width_ratio = np.log(real_box.width / anchor_box.width)
    log_height_ratio = np.log(real_box.height / anchor_box.height)
    return (xcentre_offset, ycentre_offset, log_width_ratio, log_height_ratio)

def perturb_with_xywh(anchor_box, xcentre_offset, ycentre_offset, log_width_ratio, log_height_ratio):
    new_xcentre = anchor_box.xcentre + xcentre_offset * 32
    new_ycentre = anchor_box.ycentre + ycentre_offset * 32
    new_width = anchor_box.width * np.exp(log_width_ratio)
    new_height = anchor_box.height * np.exp(log_height_ratio)

    new_xmin = new_xcentre - 0.5 * new_width
    new_xmax = new_xcentre + 0.5 * new_width
    new_ymin = new_ycentre - 0.5 * new_height
    new_ymax = new_ycentre + 0.5 * new_height
    return Box(new_xmin, new_xmax, new_ymin, new_ymax)

def compute_average_of_boxes(boxes, weights):
    ave_xmin = np.clip(np.average([box.xmin for box in boxes], weights=weights), 0, 160)
    ave_xmax = np.clip(np.average([box.xmax for box in boxes], weights=weights), 0, 160)
    ave_ymin = np.clip(np.average([box.ymin for box in boxes], weights=weights), 0, 160)
    ave_ymax = np.clip(np.average([box.ymax for box in boxes], weights=weights), 0, 160)
    return Box(ave_xmin, ave_xmax, ave_ymin, ave_ymax)

def get_training_targets(real_box, anchor_boxes):
    iou_values = []  # IoU's of *anchor* with real box
    xywh_values = [] # The centre and width/height offsets of real box relative to anchor

    for anchor_box in anchor_boxes:
        iou_values.append(calc_iou(real_box, anchor_box))
        xywh_values.append(calc_xywh(real_box, anchor_box))

    iou_values = np.resize(np.array(iou_values), (5, 5, 4, 1))
    xywh_values = np.resize(np.array(xywh_values), (5, 5, 4, 4))
    return iou_values, xywh_values

def get_predicted_box(iou_pred, xywh_pred, anchor_boxes):
    iou_pred = np.resize(np.array(iou_pred), new_shape=(100,))
    weights = np.square(iou_pred)   # weight predicted boxes by (predicted IoU)^2.

    xywh_pred = np.resize(np.array(xywh_pred), new_shape=(100, 4))
    boxes = []
    for box_id in range(100):
        xcentre_offset, ycentre_offset, log_width_ratio, log_height_ratio = xywh_pred[box_id]
        box = perturb_with_xywh(anchor_boxes[box_id], xcentre_offset, ycentre_offset,
                                log_width_ratio, log_height_ratio)
        boxes.append(box)

    return compute_average_of_boxes(boxes, weights)

def load_image(filestem, images_path, annotations_path):
    raw_image = np.array(PIL.Image.open('{}/{}.jpg'.format(images_path, filestem)))
    orig_height, orig_width = raw_image.shape[0], raw_image.shape[1]

    if len(raw_image.shape) == 2:   # black and white
        raw_image = np.stack([raw_image for _ in range(3)], axis=-1)

    image = tf.image.resize(raw_image, size=(160, 160))
    image = image / 127.5 - 1.0

    annotation = xml.etree.ElementTree.parse('{}/{}.xml'.format(annotations_path, filestem))
    xmin = int(annotation.findtext('./object/bndbox/xmin')) * 160 / orig_width
    xmax = int(annotation.findtext('./object/bndbox/xmax')) * 160 / orig_width
    ymin = int(annotation.findtext('./object/bndbox/ymin')) * 160 / orig_height
    ymax = int(annotation.findtext('./object/bndbox/ymax')) * 160 / orig_height
    box = Box(xmin, xmax, ymin, ymax)

    return image, box

def random_crop(image, box):
    left_crop = np.random.randint(low=0, high=(min(16, box.xmin) + 1))
    right_crop = np.random.randint(low=0, high=(min(16, 160 - box.xmax) + 1))
    top_crop = np.random.randint(low=0, high=(min(16, box.ymin) + 1))
    bottom_crop = np.random.randint(low=0, high=(min(16, 160 - box.ymax) + 1))

    cropped_image = image[top_crop:(160 - bottom_crop), left_crop:(160 - right_crop), :]
    cropped_image = tf.image.resize(cropped_image, size=(160, 160))

    new_xmin = (box.xmin - left_crop) / (160 - left_crop - right_crop) * 160
    new_xmax = (box.xmax - left_crop) / (160 - left_crop - right_crop) * 160
    new_ymin = (box.ymin - top_crop) / (160 - top_crop - bottom_crop) * 160
    new_ymax = (box.ymax - top_crop) / (160 - top_crop - bottom_crop) * 160
    new_box = Box(new_xmin, new_xmax, new_ymin, new_ymax)

    return cropped_image, new_box

def random_flip(image, box):
    if np.random.uniform() < 0.5:
        flipped_image = image[:, ::-1, :]
        new_box = Box((160 - box.xmax), (160 - box.xmin), box.ymin, box.ymax)
        return flipped_image, new_box
    else:
        return image, box

def make_dataset(filestems, images_path, annotations_path, anchor_boxes, jitter, batch_size):

    def generator():
        while True:
            filestem = np.random.choice(filestems)
            image, box = load_image(filestem, images_path, annotations_path)

            if jitter:
                image, box = random_crop(image, box)
                image, box = random_flip(image, box)

            iou_values, xywh_values = get_training_targets(box, anchor_boxes)

            yield (image, iou_values, xywh_values)

    return tf.data.Dataset.from_generator(
                    generator,
                    output_types=(tf.float32, tf.float32, tf.float32),
                    output_shapes=((160, 160, 3), (5, 5, 4, 1), (5, 5, 4, 4))) \
                .batch(batch_size, drop_remainder=True) \
                .repeat(None)

def create_train_test_data(images_path, annotations_path, anchor_boxes,
                           num_test_images, train_batch_size, test_batch_size):
    filestems = [file.split('.')[0] for file in os.listdir(annotations_path)]
    np.random.shuffle(filestems)
    train_filestems = filestems[:-num_test_images]
    test_filestems = filestems[-num_test_images:]

    train_data = make_dataset(train_filestems, images_path, annotations_path, anchor_boxes,
                              jitter=True, batch_size=train_batch_size)
    test_data = make_dataset(test_filestems, images_path, annotations_path, anchor_boxes,
                             jitter=False, batch_size=test_batch_size)

    return train_data, test_data

def build_model():
    inputs = tf.keras.Input(shape=(160, 160, 3), dtype=tf.float32)

    # 1. Feature extraction base (pretrained).
    pretrained_model = tf.keras.applications.mobilenet_v2.MobileNetV2(
            input_shape=(160, 160, 3), include_top=False, weights='imagenet')

    for layer in pretrained_model.layers:
        layer.trainable = False

    feature_extraction_layers = tf.keras.models.Model(
            pretrained_model.input, pretrained_model.get_layer('block_16_project_BN').output)

    feature_values = feature_extraction_layers(inputs)

    # 2. Predictors of IoU and xywh offsets for each anchor.
    iou_hidden = tf.keras.layers.Conv2D(filters=128, kernel_size=3,
                                        padding='same', activation='relu')(feature_values)
    iou = tf.keras.layers.Conv2D(filters=4, kernel_size=1, activation='sigmoid')(iou_hidden)
    iou = tf.keras.layers.Reshape((5, 5, 4, 1))(iou)

    xywh_hidden = tf.keras.layers.Conv2D(filters=256, kernel_size=3,
                                         padding='same', activation='relu')(feature_values)
    xywh = tf.keras.layers.Conv2D(filters=16, kernel_size=1, activation=None)(xywh_hidden)
    xywh = tf.keras.layers.Reshape((5, 5, 4, 4))(xywh)

    return tf.keras.models.Model(inputs, [iou, xywh])

@tf.function
def run_train_step(model, optimizer, images, iou_true, xywh_true, xywh_loss_multiplier):
    with tf.GradientTape() as tape:
        iou_pred, xywh_pred = model(images, training=True)
        iou_loss = tf.keras.losses.BinaryCrossentropy()(iou_true, iou_pred)
        xywh_loss = tf.keras.losses.MeanSquaredError()(xywh_true, xywh_pred,
                                                       sample_weight=tf.squeeze(iou_true))
            # only care about getting xywh correct when anchor box overlaps well with real box
        loss = iou_loss + xywh_loss_multiplier * xywh_loss

    gradients = tape.gradient(loss, model.trainable_variables)
    optimizer.apply_gradients(zip(gradients, model.trainable_variables))

    return loss

def demo_predictions(model, images, iou_true, xywh_true, anchor_boxes, savepath):
    iou_pred, xywh_pred = model(images, training=False)

    for idx in range(images.shape[0]):
        image = images[idx]
        image = 0.5 * np.array(image) + 0.5
        plt.imshow(image)

        box_pred = get_predicted_box(iou_pred[idx], xywh_pred[idx], anchor_boxes)
        rect = plt.Rectangle((box_pred.xmin, box_pred.ymin),
                             width=box_pred.width, height=box_pred.height,
                             fill=False, edgecolor='r', linewidth=2.5)
        plt.gca().add_patch(rect)

        box_true = get_predicted_box(iou_true[idx], xywh_true[idx], anchor_boxes) # any idx works
        rect = plt.Rectangle((box_true.xmin, box_true.ymin),
                             width=box_true.width, height=box_true.height,
                             fill=False, edgecolor='b', linewidth=2.5)
        plt.gca().add_patch(rect)

        plt.savefig('{}/prediction{:02}.png'.format(savepath, idx))
        plt.show()

def main(num_train_batches, train_batch_size, test_batch_size, num_test_images,
         learning_rate, xywh_loss_multiplier,
         images_path, annotations_path, predictions_path):
    '''
    :param num_train_batches: number of training batches
    :param train_batch_size: number of images in a training batch
    :param test_batch_size: number of images to predict on in each demo
    :param num_test_images: number of images to reserve for demo predictions
    :param learning_rate: learning rate for Adam optimizer
    :param xywh_loss_multiplier: weighting to give to loss for centre and width/height offsets
                            relative to loss for intersection-over-unions
    :param images_path: folder in which images are saved
    :param annotations_path: folder in which xml bounding box annotations are saved
    :param predictions_path: folder to save prediction images to
    '''
    anchor_boxes = make_anchor_boxes()
    train_data, test_data = create_train_test_data(
            images_path, annotations_path, anchor_boxes,
            num_test_images, train_batch_size, test_batch_size)
    test_iter = iter(test_data)

    model = build_model()
    optimizer = tf.optimizers.Adam(lr=learning_rate)
    losses = []

    for batch_id, (images, iou_true, xywh_true) in enumerate(train_data):
        loss = run_train_step(model, optimizer, images, iou_true, xywh_true, xywh_loss_multiplier)
        losses.append(loss)

        if (batch_id + 1) % 25 == 0:
            print('After {} batches: Loss = {:.3f}'.format(
                    (batch_id + 1), sum(losses) / len(losses)))
            losses = []

            images, iou_true, xywh_true = next(test_iter)
            demo_predictions(model, images, iou_true, xywh_true,
                             anchor_boxes, savepath=predictions_path)

if __name__ == '__main__':
    images_path = sys.argv[1]
    annotations_path = sys.argv[2]
    predictions_path = sys.argv[3]

    main(num_train_batches=250,
         train_batch_size=64,
         test_batch_size=16,
         num_test_images=128,
         learning_rate=0.001,
         xywh_loss_multiplier=5.0,
         images_path=images_path,
         annotations_path=annotations_path,
         predictions_path=predictions_path)


# -*- coding: utf-8 -*-
"""
Created on Fri Feb  2 10:24:01 2018

@author: xsjxi
"""
import networkx as nx
import math
import weight_clustering3 as wc3

def pair(x, y):
    if (x < y):
        return (x, y)
    else:
        return (y, x)

def GWMI(G):

    edges = nx.edges(G)
    nodes = nx.nodes(G)
    beta = -math.log2(0.0001)
    sim_dict = {}
    g = nx.Graph()

    weight_dic = {}
    for u, v in edges:
        s = len(list(nx.common_neighbors(G, u, v)))
        weight_dic[(u, v)] = s + 1
        weight_dic[(v, u)] = s + 1
        g.add_edge(u, v, weight = weight_dic[(u, v)])
    #print(weight_dic)
    all_weight = 0
    for u, v in edges:
        all_weight = all_weight + weight_dic[(u, v)]
    #print(all_weight)

    nodes_Weight_dict = {}
    weight_list = []

    # 得到每个点的“点权值”
    for v in nodes:
        node_weight = 0
        v_neighbors = nx.neighbors(G,v)
        for u in v_neighbors:
            node_weight += weight_dic[(u, v)]
        weight_list.append(node_weight)
        nodes_Weight_dict[v] = node_weight

    distinct_weight_list = list(set(weight_list))
    #print(nodes_Weight_dict)
    size = len(distinct_weight_list)

    self_Connect_dict = {}
    #得到不同‘点权值’的点之间相连的互信息
    for x in range(size):
        w_x = distinct_weight_list[x]
        for y in range(x, size):
            w_y = distinct_weight_list[y]
            p0 = 1
            (w_n, w_m) = pair(w_x, w_y)
            a = all_weight + 1
            b = all_weight - w_m + 1
            for i in range(1, w_n + 1):
                p0 *= (b - i) / (a - i)
            if p0 == 1:
                self_Connect_dict[(w_n, w_m)] = beta
                self_Connect_dict[(w_m, w_n)] = beta
            else:
                self_Connect_dict[(w_n, w_m)] = -math.log2(1 - p0)
                self_Connect_dict[(w_m, w_n)] = -math.log2(1 - p0)
            #print (str(w_n) + "," + str(w_m))
            #print (self_Connect_dict[(w_n, w_m)])
    #print(self_Connect_dict)
    self_Conditional_dict = {}
    for z in nodes:
        w_z = nodes_Weight_dict[z]
        #d_z = nx.degree(G,z)
        if w_z > 1:
            alpha = 2 / (w_z * (w_z - 1))
            cc_z = wc3.weight_clustering3(g, z)#修改为加权聚类系数
            #cc_z = nx.clustering(G, z)
            if cc_z == 0:
                log_c = beta
            else:
                log_c = -math.log2(cc_z)
            # end if
            s = 0
            neighbor_list = nx.neighbors(G,z)
            size = len(neighbor_list)
            for i in range(size):
                m = neighbor_list[i]
                for j in range(i+1,size):
                    n = neighbor_list[j]
                    (k_x, k_y) = pair(nodes_Weight_dict[m], nodes_Weight_dict[n])
                    if i!=j:
                        s += (self_Connect_dict[(k_x, k_y)] - log_c)
            self_Conditional_dict[z] = alpha * s
    #print(self_Conditional_dict)
    sim_dict = {}  # 存储相似度的字典
    ebunch = nx.non_edges(G)

    for x, y in ebunch:
        s = 0
        (k_x, k_y) = pair(nodes_Weight_dict[x], nodes_Weight_dict[y])
        for z in nx.common_neighbors(G, x, y):
            s += self_Conditional_dict[z]
        sim_dict[(x, y)] = s - self_Connect_dict[(k_x, k_y)]
        # end if
    # end for
    #print(sim_dict)
    return sim_dict

# =============================================================================
# G = nx.Graph()
# G.add_edge(1,2)
# G.add_edge(1,3)
# G.add_edge(1,4)
# G.add_edge(2,4)
# G.add_edge(2,5)
# G.add_edge(5,6)
# G.add_edge(5,7)
# G.add_edge(6,7)
# G.add_edge(6,8)
# G.add_edge(7,8)
# GWMI(G)
# =============================================================================


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pandas as pd
import os

def get_species_from_dex(input_dex):
    with open(input_dex) as f:
        raw_file = f.read()

    species_list = raw_file.split("[SPECIES LIST]")[1].split("[END OF FILE]")[0].split("\n")[1:]

    species = None
    id = None

    species_dict = {}

    for line in species_list:
        if species is not None and id is not None:
            species_dict[id] = species
            species = None
            id = None
        if line.find("Species") == 0:
            species = line[10:]
        if line.find("  ID : ") > 0:
            id = line[8:]
    return species_dict

def get_dex_as_dataframe(input_dex):
    species_dict = get_species_from_dex(input_dex)

    with open(input_dex) as f:
        raw_file = f.read()

    samples_list = raw_file.split("[SPECIES LIST]")[0].split("[SAMPLE ")[1:]

    sample_data = {}

    for sample in samples_list:
        depth = float(sample.split('m')[0])
        sample_id = sample.split("Sample id = ")[1].split("\n")[0]
        counts_dict = {}
        for species_data in sample.split("Species = "):
            if species_data.find("Species id : ") > 0:
                species_id = species_data.split("Species id : ")[1].split("\n")[0]
                count = 0
                if species_data.find("Species count : ") > 0:
                    count = int(species_data.split("Species count : ")[1].split("\n")[0])
                if species_data.find("Abundance : +") > 0:
                    count = 50
                counts_dict[species_id] = count
        sample_data[int(sample_id)] = depth, counts_dict

    df = pd.DataFrame(0, columns=species_dict.keys(), index=sample_data.keys())
    df["Depth"] = None

    for sample_id, sample in sample_data.items():
        depth, counts_dict = sample
        df.at[sample_id, 'Depth'] = depth
        for key, val in counts_dict.items():
            df.at[sample_id, key] = val
    return df

if __name__ == "__main__":

    import glob
    import sys

    if len(sys.argv) != 2:
        "No root given, defaulting to ../data/"
        root = '../data/'
    else:
        root = sys.argv[1]

    filenames = glob.glob(os.path.join(root + '*RAW*.DEX'))

    for filename in filenames:
        df = get_dex_as_dataframe(filename)
        print(df)


vogais = ['a', 'e', 'i', 'o', 'u']

palavra = input()

sifra = []
for i in palavra:
    #as vogais não são modificadas
    if i in vogais:
        sifra += i
    #se consoante
    else:
        #a. inicia com a própria consoante
        sifra += i

        #b. busca vogal mais próxima
        consAscii = ord(i) #transformar a letra em decimal ascii

        menordist = 26 #iniciei com valor alto: tamanho do alfabeto
        proximaVogal = ''
        for j in vogais:
            distAtual = abs(consAscii - ord(j))
            if distAtual < menordist:
                menordist = distAtual
                proximaVogal = j
        sifra += proximaVogal

        #c. busca próxima consoante
        proximaConsoante = consAscii
        if proximaConsoante != 122: #vê se é diferente de 'z'
            proximaConsoante += 1
            if chr(proximaConsoante) in vogais :
                proximaConsoante += 1
        sifra += chr(proximaConsoante)

#imprime a lista como string
print(''.join(sifra))


# Always prefer setuptools over distutils
# To use a consistent encoding
from __future__ import print_function
from codecs import open
import os
from subprocess import check_call

from setuptools import setup, find_packages
from setuptools.command.develop import develop
from setuptools.command.egg_info import egg_info
from setuptools.command.install import install

here = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))

def readme():
    # Get the long description from the README file
    with open(os.path.join(here, 'README.rst'), encoding='utf-8') as f:
        return f.read()

def read_requirements():
    """parses requirements from requirements.txt"""
    reqs_path = os.path.join(here, 'requirements.txt')
    with open(reqs_path, encoding='utf8') as f:
        reqs = [line.strip() for line in f if not line.strip().startswith('#')]

    names = []
    links = []
    for req in reqs:
        if '://' in req:
            links.append(req)
        else:
            names.append(req)
    return {'install_requires': names, 'dependency_links': links}

def custom_command():
    check_call("python -m nltk.downloader universal_tagset punkt wordnet".split())

class CustomInstallCommand(install):
    def run(self):
        custom_command()
        install.run(self)

class CustomDevelopCommand(develop):
    def run(self):
        custom_command()
        develop.run(self)

class CustomEggInfoCommand(egg_info):
    def run(self):
        custom_command()
        egg_info.run(self)

setup(
    name='negbio',

    # Versions should comply with PEP440.  For a discussion on single-sourcing
    # the version across setup.py and the project code, see
    # https://packaging.python.org/en/latest/single_source_version.html
    version='0.9.4',

    description='NegBio: a tool for negation and uncertainty detection',
    long_description=readme(),

    # The project's main homepage.
    url='https://github.com/ncbi-nlp/NegBio.git',

    # Author details
    author='Yifan Peng',
    author_email='yifan.peng@nih.gov',

    license='Public Domain',

    # See https://pypi.python.org/pypi?%3Aaction=list_classifiers
    classifiers=[
        'Development Status :: 3 - Alpha',
        # Indicate who your project is intended for
        'Intended Audience :: Developers',
        'Intended Audience :: Science/Research',

        # Pick your license as you wish (should match "license" above)
        'License :: Public Domain',

        'Operating System :: MacOS',
        'Operating System :: POSIX',
        'Operating System :: POSIX :: Linux',

        # Specify the Python versions you support here.
        'Programming Language :: Python',
        'Topic :: Software Development',
        'Topic :: Software Development :: Libraries :: Application Frameworks',
    ],

    keywords='negbio',

    packages=find_packages(exclude=["tests.*", "tests", "backup", "docs"]),
    include_package_data=True,

    cmdclass={
        'install': CustomInstallCommand,
        'develop': CustomDevelopCommand,
        'egg_info': CustomEggInfoCommand
    },

    entry_points = {
        'console_scripts': ['negbio_pipeline=negbio.negbio_pipeline:main',
                            'main_chexpert=negbio.main_chexpert:main',
                            'main_mm=negbio.main_mm:main'],
    },

    **read_requirements()
)


from math import gcd
pedagang = int(input())
hari = []

for i in range(pedagang):
    hari.append(int(input()))

kpk = 1
for j in hari:
    kpk = kpk*j//gcd(kpk,j)

print(kpk)


import pandas as pd
d={'cities':['lahore','karachi'],'provinces': ['punjab','sindh']}
d2={'cities': ['isalambad','karachi','peshawar','quetta'],'provinces':['capital','sindh','KPK','balochistan']}
f1=pd.DataFrame(d)
f2=pd.DataFrame(d2)
f3=pd.concat([f1, f2])
f3=f3.drop_duplicates()
f3=f3.sort_values(by=['provinces'])
f3=f3.reset_index(drop=True)
print(f3)

Concat sort and reading data from file.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020-08-22 10:57
# @File    : mongo_to_redis.py

import pymongo
from dao import redis_db
from dao.mongo_db import MongoDB

class write_to_redis(object):
    def __init__(self):
        self._redis = redis_db.Redis()
        self.mongo = MongoDB(db='information')
        self.db_loginfo = self.mongo.db_loginfo
        self.collection = self.db_loginfo['content_labels']

    def get_from_mongoDB(self):
        pipelines = [{
            '$group':{
                '_id': "$type"
            }
        }]

        types = self.collection.aggregate(pipelines)
        count = 0
        for type in types:
            cx = {"type": type['_id']}
            data = self.collection.find(cx)
            for info in data:
                result = dict()
                result['title'] = str(info['title'])
                result['describe'] = str(info['describe'])
                result['type'] = str(info['type'])
                result['news_date'] = str(info['news_date'])
                result['content_id'] = str(info['_id'])
                result['likes'] = info['likes']
                result['reads'] = info['reads']
                result['hot_heat'] = info['hot_heat']
                result['collections'] = info['collections']
                print(result)
                # self._redis.redis.delete(str(info['_id']))
                self._redis.redis.set("news_detail:"+str(info['_id']), str(result))

if __name__ == '__main__':
    write_to_redis = write_to_redis()
    write_to_redis.get_from_mongoDB()


import itertools
from pathlib import Path

import numpy as np
import pytest

from mne import Epochs, create_info, io, pick_types, read_events
from mne.channels import make_standard_montage
from mne.preprocessing import equalize_bads, interpolate_bridged_electrodes
from mne.preprocessing.interpolate import _find_centroid_sphere
from mne.transforms import _cart_to_sph

base_dir = Path(__file__).parents[2] / "io" / "tests" / "data"
raw_fname = base_dir / "test_raw.fif"
event_name = base_dir / "test-eve.fif"
raw_fname_ctf = base_dir / "test_ctf_raw.fif"
event_id, tmin, tmax = 1, -0.2, 0.5
event_id_2 = 2

def _load_data():
    """Load data."""
    # It is more memory efficient to load data in a separate
    # function so it's loaded on-demand
    raw = io.read_raw_fif(raw_fname).pick(["eeg", "stim"])
    events = read_events(event_name)
    # subselect channels for speed
    picks = pick_types(raw.info, meg=False, eeg=True, exclude=[])[:15]
    epochs = Epochs(
        raw,
        events,
        event_id,
        tmin,
        tmax,
        picks=picks,
        preload=True,
        reject=dict(eeg=80e-6),
    )
    evoked = epochs.average()
    return raw.load_data(), epochs.load_data(), evoked

@pytest.mark.parametrize("interp_thresh", [0.0, 0.5, 1.0])
@pytest.mark.parametrize("inst_type", ["raw", "epochs", "evoked"])
def test_equalize_bads(interp_thresh, inst_type):
    """Test equalize_bads function."""
    raw, epochs, evoked = _load_data()

    if inst_type == "raw":
        insts = [raw.copy().crop(0, 1), raw.copy().crop(0, 2)]
    elif inst_type == "epochs":
        insts = [epochs.copy()[:1], epochs.copy()[:2]]
    else:
        insts = [evoked.copy().crop(0, 0.1), raw.copy().crop(0, 0.2)]

    with pytest.raises(ValueError, match="between 0"):
        equalize_bads(insts, interp_thresh=2.0)

    bads = insts[0].copy().pick("eeg").ch_names[:3]
    insts[0].info["bads"] = bads[:2]
    insts[1].info["bads"] = bads[1:]

    insts_ok = equalize_bads(insts, interp_thresh=interp_thresh)
    if interp_thresh == 0:
        bads_ok = []
    elif interp_thresh == 1:
        bads_ok = bads
    else:  # interp_thresh == 0.5
        bads_ok = bads[1:]

    for inst in insts_ok:
        assert set(inst.info["bads"]) == set(bads_ok)

def test_interpolate_bridged_electrodes():
    """Test interpolate_bridged_electrodes function."""
    raw, epochs, evoked = _load_data()
    for inst in (raw, epochs, evoked):
        idx0 = inst.ch_names.index("EEG 001")
        idx1 = inst.ch_names.index("EEG 002")
        ch_names_orig = inst.ch_names.copy()
        bads_orig = inst.info["bads"].copy()
        inst2 = inst.copy()
        inst2.info["bads"] = ["EEG 001", "EEG 002"]
        inst2.interpolate_bads()
        data_interp_reg = inst2.get_data(picks=["EEG 001", "EEG 002"])
        inst = interpolate_bridged_electrodes(inst, [(idx0, idx1)])
        data_interp = inst.get_data(picks=["EEG 001", "EEG 002"])
        assert not any(["virtual" in ch for ch in inst.ch_names])
        assert inst.ch_names == ch_names_orig
        assert inst.info["bads"] == bads_orig
        # check closer to regular interpolation than original data
        assert 1e-6 < np.mean(np.abs(data_interp - data_interp_reg)) < 5.4e-5

    for inst in (raw, epochs, evoked):
        idx0 = inst.ch_names.index("EEG 001")
        idx1 = inst.ch_names.index("EEG 002")
        idx2 = inst.ch_names.index("EEG 003")
        ch_names_orig = inst.ch_names.copy()
        bads_orig = inst.info["bads"].copy()
        inst2 = inst.copy()
        inst2.info["bads"] = ["EEG 001", "EEG 002", "EEG 003"]
        inst2.interpolate_bads()
        data_interp_reg = inst2.get_data(picks=["EEG 001", "EEG 002", "EEG 003"])
        inst = interpolate_bridged_electrodes(
            inst, [(idx0, idx1), (idx0, idx2), (idx1, idx2)]
        )
        data_interp = inst.get_data(picks=["EEG 001", "EEG 002", "EEG 003"])
        assert not any(["virtual" in ch for ch in inst.ch_names])
        assert inst.ch_names == ch_names_orig
        assert inst.info["bads"] == bads_orig
        # check closer to regular interpolation than original data
        assert 1e-6 < np.mean(np.abs(data_interp - data_interp_reg)) < 5.4e-5

    # test bad_limit
    montage = make_standard_montage("standard_1020")
    ch_names = [
        ch
        for ch in montage.ch_names
        if ch not in ["P7", "P8", "T3", "T4", "T5", "T4", "T6"]
    ]
    info = create_info(ch_names, sfreq=1024, ch_types="eeg")
    data = np.random.randn(len(ch_names), 1024)
    data[:5, :] = np.ones((5, 1024))
    raw = io.RawArray(data, info)
    raw.set_montage("standard_1020")
    bridged_idx = list(itertools.combinations(range(5), 2))
    with pytest.raises(
        RuntimeError,
        match="The channels Fp1, Fpz, Fp2, AF9, AF7 are bridged "
        "together and form a large area of bridged electrodes.",
    ):
        interpolate_bridged_electrodes(raw, bridged_idx, bad_limit=4)
    # increase the limit to prevent raising
    interpolate_bridged_electrodes(raw, bridged_idx, bad_limit=5)
    # invalid argument
    with pytest.raises(
        ValueError, match="Argument 'bad_limit' should be a strictly positive integer."
    ):
        interpolate_bridged_electrodes(raw, bridged_idx, bad_limit=-4)

def test_find_centroid():
    """Test that the centroid is correct."""
    montage = make_standard_montage("standard_1020")
    ch_names = [
        ch
        for ch in montage.ch_names
        if ch not in ["P7", "P8", "T3", "T4", "T5", "T4", "T6"]
    ]
    info = create_info(ch_names, sfreq=1024, ch_types="eeg")
    info.set_montage(montage)
    montage = info.get_montage()
    pos = montage.get_positions()
    assert pos["coord_frame"] == "head"

    # look for centroid between T7 and TP7, an average in spehrical coordinate
    # fails and places the average on the wrong side of the head between T8 and
    # TP8
    ch_names = ["T7", "TP7"]
    pos_centroid = _find_centroid_sphere(pos["ch_pos"], ch_names)
    _check_centroid_position(pos, ch_names, pos_centroid)

    # check other positions
    pairs = [
        ("CPz", "CP2"),
        ("CPz", "Cz"),
        ("Fpz", "AFz"),
        ("AF7", "F7"),
        ("O1", "O2"),
        ("M2", "A2"),
        ("P5", "P9"),
    ]
    for ch_names in pairs:
        pos_centroid = _find_centroid_sphere(pos["ch_pos"], ch_names)
        _check_centroid_position(pos, ch_names, pos_centroid)
    triplets = [("CPz", "Cz", "FCz"), ("AF9", "Fpz", "AF10"), ("FT10", "FT8", "T10")]
    for ch_names in triplets:
        pos_centroid = _find_centroid_sphere(pos["ch_pos"], ch_names)
        _check_centroid_position(pos, ch_names, pos_centroid)

def _check_centroid_position(pos, ch_names, pos_centroid):
    """Check the centroid distance.

    The cartesian average should be distanced from pos_centroid by the
    difference between the radii.
    """
    radii = list()
    cartesian_positions = np.zeros((len(ch_names), 3))
    for i, ch in enumerate(ch_names):
        radii.append(_cart_to_sph(pos["ch_pos"][ch])[0, 0])
        cartesian_positions[i, :] = pos["ch_pos"][ch]
    avg_radius = np.average(radii)
    avg_cartesian_position = np.average(cartesian_positions, axis=0)
    avg_cartesian_position_radius = _cart_to_sph(avg_cartesian_position)[0, 0]
    radius_diff = np.abs(avg_radius - avg_cartesian_position_radius)
    # distance
    distance = np.linalg.norm(pos_centroid - avg_cartesian_position)
    assert np.isclose(radius_diff, distance, atol=1e-6)


# -*- coding: utf-8 -*-
"""
Created on Tue Feb 25 00:57:23 2020

@author: Lenovo
"""

from keras.layers import Input, Dense
from keras.models import Model
import warnings
warnings.filterwarnings('ignore')
# 编码潜在空间表征维度
encoding_dim = 32
 # 自编码器输入
input_img = Input(shape=(784,))
# 使用一个全连接网络来搭建编码器
encoded = Dense(encoding_dim, activation='relu')(input_img)
# 使用一个全连接网络来对编码器进行解码
decoded = Dense(784, activation='sigmoid')(encoded)
# 构建keras模型
autoencoder = Model(input=input_img, output=decoded)

# 编码器模型
encoder = Model(input=input_img, output=encoded)
# 解码器模型
encoded_input = Input(shape=(encoding_dim,))
decoder_layer = autoencoder.layers[-1]
decoder = Model(input=encoded_input, output=decoder_layer(encoded_input))

# 编译模型
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
# 准备mnist数据
from keras.datasets import mnist
import numpy as np
(x_train, _), (x_test, _) = mnist.load_data()
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))# 训练
autoencoder.fit(x_train, x_train,
                nb_epoch=50,
                batch_size=256,
                shuffle=True,
                validation_data=(x_test, x_test))

import matplotlib.pyplot as plt

encoded_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)
n = 10
plt.figure(figsize=(20, 4))
for i in range(1, n):
    # 展示原始图像
    ax = plt.subplot(2, n, i)
    plt.imshow(x_test[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    # 展示自编码器重构后的图像
    ax = plt.subplot(2, n, i + n)
    plt.imshow(decoded_imgs[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

plt.show()


from bs4 import BeautifulSoup
import requests

class AthonScrapJogadores(object):
    def __init__(self):
        self.page = None
        self.soup = None

    def jogadores(self, url):
        self.page = requests.get(url)
        soup = BeautifulSoup(self.page.content, "html.parser").findAll("table")
        jogadores = []

        #  obter os jogadores ativos
        ativos = [table for table in soup if 'Active' in table.text]
        if ativos != []:
            ativos = [tr for tr in ativos[0].findAll('tr')]

            #  obter a lista de jogadores ativos, (exceto coach).
            for ativo in ativos:
                # as informações precisa estar conditas em td
                if len(ativo.findAll("td")) > 0:
                    #  realizar um corte para não carregar o coach
                    if '(Coach)' not in ativo.text:
                        # time composto de apenas 5 jogadores
                        if len(jogadores) < 5:
                            jogadores.append(ativo)
        return jogadores


# 7*7 격자판 가로방향 또는 세로방향으로 길이 5자리 회문수가 몇 개 있는지 구하기
import sys
sys.stdin=open("input.txt", "rt")

board = [list(map(int, input().split())) for _ in range(7)]

cnt = 0
for i in range(7):
    for mid in range(2, 5):
        if board[i][mid-1] == board[i][mid+1] and board[i][mid-2] == board[i][mid+2]:
            cnt += 1
        if board[mid-1][i] == board[mid+1][i] and board[mid-2][i] == board[mid+2][i]:
            cnt += 1
print(cnt)


from selvpcclient.base import CLICommand, ListCommand
from selvpcclient.formatters import format_servers
from selvpcclient.util import (add_resource_filter_arguments, confirm_action,
                               handle_http_error)

class Add(ListCommand):
    """Add new vrrp subnet into project"""

    columns = ['id', 'project_id', 'cidr', 'status']
    sorting_support = True

    def get_parser(self, prog_name):
        parser = super(ListCommand, self).get_parser(prog_name)
        parser.add_argument(
            'project_id',
            metavar="<project_id>"
        )
        parser.add_argument(
            '-r',
            '--region',
            required=True,
            action='append',
            dest="regions",
        )
        parser.add_argument(
            '--type',
            default="ipv4",
        )
        parser.add_argument(
            '-p',
            '--prefix',
            default=29,
            type=int,
        )
        parser.add_argument(
            '--quantity',
            default=1,
            type=int,
        )
        return parser

    @handle_http_error
    def take_action(self, parsed_args):
        body = {
            "vrrp_subnets": [
                {
                    "regions": parsed_args.regions,
                    "prefix_length": parsed_args.prefix,
                    "type": parsed_args.type,
                    "quantity": parsed_args.quantity,
                }
            ]
        }
        result = self.app.context["client"].vrrp.add(
            parsed_args.project_id, body
        )
        return self.setup_columns(result, parsed_args)

class Show(ListCommand):
    """Show detailed vrrp subnet information"""

    columns = [
        'id', 'project_id', 'cidr', 'status', 'servers'
    ]
    _formatters = {"servers": format_servers}

    def get_parser(self, prog_name):
        parser = super(ListCommand, self).get_parser(prog_name)
        parser.add_argument(
            'id',
            metavar="<vrrp_id>"
        )
        return parser

    @handle_http_error
    def take_action(self, parsed_args):
        result = self.app.context["client"].vrrp.show(parsed_args.id)
        return self.setup_columns([result._info], parsed_args)

class Delete(CLICommand):
    """Delete vrrp subnet from project"""

    def get_parser(self, prog_name):
        parser = super(CLICommand, self).get_parser(prog_name)
        parser.add_argument(
            'id',
            metavar="<vrrp_id>",
            nargs='+'
        )
        parser.add_argument(
            '--yes-i-really-want-to-delete',
            default=False,
            action='store_true'
        )
        return parser

    @confirm_action("delete")
    def take_action(self, parsed_args):
        self.app.context["client"].vrrp.delete_many(
            parsed_args.id,
            raise_if_not_found=False
        )

class List(ListCommand):
    """List of vrrp subnets"""

    columns = ['id', 'project_id', 'cidr', 'status']
    sorting_support = True

    def get_parser(self, prog_name):
        parser = super(ListCommand, self).get_parser(prog_name)
        parser.add_argument(
            '--detailed',
            default=False,
            action='store_true'
        )
        add_resource_filter_arguments(parser, add_region=False)
        return parser

    @handle_http_error
    def take_action(self, parsed_args):
        if parsed_args.detailed:
            self.columns.append('servers')
            self._formatters = {"servers": format_servers}

        result = self.app.context["client"].vrrp.list(
            project_id=parsed_args.project_id,
        )
        return self.setup_columns(result, parsed_args)


import numpy as np
from svc_sdss import pack_specobjid
from svc_sdss import unpack_specobjid

DEBUG = False

def test_sdss ():
    '''
    '''
    id1 = np.array([4565636362342079488,
                   4565636637219987456,
                   4566762812004657152,
                   6439153884405714944], dtype=np.uint64)

    if DEBUG: print('INPUT ID ==========')
    if DEBUG: print(id1)

    u = unpack_specobjid(id1)
    p1,m1,f1,r1 = u.plate, u.mjd, u.fiber, u.run2d
    if DEBUG: print(p1,m1,f1,r1)

    # Repack the values to see if we recover the IDs
    id2 = pack_specobjid(p1, m1, f1, r1)
    if DEBUG: print('REPACKED ID VALUES ==========')
    if DEBUG: print(id2)

    u = unpack_specobjid(id2)
    p2,m2,f2,r2 = u.plate, u.mjd, u.fiber, u.run2d
    if DEBUG: print(p2,m2,f2,r2)

    assert np.array_equal(p1, p2),"Plate values don't match"
    assert np.array_equal(m1, m2),"MJD values don't match"
    assert np.array_equal(f1, f2),"Fiber values don't match"
    assert np.array_equal(r1, r2),"Run2d values don't match"
    assert np.array_equal(id1, id2),"ID values don't match"

def test_specobjid ():

    specobjid = 2210146812474530816
    plate = 1963
    mjd = 54331
    fiber = 19
    run2d = 103

    s = pack_specobjid (plate, mjd, fiber, run2d)
    assert s[0] == specobjid,'Bad specobjid'

    u = unpack_specobjid (specobjid)
    assert u.plate[0] == plate,'Bad plate value'
    assert u.mjd[0] == mjd,'Bad mjd value'
    assert u.fiber[0] == fiber,'Bad fiber value'
    assert u.run2d[0] == str(run2d),'Bad run2d value'

test_sdss()
test_specobjid ()


#!/usr/bin/env python3

import argparse
import os
from os import listdir
from os.path import isfile, join

from Bio import Phylo
from treespace.create_trees import enum_trees
from treespace.drawing import draw_tree
from treespace.francis import vertex_disjoint_paths, rooted_spanning_tree, tree_based_network
from treespace.jetten import is_tree_based
from treespace.max_cst import maximum_covering_subtree
from treespace.utils import read_adjacency_list, create_dag

import subprocess

# Used by both offline and online method to analyze metrics of graphs, and store output
def analyze_generated_graphs(input_dir: str, is_newick: bool, draw_image: bool):
    list_of_network_files = [f for f in listdir(input_dir) if isfile(join(input_dir, f))]
    output_image_dir = os.path.join(input_dir, 'images')
    os.makedirs(output_image_dir, exist_ok=True)

    # Create Headers of CSV results like answers.csv
    metric_path = os.path.join(output_image_dir, "metrics.csv")
    with open(metric_path, 'w+') as fd:
        fd.write('graph,is_tree_based,max_cst,spanning_tree,rooted_tree\n')

    for network_file in list_of_network_files:
        if is_newick:
            graph = Phylo.read(join(input_dir, network_file), 'newick')
            graph = Phylo.to_networkx(graph)
            graph = create_dag(graph)
        else:
            graph = read_adjacency_list(join(input_dir, network_file))

        network_name = network_file.split('.')[0]
        print("Opening the phylogenetic network: " + network_name)
        graph_drawing_location = os.path.join(output_image_dir, network_name)
        draw_tree(graph, graph_drawing_location)

        # Obtain Metrics and Print
        tree_based = is_tree_based(graph)
        _, eta = maximum_covering_subtree(graph, graph_drawing_location, draw_image)
        missing_v1, paths = vertex_disjoint_paths(graph, graph_drawing_location, draw_image)
        tree_list = enum_trees(graph, graph_drawing_location, draw_image)

        # Print Spanning Tree and New Leaf network
        spanning_tree = rooted_spanning_tree(graph, paths)
        draw_tree(spanning_tree, graph_drawing_location + '-spanning-tree')
        new_tree_based_network = tree_based_network(graph, spanning_tree)
        draw_tree(new_tree_based_network, graph_drawing_location + '-spanning-tree-with-leaves')

        with open(metric_path, 'a+') as metric:
            if tree_based:
                metric.write(network_name + '1,' + str(eta) + ',' + str(missing_v1) + ',' + str(len(tree_list)) + '\n')
            else:
                metric.write(network_name + '0,' + str(eta) + ',' + str(missing_v1) + ',' + str(len(tree_list)) + '\n')

# Creates random Phylogenetic Networks
# These networks are usually tree-based or almost tree-based. I need to make it more random somehow...
# Query this site: http://phylnet.univ-mlv.fr/tools/randomNtkGenerator.php
def create_local_random_dag(num_leaves: int, num_reticulation: int, num_dataset: int) -> str:
    # Create new phylogenetic networks
    subprocess.call(['./phylo_generator/binary_ntk_generator',
                     str(num_leaves), str(num_reticulation),
                     str(num_dataset)])

    # move all the graphs into a directory
    input_dir = 'output_ret=' + str(num_reticulation) + '_leaves=' + str(num_leaves)
    os.makedirs(input_dir, exist_ok=True)

    for random_network in range(0, num_dataset):
        random_network = "0%d" % random_network
        subprocess.call(['mv', random_network, random_network + '.txt'])
        subprocess.call(['mv', random_network, input_dir])
    return input_dir

def main():
    parser = argparse.ArgumentParser(prog='A python program that can run algorithms used to '
                                          'compute Phylogenetic network metrics')
    group = parser.add_mutually_exclusive_group()
    parser.add_argument('--draw', '-d', dest='draw', action='store_true',
                        help="Draw all Bipartite Graphs, Trees, etc.")

    # Collect for arguments on generating random graphs
    # num_leaves=10, num_reticulation=5, num_dataset=10
    parser.add_argument('--leaves', '-l', nargs='?', dest='leaves', action='store',
                        help="number of leaves in each graph", const=1, default=10, type=int)
    parser.add_argument('--reticulation', '-r', nargs='?', dest='num_reticulation', action='store',
                        help="number of reticulation vertices for each graph", const=1, default=10,
                        type=int)
    parser.add_argument('--graphs', '-g', nargs='?', dest='num_dataset', action='store',
                        help="num of random graphs to generate", const=1, default=10, type=int)
    parser.add_argument('--dir', nargs='?', dest='dir', action='store',
                        help="Directory containing either NetworkX Adjacency List or Newick formatted graphs", type=str)
    parser.add_argument('--newick', '-n', dest='newick', action='store_true',
                        help='Identify the input is Newick data')
    group.add_argument('--generate', dest='generate', action='store_true',
                       help="Generate a new folder with random binary phylogenetic networks and collect metrics")

    args = parser.parse_args()

    if args.generate:
        new_dir = create_local_random_dag(args.leaves, args.num_reticulation, args.num_dataset)
        analyze_generated_graphs(new_dir, False, args.draw)
    else:
        analyze_generated_graphs(args.dir, args.newick, args.draw)

if __name__ == '__main__':
    main()


## 첫째 줄에 입력으로 주어진 N개의 정수 중에 v가 몇 개인지 출력한다.
num_input = int(input())
a = input().split()
a = [int(i) for i in a]
b = int(input())
result = 0
for i in range(num_input):
    if a[i] == b:
        result += 1
print(result)

## N개의 정수가 주어진다. 이때, 최솟값과 최댓값을 구하는 프로그램을 작성하시오.
a = int(input())
input_list = input().split()
input_list = [int(i) for i in input_list]
print(min(input_list),max(input_list))

## 9개의 서로 다른 자연수가 주어질 때, 이들 중 최댓값을 찾고 그 최댓값이 몇 번째 수인지를 구하는 프로그램을 작성하시오.
# 예를 들어, 서로 다른 9개의 자연수
# 3, 29, 38, 12, 57, 74, 40, 85, 61
# 이 주어지면, 이들 중 최댓값은 85이고, 이 값은 8번째 수이다.
input_list = []
while 1:
    try:
        a = int(input())
        input_list.append(a)
    except:
        break
print(max(input_list))
print(input_list.index(max(input_list))+1)

## 세 개의 자연수 A, B, C가 주어질 때 A × B × C를 계산한 결과에 0부터 9까지 각각의 숫자가 몇 번씩 쓰였는지를 구하는 프로그램을 작성하시오.
## 예를 들어 A = 150, B = 266, C = 427 이라면 A × B × C = 150 × 266 × 427 = 17037300 이 되고, 계산한 결과 17037300 에는 0이 3번, 1이 1번, 3이 2번, 7이 2번 쓰였다.
input_list = []
A = int(input())
B = int(input())
C = int(input())
final_value = str(A * B * C)
for i in range(10):
    print(final_value.count(str(i)))

## 두 자연수 A와 B가 있을 때, A%B는 A를 B로 나눈 나머지 이다. 예를 들어, 7, 14, 27, 38을 3으로 나눈 나머지는 1, 2, 0, 2이다.
## 수 10개를 입력받은 뒤, 이를 42로 나눈 나머지를 구한다. 그 다음 서로 다른 값이 몇 개 있는지 출력하는 프로그램을 작성하시오.
나머지_리스트 = []
while 1:
    try:
        a = int(input())
        나머지 = a%42
        if 나머지 not in 나머지_리스트:
            나머지_리스트.append(나머지)
    except:
        break
print(len(나머지_리스트))

## 세준이는 기말고사를 망쳤다. 세준이는 점수를 조작해서 집에 가져가기로 했다. 일단 세준이는 자기 점수 중에 최댓값을 골랐다. 이 값을 M이라고 한다. 그리고 나서 모든 점수를 점수/M*100으로 고쳤다.
## 예를 들어, 세준이의 최고점이 70이고, 수학점수가 50이었으면 수학점수는 50/70*100이 되어 71.43점이 된다.
## 세준이의 성적을 위의 방법대로 새로 계산했을 때, 새로운 평균을 구하는 프로그램을 작성하시오.
a = int(input())
score = input().split()
score = [int(i) for i in score]
num = 0
for i in score:
    i = i/max(score) *100
    num += i
print(num/a)

## "OOXXOXXOOO"와 같은 OX퀴즈의 결과가 있다. O는 문제를 맞은 것이고, X는 문제를 틀린 것이다. 문제를 맞은 경우 그 문제의 점수는 그 문제까지 연속된 O의 개수가 된다. 예를 들어, 10번 문제의 점수는 3이 된다.
## "OOXXOXXOOO"의 점수는 1+2+0+0+1+0+0+1+2+3 = 10점이다.
## OX퀴즈의 결과가 주어졌을 때, 점수를 구하는 프로그램을 작성하시오.
a = int(input())
for i in range(a):
    input_case = list(input())
    for i in range(len(input_case)):
        if ((i==0) & (input_case[i] == 'O')):
            input_case[i] = '1'
        elif((i==0) & (input_case[i] == 'X')):
            input_case[i] = '0'
        else:
            if input_case[i] == 'X':
                input_case[i] = '0'
            elif ((input_case[i] == 'O') & (input_case[i-1] == '0')):
                input_case[i] = '1'
            else:
                if input_case[i-1] != '0':
                    input_case[i] = str(int(input_case[i-1]) + 1)
    b = 0
    for i in input_case:
        b += int(i)
    input_case
    print(b)

## 대학생 새내기들의 90%는 자신이 반에서 평균은 넘는다고 생각한다. 당신은 그들에게 슬픈 진실을 알려줘야 한다.
a = int(input())
for i in range(a):
    information = input().split()
    information = [int(i) for i in information]
    mean_value = sum(information[1:])/information[0]
    over_value = 0
    for i in range(information[0]):
        if mean_value < information[i+1]:
            over_value += 1
        else:
            pass
    print("{:.3f}%".format(round((over_value/information[0]) * 100, 3)))

## X대학 M교수님은 프로그래밍 수업을 맡고 있다. 교실엔 학생이 30명이 있는데, 학생 명부엔 각 학생별로 1번부터 30번까지 출석번호가 붙어 있다.
## 교수님이 내준 특별과제를 28명이 제출했는데, 그 중에서 제출 안 한 학생 2명의 출석번호를 구하는 프로그램을 작성하시오.
## 출력은 2줄이다. 1번째 줄엔 제출하지 않은 학생의 출석번호 중 가장 작은 것을 출력하고, 2번째 줄에선 그 다음 출석번호를 출력한다.
a = [i+1 for i in range(30)]
for i in range(28):
    b = int(input()) - 1
    a[b] = 0

for k in a:
    if k==0:
        pass
    else:
        print(k)

백준/1차원 배열.py


from django.contrib import admin
from .models import *

# Register your models here.

class BannerMessagesInline(admin.TabularInline):
    model = BannerMessages
    extra = 3

class MainBannerInline(admin.TabularInline):
    model = MainBanner
    extra = 1

class MainBannerAdmin(admin.ModelAdmin):
    display_list = ['title', 'active', 'page_related']
    filter_list = ['active', 'page_related']

class HomepageAdmin(admin.ModelAdmin):
    readonly_fields = ['tiny_background_image', 'tiny_circle_image']
    display_list = ['title', 'active',]
    filter_list = ['active']
    inlines = [MainBannerInline, BannerMessagesInline]
    fieldsets = (
        ('Βασικά Χαρακτηριστικά', {
            'fields': (
                ('title', 'keywords', 'description'),
                ('tiny_background_image','background_image'),
                ('tiny_circle_image', 'circle_image')
                )
        }),
        ('Διαχείρηση υπόλοιπων Σελίδων', {
            'classes': ('collapse',),
            'fields': (
                ('blog_title', 'blog_keywords', 'blog_description'),
                ('store_title', 'store_keywords', 'store_description'),
                ('about_title', 'about_keywords', 'about_description'),
                ),
        }),
    )

@admin.register(CircleImages)
class CircleImagesAdmin(admin.ModelAdmin):
    pass

@admin.register(InstagramFeed)
class InstagramFeedAdmin(admin.ModelAdmin):
    pass

admin.site.register(Homepage, HomepageAdmin)
admin.site.register(MainBanner, MainBannerAdmin)
admin.site.register(BannerMessages)
admin.site.register(IconMessages)
admin.site.register(ContactInfo)
admin.site.register(Phones)


import pygame

class sound:

    def __init__(self,soundName):
        self.soundName = soundName
        self.muted = False
        self.clicked = False

    def play(self):
        if self.muted == False:
            s = pygame.mixer.Sound(self.soundName)
            s.set_volume(0.5)
            s.play()

    def stop(self):
        if self.clicked == False:
            self.muted = True
            self.clicked = True
        else:
            self.muted = False
            self.clicked = False


from base64 import b64encode, b64decode
from Cryptodome.Cipher import DES

k2 = [0x6E, 0x06, 0x15, 0x51, 0x93, 0x5B, 0x07, 0xEA]
key = bytes(k2)
x = DES.new(key, DES.MODE_ECB)
s = b"GcDk0SvnNA1tsmp5FCK1FpSDfUXZbhHBSPheZaixuMyzqyysOAPCPB/p7sMpmK1KZo+lPfhMZxw="
c = b64decode(s)# 解密
p = x.decrypt(c)# 还原奇偶字符
x = b64decode(p)
y = b64decode(p[28:-4])
for i in range(37):
    if(i%2==0):
        print(chr(x[i//2]), end='')
    else:
        print(chr(y[i//2]), end='')


#!/usr/bin/env python

from __future__ import division
from __future__ import print_function

'''
/**
 * @file init_elphel393.py
 * @brief 10393 related init script
 * @copyright Copyright (C) 2016 Elphel Inc.
 * @author Oleg Dzhimiev <oleg@elphel.com>
 * @deffield updated: unknown
 *
 * @par <b>License</b>:
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
'''

__author__ = "Oleg Dzhimiev"
__copyright__ = "Copyright (C) 2016 Elphel, Inc."
__license__ = "GPL"
__version__ = "3.0+"
__maintainer__ = "Oleg Dzhimiev"
__email__ = "oleg@elphel.com"
__status__ = "Development"

import subprocess
import sys
import time
import os

#params
SENSOR_TYPE = 5
IPADDR = "192.168.0.9"
NETMASK = "255.255.255.0"
IMGSRV_PORT = 2323
CAMOGM_PORT = 3456
CAMOGM_PIPE = "/var/volatile/camogm_cmd"
SATA_EN = 1

PYDIR = "/usr/local/bin"
VERILOG_DIR = "/usr/local/verilog"
LOGFILE = "/var/log/init_elphel393.log"
FPGA_VERION_FILE = '/sys/devices/soc0/elphel393-framepars@0/fpga_version'
TIMEOUT = 120

#functions

def get_fpga():
    with open(FPGA_VERION_FILE,'r') as f:
        try:
            return f.read()
        except:
            return None

def fpga_OK(timeout):
    '''
    Get fpga bitstream version once it's available
    @timeout - int - seconds
    '''
    ntry = 0
    fpga = None
    while not fpga and ((not timeout) or (ntry<timeout)):
        fpga = get_fpga()
        print ('.',end='')
        sys.stdout.flush()
        ntry += 1
        time.sleep(1)
    return fpga

def colorize(string, color, bold):
    '''
    Colors for terminal output
    @string - string - text to print
    @color - string - see below
    @bold - bool - True/False
    '''
    color=color.upper()
    attr = []
    if color == 'RED':
        attr.append('31')
    elif color == 'GREEN':
        attr.append('32')
    elif color == 'YELLOW':
        attr.append('33')
    elif color == 'BLUE':
        attr.append('34')
    elif color == 'MAGENTA':
        attr.append('35')
    elif color == 'CYAN':
        attr.append('36')
    elif color == 'GRAY':
        attr.append('37')
    else:
        pass
        # red
    if bold:
        attr.append('1')
    return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)

def log_msg(msg, mode=0):
    '''
    Print log message
    @msg
    @mode - int - 0,2,3,4
    '''
    bold = False
    color = ""
    if mode == 2: #bold red - error
        color = "RED"
        bold = True
    elif mode == 3: # just bold
        bold = True
    elif mode == 4: # just bold
        bold = True
        color = "YELLOW" #warning

    with open ('/proc/uptime') as f:
        t=float(f.read().split()[0])
    with open(LOGFILE,'a') as msg_file:
        print("[%8.2f]  %s"%(t,msg),file=msg_file)
    if bold or color:
        msg = colorize(msg,color,bold)
    print (colorize("[%8.2f] %s: "%(t, sys.argv[0].split('/')[-1].split('.')[0]),'CYAN',0)+msg)

def shout(cmd):
    '''
    call subprocess with shell=True
    @cmd
    '''
    #subprocess.call prints to console
    subprocess.call(cmd,shell=True)

def init_ipaddr(ip, mask):
    '''
    simple 'ifconfig'
    @ip - string - ip address
    @mask - string - netmask
    '''
    shout("ifconfig eth0 "+ip+" netmask "+mask)

def init_imgsrv(port):
    '''
    Start Image Server (imgsrv), restart lighttpd (for imgsrv?) and initialize exif header template
    @port - int or str
    '''
    shout("imgsrv -p "+str(port))
    #restart PHP - it can get errors while opening/mmaping at startup, then some functions fail
    shout("killall lighttpd; /usr/sbin/lighttpd -f /etc/lighttpd.conf")
    shout("/www/pages/exif.php init=/etc/Exif_template.xml")

def init_autoexp_daemon(index):
    '''
    Start autoexposure daemon, provide port
    @index - string
    '''
    shout("autoexposure -p "+index+" -c 0 -b 0 -d 1 &")

def init_sata(sata_en):
    '''
    SATA init
    @sata_en - int - 0 or 1
    '''
    # init SATA only if 10389 board is present
    if os.path.isfile("/sys/devices/soc0/elphel393-pwr@0/detected_10389"):
        if (sata_en==1):
            # SATA core is implented in fpga - first chech, then wait until programmed
            if not get_fpga():
                log_msg ("Waiting for the FPGA to be programmed to start SATA", 4)
                # look up TIMEOUT in the params in the beginning of the file
                if not fpga_OK(TIMEOUT):
                    log_msg ("Timeout while waiting for the FPGA to be programmed", 2)
                    return
                else:
                    log_msg ("Done waiting for the FPGA", 4)

            # default init will connect to internal SSD
            shout(PYDIR+"/x393sata.py")  # Should be after modprobe? Wait for the FPGA should be before it

            # for Eyesis4Pi (temporary fix)
            if switch['eyesis']!=0:

              log_msg ("Setting VSC3304 for Eyesis4Pi (driver will be loaded)", 4)
              log_msg ("  Details : /var/log/x393sata_control.log")
              log_msg ("  State   : /var/state/ssd")

              # Option 1: use internal SSDs: zynq <-> internal ssd, AHCI module will be loaded by the script
              # uncomment for use
              #shout(PYDIR+"/x393sata_control.py set_zynq_ssd")

              # Option 2: use external SSDs: zynq <-> external ssd, AHCI module will be loaded by the script
              # uncomment for use
              shout(PYDIR+"/x393sata_control.py set_zynq_esata")
            else:
              # load the AHCI driver module
              shout("modprobe ahci_elphel &")
              shout("sleep 2")
              shout("echo 1 > /sys/devices/soc0/amba@0/80000000.elphel-ahci/load_module")
    else:
        log_msg ("10389 was not detected: skipping SATA init")

def init_usb_hub():
    '''
    Initializes USB HUB on 10389 board (stays initialized through reboot, does not respond after initialized)
    '''
    if not os.path.exists('/sys/bus/usb/devices/1-1'):
        if os.path.isfile('/sys/devices/soc0/elphel393-pwr@0/detected_10389'):
            shout("i2cset -y 0 0x2c 0xff 0x0201 w")
            shout("i2cset -y 0 0x2c 0xff 0x0001 w")
            shout("i2cset -y 0 0x2c 0x00 0x3401 w")
            shout("i2cset -y 0 0x2c 0x01 0x1201 w")
            shout("i2cset -y 0 0x2c 0x06 0x9b01 w")
            shout("i2cset -y 0 0x2c 0x07 0x1001 w")
            shout("i2cset -y 0 0x2c 0x08 0x0001 w")
            shout("i2cset -y 0 0x2c 0xff 0x0101 w")
            log_msg ("Initialized USB hub with Vendor=1234")
        else:
            log_msg ("10389 was not detected: skipping USB hub init")
    else:
        log_msg ("USB hub was already initialized")

def start_gps_compass():
    '''
    Detect GPS and/or compass boards and start them
    '''
    if not get_fpga():
        log_msg ("Waiting for the FPGA to be programmed to start SATA", 4)
        if not fpga_OK(TIMEOUT):
            print()
            log_msg ("Timeout while waiting for the FPGA to be programmed", 2)
            return
        else:
            log_msg ("Done waiting for the FPGA", 4)
    shout("start_gps_compass.php")

def disable_gpio_10389():
    '''
    Is needed for Eyesis4Pi because GPIO is used to power something else
    '''
    gpio_10389 = "/sys/devices/soc0/elphel393-pwr@0/gpio_10389"
    shout("echo '0x101' > "+gpio_10389) #power on
    shout("echo '0x100' > "+gpio_10389) #power off
    time.sleep(1)

#main

# default
switch = {
    'usb_hub':1,
    'ip':1,
    'imgsrv':1,
    'autoexp_daemon':1,
    'autocampars':1,
    'sata':1,
    'gps':1,
    'eyesis':0
    }

# update 'switch' dictionary from argv[1] which should be evaluated as dictionary as well:
# look it up in /etc/init.d/init_elphel393
if len(sys.argv) > 1:
    switch.update(eval(sys.argv[1]))

#pre
volatile_html = '/var/volatile/html'
if not os.path.exists(volatile_html):
    os.mkdir(volatile_html)

# need to disable fan for eyesis
# if switch['eyesis']!=0:
#    log_msg('Eyesis mode: turn off gpio 10389')
#    disable_gpio_10389()

# Start temperature monitor and let it control fan (set 'off' to disable fan control)
# Move tempon early, so other problems will not block it
if switch['eyesis']!=0:
    log_msg('Eyesis mode: turn off gpio 10389')
    disable_gpio_10389()
    log_msg("Eyesis: fan off")
    shout("tempmon.py --control_fan off &")
else:
    log_msg("Fan on")
    shout("tempmon.py --control_fan on &")

'''
1: program USB hub
'''
if switch['usb_hub']==1:
    log_msg('Initialize USB hub')
    init_usb_hub()
else:
    log_msg("skip USB hub initiualization")

'''
2: set ip and netmask
'''
if switch['ip']==1:
    log_msg(sys.argv[0]+": ip = "+IPADDR+", mask = "+NETMASK)
    init_ipaddr(IPADDR, NETMASK)
else:
    log_msg("skip ip")

'''
3: init image server
'''
if switch['imgsrv']==1:
    log_msg(sys.argv[0]+": imgsrv")
    init_imgsrv(IMGSRV_PORT)
else:
    log_msg("skip imgsrv")

'''
4: init autoexposure daemon - try for all possible ports
'''
log_msg(sys.argv[0]+": auto exposure daemon")
if switch['autoexp_daemon']==1:
    for i in range(1,5):
        init_autoexp_daemon(str(i-1))

'''
5: Init camera parameters
   * FPGA gets programmed at this step - default bitstream is used which
     depends on the camera type programmed in 10389 if detected.
     If the board is not found then the default bitstream is:
     /usr/local/verilog/x393_parallel.bit
'''
if switch ['autocampars'] == 1:
#   shout("autocampars.php --init --ignore-revision")
    shout("autocampars.php --init")

'''
6: Init SATA
   * If the bitstream wasn't programmed at the previous step
     it will be programmed here by x339sata.py,
     the default bitstream is /usr/local/verilog/x393_sata.bit
     but it does not include the camera part
'''
if switch['sata']==1:
    log_msg("init SATA")
    init_sata(SATA_EN)
else:
    log_msg("skip SATA")

'''
7: Start GPS/IMU if any detected
'''
if switch['gps']==1:
    log_msg('Start GPS and event logger')
    start_gps_compass()
else:
    log_msg("skip GPS")

# create directory for camogm pipes, symlink /var/state should already be in rootfs

if not os.path.exists("/var/volatile/state"):
    os.mkdir('/var/volatile/state')
else:
    log_msg ("/var/volatile/state already exists")

log_msg("DONE, log file: "+LOGFILE, 3)


from django.contrib import admin
from .models import Transacao, TransacaoAfiliadoAdministrador

def processo_id(obj):
    return obj.pedido.id
processo_id.short_description = 'ID do Processo'

@admin.register(Transacao)
class TransacaoNovoClienteModelAdmin(admin.ModelAdmin):
    list_display= [ processo_id,
                    'id',
                    'cliente',
                    'afiliado',
                    'servico',
                    'pedido',
                    'preco',
                    'statusPagamento',
                    'FormaDePagamento',
                    'data_criacao',
                    'data_atualizacao'
                   ]

@admin.register(TransacaoAfiliadoAdministrador)
class TransacaoAfiliadoADMModelAdmin(admin.ModelAdmin):
    list_display= [ 'id',
                    'afiliado',
                    'servico',
                    'pedido',
                    'preco_suporte',
                    'statusPagamento',
                    'FormaDePagamento',
                    'data_criacao',
                    'data_atualizacao',
                    'ObservacoesAdm',
                   ]


from math        import prod
from collections import defaultdict

def preprocessing(input):
    cave = defaultdict(lambda: 9)
    for y, row in enumerate(input.splitlines(), 1):
        for x, n in enumerate(row, 1):
            cave[(x, y)] = int(n)
    return cave

def solver(cave):
    neighbours = {(-1, 0), (0, 1), (1, 0), (0, -1)}
    low_points = {(x, y) for x, y in list(cave) if all(cave[(x, y)] < cave[(x + dx, y + dy)] for dx, dy in neighbours)}
    yield len(low_points) + sum(cave[pos] for pos in low_points)
    yield prod(sorted(basin_size(cave, pos, set()) for pos in low_points)[-3:])

def basin_size(cave, pos, visited):
    x, y = pos
    if cave[(x, y)] == 9 or (x, y) in visited: return 0
    else:
        visited.add(pos)
        return 1 + sum(basin_size(cave, (x + dx, y + dy), visited) for dx, dy in {(-1, 0), (0, 1), (1, 0), (0, -1)})