def populate_region_selection_tab(self):
        self.region_figures = matts_admin_functions.create_empty_list(number_of_planes)
        self.region_canvases = matts_admin_functions.create_empty_list(number_of_planes)
        self.region_widgets = matts_admin_functions.create_empty_list(number_of_planes)
        self.roi_displays = matts_admin_functions.create_empty_list(number_of_planes)
        global region_combo_boxes

        for plane in range(number_of_planes):
            figure, canvas, widget = create_canvas_widget(400, 400)
            self.region_figures[plane] = figure
            self.region_canvases[plane] = canvas
            self.region_widgets[plane] = widget

            self.roi_displays[plane] = QGraphicsView()
            self.roi_displays[plane].setMinimumWidth(500)
            region_combo_boxes[plane] = QComboBox()

            region_combo_boxes[plane].addItem("Forebrain")
            region_combo_boxes[plane].addItem("Tectum")
            region_combo_boxes[plane].addItem("Midbrain")
            region_combo_boxes[plane].addItem("Hindbrain")

            combo_box_function = self.create_region_combo_box_function(plane)
            region_combo_boxes[plane].currentIndexChanged.connect(combo_box_function)

            self.region_selection_scroll_area_contents_layout.addWidget(region_combo_boxes[plane], plane, 0)
            self.region_selection_scroll_area_contents_layout.addWidget(self.region_widgets[plane], plane, 1)
            self.region_selection_scroll_area_contents_layout.addWidget(self.roi_displays[plane], plane, 2)
def draw_roi_scene(plane):
    scene = QGraphicsScene()
    scale_factor = 3
    #scene.setSceneRect(0,0, tiff_dimensions[0], tiff_dimensions[1])

    roi_size = 10
    roi_elipses = matts_admin_functions.create_empty_list(
        len(neuron_coordinates[plane]))

    y_offset = -50
    x_offset = -50

    for roi_index in range(len(neuron_coordinates[plane])):

        x_cord = neuron_coordinates[plane][roi_index][1]
        y_cord = neuron_coordinates[plane][roi_index][0]

        probability = cell_probabilities[plane][roi_index]
        colour = get_colour(probability, "hot", 0.8)

        roi_elipses[roi_index] = QGraphicsEllipseItem(
            (x_cord + x_offset), (y_cord + y_offset), roi_size, roi_size)
        roi_elipses[roi_index].setBrush(QBrush(colour))
        roi_elipses[roi_index].setPos(x_cord / scale_factor,
                                      y_cord / scale_factor)

        if neuron_decisions[plane][roi_index] == False:
            roi_elipses[roi_index].setOpacity(0.2)

        scene.addItem(roi_elipses[roi_index])

    main.roi_displays[plane].setScene(scene)
def create_lasso_functions():

    plane_functions = matts_admin_functions.create_empty_list(number_of_planes)

    for plane in range(number_of_planes):
        plane_functions[plane] = create_plane_function(plane)

    return plane_functions
def create_lassos():
    lassos = matts_admin_functions.create_empty_list(number_of_planes)
    lasso_functions = create_lasso_functions()

    for plane in range(number_of_planes):
        axis = main.region_figures[plane].get_axes()
        lassos[plane] = LassoSelector(axis[0], lasso_functions[plane])

    return lassos
def load_cell_probabilities():
    global cell_probabilities
    cell_probabilities = matts_admin_functions.create_empty_list(number_of_planes)

    for plane in range(number_of_planes):
        is_cell_file_location = str(folder_location + str(r"\plane"+str(plane)) +"\iscell.npy")
        is_cell = np.load(is_cell_file_location)

        for cell in range(len(is_cell)):
            cell_probabilities[plane].append(is_cell[cell][1])
def bcl_function(trace):

    # BCL Parameters
    wdt = 0.1
    lamb = 0.6
    varB = 2e-2
    varC = 2
    initial_baseline = 1
    Cmean = 0
    frequency = 2.7

    #Preprocess Function
    t1 = trace / np.mean(trace)
    y = lowpass_filter(t1, 500)  #2000 too high, #200 too low
    difft = diff(y)
    varX = get_variance_of_the_decreases(difft)

    #Declare Variables
    N = len(y)
    B = matts_admin_functions.create_empty_list(N)
    c = matts_admin_functions.create_list_of_zeros(N)
    sks = matts_admin_functions.create_list_of_zeros(N)
    B[0] = initial_baseline
    loglik = 0
    dt = float(1) / frequency

    for t in range(1, N):
        cnew = c[t - 1] * np.exp(-lamb * dt)
        Bnew = (varX * B[t - 1] + varB * dt *
                (y[t] - cnew)) / (varX + varB * dt)

        logp0 = log(1 -
                    wdt) - 0.5 * log(2 * pi) - 0.5 * log(varX + varB * dt) - (
                        y[t] - cnew - B[t - 1])**2 / (2 * varX + 2 * varB * dt)

        cspike = Cmean + cnew + (y[t] - cnew - B[t - 1]) / (
            1 + varB * dt / varC + varX / varC)
        Bspike = B[t - 1] + varB * dt / varC * (cspike - cnew - Cmean)

        logp1 = log(wdt) - 0.5 * log(2 * pi) - 0.5 * log(
            varX + varB * dt + varC) - (y[t] - cnew - B[t - 1] - Cmean)**2 / (
                2 * varX + 2 * varB * dt + 2 * varC)

        if logp1 < logp0:
            c[t] = cnew
            B[t] = Bnew
            loglik = loglik + logp0

        else:
            c[t] = cspike
            B[t] = Bspike
            sks[t] = 1
            loglik = loglik + logp1

    return c, B, sks
def load_flourescence_traces():
    global flourescence_traces
    global number_of_timepoints
    flourescence_traces = matts_admin_functions.create_empty_list(number_of_planes)

    for plane in range(number_of_planes):
        trace_file_location = str(folder_location + str(r"\plane"+str(plane)) + "\F.npy")
        traces = np.load(trace_file_location)
        flourescence_traces[plane] = traces

    number_of_timepoints = len(flourescence_traces[plane][0])
    def create_region_selection_tab(self):
        self.region_selection_tab = QWidget()
        self.region_selection_tab_layout = QGridLayout()
        self.region_selection_tab.setLayout(self.region_selection_tab_layout)

        #Input Directory Button
        self.input_directory_button = QPushButton("Load Suite2p Data")
        self.input_directory_button.clicked.connect(load_data)

        #Number of Planes Input
        self.number_of_planes_spinner = QSpinBox()
        self.number_of_planes_spinner.setValue(number_of_planes)
        self.number_of_planes_label = QLabel("Number Of Planes: ")
        self.number_of_planes_spinner.valueChanged.connect(
            self.set_number_of_planes)

        #Cell Probability Q-Slider
        self.probabiltiy_slider = QSlider(Qt.Horizontal)
        self.probabiltiy_slider.valueChanged.connect(
            self.threshold_slider_value_changed)
        self.probabiltiy_slider.setValue(cell_probability_threshold * 100)
        self.probability_threshold_button = QPushButton(
            "Threshold Cell Probability")
        self.probability_threshold_button.clicked.connect(
            threshold_cell_probability)
        self.probabiltiy_slider_label = QLabel("Cell Probability Threshold")
        self.probabiltiy_slider_value_label = QLabel(
            str(cell_probability_threshold))

        #Save Traces Button
        self.save_traces_button = QPushButton("Save Traces")
        self.save_traces_button.clicked.connect(save_traces)

        #Extract Spikes Button
        self.extract_spikes_button = QPushButton("Extract Spikes")
        self.extract_spikes_button.clicked.connect(extract_spikes)

        #Number of Neurons Button and Label
        self.number_of_neurons_button = QPushButton(
            "Calculate Number of Remaining Neurons")
        self.number_of_neurons_button.clicked.connect(
            calculate_number_of_cells)
        self.number_of_neurons_label = QLabel()

        #Create Scroll Area
        self.region_selection_scroll_area_contents = QWidget()
        self.region_selection_scroll_area_contents_layout = QGridLayout()
        self.region_selection_scroll_area_contents.setLayout(
            self.region_selection_scroll_area_contents_layout)

        self.region_selection_scroll_area = QScrollArea()
        self.region_selection_scroll_area.setWidget(
            self.region_selection_scroll_area_contents)
        self.region_selection_scroll_area.setWidgetResizable(True)

        self.region_figures = matts_admin_functions.create_empty_list(
            number_of_planes)
        self.region_canvases = matts_admin_functions.create_empty_list(
            number_of_planes)
        self.region_widgets = matts_admin_functions.create_empty_list(
            number_of_planes)
        self.roi_displays = matts_admin_functions.create_empty_list(
            number_of_planes)
        self.region_combo_boxes = matts_admin_functions.create_empty_list(
            number_of_planes)

        for plane in range(number_of_planes):
            figure, canvas, widget = create_canvas_widget(400, 400)
            self.region_figures[plane] = figure
            self.region_canvases[plane] = canvas
            self.region_widgets[plane] = widget

            self.roi_displays[plane] = QGraphicsView()
            self.roi_displays[plane].setMinimumWidth(500)
            self.region_combo_boxes[plane] = QComboBox()

            self.region_combo_boxes[plane].addItem("Forebrain")
            self.region_combo_boxes[plane].addItem("Tectum")
            self.region_combo_boxes[plane].addItem("Midbrain")
            self.region_combo_boxes[plane].addItem("Hindbrain")

            self.region_selection_scroll_area_contents_layout.addWidget(
                self.region_combo_boxes[plane], plane, 0)
            self.region_selection_scroll_area_contents_layout.addWidget(
                self.region_widgets[plane], plane, 1)
            self.region_selection_scroll_area_contents_layout.addWidget(
                self.roi_displays[plane], plane, 2)

        self.region_selection_tab_layout.setColumnMinimumWidth(0, 1000)
        self.region_selection_tab_layout.addWidget(self.input_directory_button,
                                                   0, 1, 1, 4)
        self.region_selection_tab_layout.addWidget(
            self.number_of_planes_spinner, 1, 2, 1, 1)
        self.region_selection_tab_layout.addWidget(self.number_of_planes_label,
                                                   1, 1, 1, 1)
        self.region_selection_tab_layout.addWidget(
            self.region_selection_scroll_area, 0, 0, 20, 1)
        self.region_selection_tab_layout.addWidget(
            self.probabiltiy_slider_label, 2, 1, 1, 1)
        self.region_selection_tab_layout.addWidget(self.probabiltiy_slider, 2,
                                                   2, 1, 2)
        self.region_selection_tab_layout.addWidget(
            self.probabiltiy_slider_value_label, 2, 4, 1, 1)
        self.region_selection_tab_layout.addWidget(
            self.probability_threshold_button, 3, 1, 1, 4)
        self.region_selection_tab_layout.addWidget(self.save_traces_button, 4,
                                                   1, 1, 4)
        self.region_selection_tab_layout.addWidget(self.extract_spikes_button,
                                                   5, 1, 1, 4)
        self.region_selection_tab_layout.addWidget(
            self.number_of_neurons_button, 6, 1, 1, 1)
        self.region_selection_tab_layout.addWidget(
            self.number_of_neurons_label, 6, 3, 1, 2)

        self.tabs.addTab(self.region_selection_tab, "Region Selection")
def load_rois():
    global neuron_coordinates
    global neuron_decisions
    global within_region_decisions
    global cell_probability_decisions
    global cell_noises
    global cell_noise_decisions
    global region_assignments
    global full_neuron_regions
    global spike_matricies
    global cell_trace_figures
    global position_indicator_axes
    global cell_trace_canvases

    neuron_coordinates = matts_admin_functions.create_empty_list(
        number_of_planes)
    neuron_decisions = matts_admin_functions.create_empty_list(
        number_of_planes)
    within_region_decisions = matts_admin_functions.create_empty_list(
        number_of_planes)
    cell_probability_decisions = matts_admin_functions.create_empty_list(
        number_of_planes)
    cell_noises = matts_admin_functions.create_empty_list(number_of_planes)
    cell_noise_decisions = matts_admin_functions.create_empty_list(
        number_of_planes)
    region_assignments = matts_admin_functions.create_empty_list(
        number_of_planes)
    spike_matricies = matts_admin_functions.create_empty_list(number_of_planes)
    cell_trace_figures = matts_admin_functions.create_empty_list(
        number_of_planes)
    cell_trace_canvases = matts_admin_functions.create_empty_list(
        number_of_planes)
    position_indicator_axes = matts_admin_functions.create_empty_list(
        number_of_planes)

    for plane in range(number_of_planes):
        stats_file_location = str(folder_location +
                                  str(r"\plane" + str(plane)) + "\stat.npy")
        stats = np.load(stats_file_location)

        for neuron in range(len(stats)):
            neuron_coordinates[plane].append(stats[neuron]["med"])
            neuron_decisions[plane].append(True)
            within_region_decisions[plane].append(True)
            cell_probability_decisions[plane].append(True)
            cell_noises[plane].append(0)
            cell_noise_decisions[plane].append(True)
            region_assignments[plane].append("N/A")
            spike_matricies[plane].append([])
            cell_trace_figures[plane].append([])
            position_indicator_axes[plane].append([])
            cell_trace_canvases[plane].append([])

    populate_trace_combobox()
def setup_variables():
    global neuron_coordinates
    global neuron_decisions
    global within_region_decisions
    global cell_probability_decisions
    global cell_noises
    global cell_noise_decisions
    global region_assignments
    global full_neuron_regions
    global spike_matricies
    global cell_trace_figures
    global position_indicator_axes
    global cell_trace_canvases
    global calcium_traces
    global baseline_traces
    global region_combo_boxes
    global currently_selected_region
    global neuron_locations
    global fourier_traces

    neuron_coordinates          = matts_admin_functions.create_empty_list(number_of_planes)
    neuron_decisions            = matts_admin_functions.create_empty_list(number_of_planes)
    within_region_decisions     = matts_admin_functions.create_empty_list(number_of_planes)
    cell_probability_decisions  = matts_admin_functions.create_empty_list(number_of_planes)
    cell_noises                 = matts_admin_functions.create_empty_list(number_of_planes)
    cell_noise_decisions        = matts_admin_functions.create_empty_list(number_of_planes)
    region_assignments          = matts_admin_functions.create_empty_list(number_of_planes)
    spike_matricies             = matts_admin_functions.create_empty_list(number_of_planes)
    cell_trace_figures          = matts_admin_functions.create_empty_list(number_of_planes)
    cell_trace_canvases         = matts_admin_functions.create_empty_list(number_of_planes)
    position_indicator_axes     = matts_admin_functions.create_empty_list(number_of_planes)
    calcium_traces              = matts_admin_functions.create_empty_list(number_of_planes)
    baseline_traces             = matts_admin_functions.create_empty_list(number_of_planes)
    region_combo_boxes          = matts_admin_functions.create_empty_list(number_of_planes)
    neuron_locations            = matts_admin_functions.create_empty_list(number_of_planes)
    fourier_traces              = matts_admin_functions.create_empty_list(number_of_planes)
    currently_selected_region   = matts_admin_functions.create_list_of_item(number_of_planes,"Forebrain")


    print "Number of neurons", number_of_neurons

    for plane in range(number_of_planes):
        for neuron in range(number_of_neurons[plane]):
            neuron_decisions            [plane].append(True)
            within_region_decisions     [plane].append(False) #change back later
            cell_probability_decisions  [plane].append(True)
            cell_noises                 [plane].append(0)
            cell_noise_decisions        [plane].append(True)
            region_assignments          [plane].append("N/A")
            spike_matricies             [plane].append([])
            cell_trace_figures          [plane].append([])
            position_indicator_axes     [plane].append([])
            cell_trace_canvases         [plane].append([])
            calcium_traces              [plane].append([])
            baseline_traces             [plane].append([])
            fourier_traces              [plane].append([])
            neuron_locations            [plane].append(0)

    populate_trace_combobox()