Ejemplo n.º 1
0
    def random_image_set(x, y, colors, n):
        """
        Creates a image set
        :param x: The width of the image
        :param y: The height of the image
        :param colors: IF the image needs colors
        :param n: The number of images
        :return: A ImageSet
        """

        max_value = 255
        rand_im = np.random.rand(x, y)
        exposure_im = np.zeros((n, x, y), dtype=int)
        if colors:
            rand_im = np.random.rand(x, y, 3)
            exposure_im = np.zeros((n, x, y, 3), dtype=int)

        rand_im = rand_im
        exposures = np.zeros(n)
        for i in range(1, n + 1):
            exposure_im[i - 1] = (rand_im * max_value * 2**(i - 1)).astype(int)
            exposure_im[i - 1][exposure_im[i - 1] > max_value] = max_value
            exposures[i - 1] = 2**(i - 1)

        im_set = ImageSet(exposure_im)
        im_set.shutter_speed = np.log(exposures)
        im_set.original_shape = rand_im.shape

        return rand_im, im_set
Ejemplo n.º 2
0
 def test_compute_spots_peripheral_distance(self):
     image_set = ImageSet(self.repo, path_list=['mrna/arhgdia/2h/'])
     result = image_set.compute_spots_peripheral_distance()
     total_sum=0
     for res in result:
         total_sum += np.sum(res)
     self.assertEqual(total_sum, 20030.0)
Ejemplo n.º 3
0
def compute_stability(gene, bootstrap=500, force2D=True):
    total_mads = []
    imageset = ImageSet(analysis_repo, ["mrna/" + gene + "/"], force2D=force2D)
    spots_peripheral_distances = imageset.compute_spots_peripheral_distance()
    peripheral_profiles = np.zeros((len(spots_peripheral_distances), 10))
    for j in range(len(spots_peripheral_distances)):
        for i in range(0, 10):
            peripheral_profiles[j, i] = float(
                len(
                    np.where((spots_peripheral_distances[j] >= (
                        (i * 10) + 1)) & (spots_peripheral_distances[j] <=
                                          (i + 1) * 10))[0]) /
                float(len(spots_peripheral_distances[j])))
    logger.info(
        "Compute mean_absolute_deviation for randomly selected images in {} dataset",
        gene)
    for j in tqdm.tqdm(range(bootstrap), desc="Simulation"):
        mads = []
        for i in range(1, len(imageset.images) - 1):
            arr = peripheral_profiles[np.random.choice(
                peripheral_profiles.shape[0], i, replace=True)]
            rand_idx = randint(0, peripheral_profiles.shape[0] - 1)
            mean_arr = np.mean(arr, axis=0)
            arr_diff = mean_arr - peripheral_profiles[rand_idx, :]
            mse = helpers.mean_absolute_deviation(arr_diff)
            mads.append(mse)
        total_mads.append(mads)
    return total_mads
Ejemplo n.º 4
0
 def test_compute_normalised_quadrant_densities_protein(self):
     image_set = ImageSet(self.repo, path_list=['protein/arhgdia/2h/'])
     res = image_set.compute_normalised_quadrant_densities()
     mtoc_density = res[res[:, 1] == 1].sum() / len(res[res[:, 1] == 1])
     non_mtoc_density = res[res[:, 1] == 0].sum() / len(res[res[:, 1] == 0])
     self.assertAlmostEqual(mtoc_density, 1.98375276421, places=3)
     self.assertAlmostEqual(non_mtoc_density, 1.0445809579196, places=3)
Ejemplo n.º 5
0
def compute_degree_of_clustering(genes_list, analysis_repo, molecule_type):
    gene2_degree_of_clustering = {}
    gene2median_degree_of_clustering = {}
    gene2error_degree_of_clustering = {}
    gene2confidence_interval = {}
    degrees_of_clustering = []

    for gene in genes_list:
        image_set = ImageSet(analysis_repo,
                             ['{0}/{1}/'.format(molecule_type, gene)])
        d_of_c = np.array(image_set.compute_degree_of_clustering())
        degrees_of_clustering.append(d_of_c)

    for gene, degree_of_clustering in zip(genes_list, degrees_of_clustering):
        degree_of_clustering = np.log(degree_of_clustering)
        gene2_degree_of_clustering[gene] = degree_of_clustering
        gene2median_degree_of_clustering[gene] = np.median(
            degree_of_clustering)
        # Standard error and CI computation
        gene2error_degree_of_clustering[gene] = helpers.sem(
            degree_of_clustering, factor=0)
        lower, higher = helpers.median_confidence_interval(
            degree_of_clustering)
        gene2confidence_interval[gene] = [lower, higher]

    return gene2_degree_of_clustering, gene2median_degree_of_clustering, gene2error_degree_of_clustering, gene2confidence_interval
Ejemplo n.º 6
0
def compute_transcript_by_cell_area(analysis_repo, gene, timepoints):
    transcript_by_cell_area = {"total_transcript": [], "cell_area": []}
    for timepoint in timepoints:
        image_set = ImageSet(analysis_repo, [f"{'mrna'}/{gene}/{timepoint}/"], force2D=False)
        [transcript_by_cell_area["total_transcript"].append(image.compute_cytoplasmic_total_spots()) for image in image_set.get_images()]
        [transcript_by_cell_area["cell_area"].append(image.compute_cell_area()) for image in image_set.get_images()]
    return pd.DataFrame(transcript_by_cell_area)
Ejemplo n.º 7
0
def plot_dynamic_barplot(analysis_repo):
    '''
    Formats the data and calls the plotting function
    '''
    plot_colors = constants.analysis_config['PLOT_COLORS']

    # paired mRNA-protein barplots, so we go through proteins (we have less proteins than mRNA)
    tp_mrna = constants.dataset_config['TIMEPOINTS_MRNA']
    tp_proteins = constants.dataset_config['TIMEPOINTS_PROTEIN']
    all_timepoints = np.sort(list(set(tp_mrna) | set(tp_proteins)))
    for i, gene in enumerate(constants.analysis_config['PROTEINS']):
        df = pd.DataFrame(
            columns=["Molecule", "Timepoint", "d_of_c", "error", "CI"])
        for molecule, timepoints in zip(["mrna", "protein"],
                                        [tp_mrna, tp_proteins]):
            for j, tp in enumerate(all_timepoints):
                if tp not in timepoints:
                    df = df.append(
                        {
                            "Molecule": molecule,
                            "Timepoint": tp,
                            "error": 0,
                            "CI": [0, 0],
                            "d_of_c": 0
                        },
                        ignore_index=True)
                    continue
                image_set = ImageSet(
                    analysis_repo, ["{0}/{1}/{2}/".format(molecule, gene, tp)])
                degree_of_clustering = np.log(
                    image_set.compute_degree_of_clustering(
                    ))  # * factor[gene][molecule][j]
                err = helpers.sem(degree_of_clustering, factor=6)
                lower, higher = helpers.median_confidence_interval(
                    degree_of_clustering)
                df = df.append(
                    {
                        "Molecule": molecule,
                        "Timepoint": tp,
                        "error": err,
                        "CI": [lower, higher],
                        "d_of_c": degree_of_clustering
                    },
                    ignore_index=True)
        df = df.sort_values('Timepoint')
        df = df.groupby('Molecule').apply(mean_column)
        my_pal = {
            "mrna": str(plot_colors[i]),
            "protein": str(color_variant(plot_colors[i], +80))
        }
        tgt_image_name = constants.analysis_config[
            'DYNAMIC_FIGURE_NAME_FORMAT'].format(gene=gene)
        tgt_fp = pathlib.Path(
            constants.analysis_config['FIGURE_OUTPUT_PATH'].format(
                root_dir=global_root_dir), tgt_image_name)
        plot.bar_profile_median_timepoints(df,
                                           palette=my_pal,
                                           figname=tgt_fp,
                                           fixed_yscale=15)
Ejemplo n.º 8
0
 def test_compute_volumes_from_periphery(self):
     image_set = ImageSet(self.repo, path_list=['mrna/arhgdia/3h/'])
     self.assertEqual(len(image_set.images), 5, "Expected 5 images")
     peripheral_volumes = image_set.compute_volumes_from_periphery()
     self.assertEqual(peripheral_volumes.shape, (5, 100))
     cytoplasmic_volumes = [img.compute_cytoplasmic_volume() for img in image_set.images]
     self.assertTrue(np.allclose(peripheral_volumes[:, 99], np.array(cytoplasmic_volumes)))
     self.assertAlmostEqual(peripheral_volumes.sum(), 121874.96804733, places=5)
Ejemplo n.º 9
0
 def test_compute_spots_signal_from_periphery(self):
     image_set = ImageSet(self.repo, path_list=['mrna/arhgdia/2h/'])
     self.assertEqual(len(image_set.images), 5, "Expected 5 images")
     peripheral_signals = image_set.compute_signal_from_periphery()
     self.assertEqual(peripheral_signals.shape, (5, 100))
     spots_counts = [img.compute_cytoplasmic_total_spots() for img in image_set.images]
     self.assertTrue(np.all(peripheral_signals[:, 99] == np.array(spots_counts)))
     self.assertAlmostEqual(peripheral_signals.sum(), 20166.0)
Ejemplo n.º 10
0
 def test_compute_normalised_quadrant_densities_mrna(self):
     image_set = ImageSet(self.repo, path_list=['mrna/arhgdia/2h/'])
     res = image_set.compute_normalised_quadrant_densities()
     self.assertEqual(res.shape[0], 20)
     mtoc_density = res[res[:, 1] == 1].sum() / len(res[res[:, 1] == 1])
     self.assertAlmostEqual(mtoc_density, 2.22758047513, places=5)
     non_mtoc_density = res[res[:, 1] == 0].sum() / len(res[res[:, 1] == 0])
     self.assertAlmostEqual(non_mtoc_density, 1.089256691215, places=5)
Ejemplo n.º 11
0
 def test_compute_intensities_signal_from_periphery(self):
     image_set = ImageSet(self.repo, path_list=['protein/arhgdia/4h/'])
     self.assertEqual(len(image_set.images), 5, "Expected 5 images")
     peripheral_signals = image_set.compute_signal_from_periphery()
     cytoplasmic_intensities = [img.compute_cytoplasmic_total_intensity() for img in image_set.images]
     self.assertEqual(peripheral_signals.shape, (5, 100))
     self.assertTrue(np.allclose(peripheral_signals[:, 99], np.array(cytoplasmic_intensities)))
     self.assertAlmostEqual(peripheral_signals.sum(), 29384734352.0)
Ejemplo n.º 12
0
 def test_compute_areas_from_periphery(self):
     image_set = ImageSet(self.repo, path_list=['mrna/arhgdia/3h/'])
     self.assertEqual(len(image_set.images), 5, "Expected 5 images")
     peripheral_areas = image_set.compute_areas_from_periphery()
     self.assertEqual(peripheral_areas.shape, (5, 100))
     cytoplasmic_areas = [img.compute_cell_area() - img.compute_nucleus_area() for img in image_set.images]
     self.assertTrue(np.allclose(peripheral_areas[:, 99], np.array(cytoplasmic_areas)))
     self.assertAlmostEqual(peripheral_areas.sum(), 142574.96909927676, places=5)
Ejemplo n.º 13
0
def compute_zline_distance(repo, molecule_list, timepoints, z_line_spacing):
    all_median_profiles = []
    for molecule in molecule_list:
        for timepoint in timepoints:
            image_set = ImageSet(repo, [f"{'mrna'}/{molecule}/{timepoint}/"])
            if image_set.__sizeof__() < 5:
                logger.warning("Image set is small for {}", molecule)
            total_profile = image_set.compute_zline_distance(z_line_spacing)
            all_median_profiles.append(np.median(total_profile, axis=0))
    return all_median_profiles
Ejemplo n.º 14
0
    def test_alignment(self):
        """
        Tests if the ImageSet info is correct except the image, as this is a little unpredictable
        """
        rand_im = (np.random.rand(225, 225, 4) * 255).astype(np.uint8)
        rand_im[:, :, -1] = 1
        input_im = np.zeros((4,) + rand_im.shape, dtype=np.uint8)
        for i in range(0, input_im.shape[0]):
            input_im[i] = rand_im

        input_im[1, :-2, :] = input_im[1, 2:, :]
        input_im[1, -3:, :] = 0
        input_im[3] = np.rot90(input_im[3])

        input_im_set = ImageSet(input_im)
        input_im_set.original_shape = input_im.shape[1:]
        input_im_set.shutter_speed = np.array([1, 2, 3, 4])

        expected_im_set = ImageSet(input_im)  # ignoring the returned image here
        expected_im_set.original_shape = (222, 222, 4)
        expected_im_set.shutter_speed = input_im_set.shutter_speed.copy()

        output_image_set = input_im_set.aligned_image_set()

        self.assertEqual(expected_im_set.original_shape, output_image_set.original_shape)
        self.assertTrue(np.array_equal(expected_im_set.shutter_speed, output_image_set.shutter_speed))
Ejemplo n.º 15
0
 def test_compute_cell_mask_between_nucleus_centroids(self):
     image_set = ImageSet(self.repo, path_list=['mrna/actn2/immature/'])
     nuc_dist, nucs_dist, cell_masks, nucs_pos = image_set.compute_cell_mask_between_nucleus_centroids()
     self.assertEqual(np.sum([754, 483, 526]), np.sum(nuc_dist))
     self.assertEqual(len([754]) + len([483, 526]), len(nucs_dist[0]) + len(nucs_dist[1]))
     for nuc_pos in nucs_pos:
         if len(nuc_pos) == 1:
             self.assertEqual(nuc_pos[0],[110, 864])
         else:
             self.assertEqual(nuc_pos[0], [154, 637])
             self.assertEqual(nuc_pos[1], [637, 1163])
Ejemplo n.º 16
0
 def test_compute_zline_distance(self):
     image_set = ImageSet(self.repo, path_list=['mrna/actn2/immature/'])
     result = image_set.compute_zline_distance(20)
     test = [[0.24374599, 0.03463759, 0.0365619, 0.0436177, 0.03207184,
              0.02758178, 0.0365619, 0.03207184, 0.03014753, 0.02694035,
              0.02309173, 0.01860167, 0.02180885, 0.02758178, 0.01988454,
              0.0, 0.0, 0.0, 0.0, 0.0],
             [0.38974359, 0.01230769, 0.01025641, 0.02871795, 0.02153846,
              0.02461538, 0.01948718, 0.02666667, 0.03076923, 0.01333333,
              0.0225641, 0.01025641, 0.01538462, 0.01333333, 0.00923077,
              0.0, 0.0, 0.0, 0.0, 0.0]]
     self.assertEqual(len(result), len(test))
     self.assertAlmostEqual(np.sum(result), np.sum(test), places=5)
Ejemplo n.º 17
0
 def test_gray_images(self):
     """
     Tests ImageSet.gray_images()
     """
     image = np.array([[[  # One image with shape (1, 3, 3)
         [1, 2, 3], [2, 3, 4], [100, 100, 100]
     ]]])
     image_set = ImageSet(image)
     image_set.original_shape = (1, 3, 3)
     image_set.shutter_speed = [1]
     expected_image = np.array([[  # Shape (1, 3)
         2, 3, 100
     ]]).astype(float)
     output = image_set.gray_images()
     self.assertTrue(np.array_equal(output.images[0], expected_image))
Ejemplo n.º 18
0
    def test_find_reference_points_for(self):
        """
        Tests the find_reference_points_for(...)
        """
        gray_input = ImageSet(np.zeros(
            (10, 10, 3)))  # 10 images with shape (10, 3)
        gray_expected = np.arange(0, 30)
        gray_output = hdr.find_reference_points_for(gray_input)
        self.assertTrue(np.array_equal(gray_output, gray_expected))

        color_input = ImageSet(np.zeros(
            (10, 1000, 300, 3)))  # 10 images with shape (1000, 300, 3)
        color_expected = np.arange(0, 300000, 300)
        color_output = hdr.find_reference_points_for(color_input)
        self.assertTrue(np.array_equal(color_output, color_expected))
Ejemplo n.º 19
0
def main():
    image_set = ImageSet.load(sys.argv[1], 240, 320)
    image_set.split(train_rate=0.9, seed="numazu_shine")

    model = resnet.ResnetBuilder.build_resnet_18(
        (image_set.color, image_set.height, image_set.width),
        image_set.num_classes)
    model.summary()
    bot = LearningBot(model)

    history = {}
    for image_data in image_set.get_iter_for_learning_curve(5):
        size = len(image_data[0])

        datagen = image.ImageDataGenerator(zca_whitening=True,
                                           rotation_range=10,
                                           width_shift_range=0.2,
                                           height_shift_range=0.2,
                                           shear_range=0.5,
                                           zoom_range=0.3,
                                           channel_shift_range=0.,
                                           horizontal_flip=True)
        datagen.fit(image_data[0])

        history[size] = bot.learn_by_generator(*image_data,
                                               datagen,
                                               batch_size=128,
                                               steps_per_epoch=size / 128)

    bot.draw_history_list('test.png',
                          history, ["acc", "val_acc"],
                          method=DrawMethod.best)
Ejemplo n.º 20
0
 def test_can_build_mono_type_if(self):
     image_set = ImageSet(self.repo, path_list=['protein/arhgdia/3h/'])
     self.assertEqual(len(image_set.images), 5, "Expected 5 images")
     for image in image_set.images:
         self.assertTrue(type(image) == Image3dWithIntensitiesAndMTOC)
         with self.assertRaises(AttributeError):  # 'ImageWithIntensities' object has no attribute 'get_spots'
             self.assertEqual(image.get_spots().shape[1], 3)
         self.assertEqual(image.get_intensities().shape, (512, 512))
Ejemplo n.º 21
0
def intensities_cytoplasmic_total_count(analysis_repo, keyorder):
    gene2cyto_count = {}
    gene2median_cyto_count = {}
    gene2error = {}
    gene2confidence_interval = {}
    for gene in constants.analysis_config['PROTEINS']:
        logger.info("Running protein cytoplasmic total count analysis for {}", gene)
        imageset = ImageSet(analysis_repo, ['protein/%s/' % gene])
        gene2cyto_count[gene] = imageset.compute_cytoplasmic_intensities()
        gene2median_cyto_count[gene] = np.median(gene2cyto_count[gene])
        gene2error[gene] = helpers.sem(gene2cyto_count[gene], factor=0)
        lower, higher = helpers.median_confidence_interval(gene2cyto_count[gene])
        gene2confidence_interval[gene] = [lower, higher]

    # generate bar plot image

    gene2median_cyto_count = collections.OrderedDict(sorted(gene2median_cyto_count.items(), key=lambda i: keyorder.index(i[0])))
    gene2error = collections.OrderedDict(sorted(gene2error.items(), key=lambda i: keyorder.index(i[0])))
    gene2confidence_interval = collections.OrderedDict(sorted(gene2confidence_interval.items(), key=lambda i: keyorder.index(i[0])))
    xlabels = constants.analysis_config['PROTEINS_LABEL']

    tgt_image_name = constants.analysis_config['FIGURE_NAME_FORMAT'].format(molecule_type="protein")
    tgt_fp = pathlib.Path(constants.analysis_config['FIGURE_OUTPUT_PATH'].format(root_dir=global_root_dir),
                          tgt_image_name)
    plot.bar_profile_median(gene2median_cyto_count,
                            gene2error.values(),
                            'proteins',
                            xlabels,
                            tgt_fp,
                            gene2confidence_interval,
                            annot=False,
                            data_to_annot=gene2cyto_count
                            )

    # generate violin plot image
    tgt_image_name = constants.analysis_config['FIGURE_NAME_VIOLIN_FORMAT'].format(molecule_type="protein")
    tgt_fp = pathlib.Path(constants.analysis_config['FIGURE_OUTPUT_PATH'].format(root_dir=global_root_dir),
                          tgt_image_name)

    plot.violin_profile(gene2cyto_count, tgt_fp, xlabels, rotation=0, annot=True)
Ejemplo n.º 22
0
 def __init__(self):
     super().__init__()
     self.title = 'Stimat'
     self.main_image = PlotCanvas(width=5, height=4)
     self.original_image_set = ImageSet([])
     self.edited_image = None
     self.hdr_image = None
     self.filter_widgets = list()
     self.filter_layout = QVBoxLayout()
     self.add_global_filter_button = QPushButton("Legg til globalt filter",
                                                 self)
     self.add_lum_filter_button = QPushButton("Legg til luminans filter",
                                              self)
     self.add_gaussian_button = QPushButton("Legg til gaussian filter",
                                            self)
     self.add_bilateral_button = QPushButton("Legg til bilateral filter",
                                             self)
     self.add_gradient_compression_button = QPushButton(
         "Legg til gradient comp. filter", self)
     self.save_image_button = QPushButton("Lagre bilde", self)
     self.align_image_button = QPushButton("Opplinjer Bildesett", self)
     self.status_label = QLabel("Ingen bilder er lastet inn")
     self.init_ui()
Ejemplo n.º 23
0
def compute_relative_densities(analysis_repo,
                               molecule_type,
                               quadrants_num=4,
                               peripheral_flag=False):
    densities = {}
    stripes = constants.analysis_config['STRIPE_NUM']
    if peripheral_flag:
        stripes_flag = False
        stripes = 1
    else:
        stripes_flag = True

    if molecule_type == 'mrna':
        timepoints = constants.dataset_config['TIMEPOINTS_MRNA']
    else:
        timepoints = constants.dataset_config['TIMEPOINTS_PROTEIN']

    for gene in constants.analysis_config['PROTEINS']:
        median_densities, gene_clusters = [], []
        for timepoint in timepoints:
            image_set = ImageSet(
                analysis_repo,
                [molecule_type + "/{0}/{1}/".format(gene, timepoint)])
            arr = image_set.compute_normalised_quadrant_densities(
                quadrants_num=quadrants_num,
                peripheral_flag=peripheral_flag,
                stripes=stripes,
                stripes_flag=stripes_flag)
            num_images = arr.shape[0] // (quadrants_num * stripes)
            aligned_densities = arr[:, 0].reshape(num_images,
                                                  quadrants_num * stripes)
            median_densities_per_slice = np.nanmedian(aligned_densities,
                                                      axis=0)
            median_densities.append(median_densities_per_slice)
        densities[gene] = median_densities

    return densities
Ejemplo n.º 24
0
def compute_density_per_quadrant(analysis_repo,
                                 molecule_type,
                                 groupby_key,
                                 quadrants_num,
                                 quadrant_labels,
                                 molecule_list,
                                 time_points,
                                 mpi_sample_size,
                                 peripheral_flag=False):
    assert len(quadrant_labels) == quadrants_num - 1
    density_per_quadrant = []
    for molecule in molecule_list:
        for timepoint in time_points:
            density_statictics = {}
            image_set = ImageSet(analysis_repo,
                                 [f"{molecule_type}/{molecule}/{timepoint}/"])
            if len(image_set.images) < 5:
                logger.warning("Image set is small for {}", molecule)
            res = image_set.compute_normalised_quadrant_densities(
                quadrants_num=quadrants_num, peripheral_flag=peripheral_flag)
            mtoc_quadrants = res[res[:, 1] == 1][:, 0]
            num_images = res.shape[0] // quadrants_num
            non_mtoc_quadrants = res[res[:, 1] == 0][:, 0].reshape(
                num_images, quadrants_num - 1)
            density_statictics["Gene"] = [molecule] * num_images
            density_statictics["Timepoint"] = [timepoint] * num_images
            density_statictics["MTOC"] = mtoc_quadrants
            for i in range(0, quadrants_num - 1):
                density_statictics["Non MTOC" + str(i)] = non_mtoc_quadrants[:,
                                                                             i]
            density_per_quadrant.append(pd.DataFrame(density_statictics))

    return DensityStats(df=pd.concat(density_per_quadrant),
                        group_key=groupby_key,
                        mpi_sample_size=mpi_sample_size,
                        quadrant_labels=quadrant_labels,
                        mtoc_quadrant_label='MTOC')
Ejemplo n.º 25
0
    def select_file(self):
        """
        Selects a set of files and generates a HDR image
        """
        file_name, ok = QFileDialog.getOpenFileNames(
            self, "Velg bilde", "", "PNG (*.png);;EXR (*.exr)")

        if ok:
            self.add_global_filter_button.setEnabled(True)
            self.add_lum_filter_button.setEnabled(True)
            self.add_gaussian_button.setEnabled(True)
            self.add_bilateral_button.setEnabled(True)
            self.add_gradient_compression_button.setEnabled(True)
            self.save_image_button.setEnabled(True)

            try:
                if file_name[0].endswith(".exr"):
                    self.original_image_set = None
                    self.hdr_image = read_image(file_name[0])
                    self.align_image_button.setEnabled(False)
                else:
                    image_info = list(
                        map(
                            lambda file: (file, file.rsplit("_", 1)[-1].
                                          replace(".png", "")), file_name))
                    self.original_image_set = ImageSet(image_info)
                    self.hdr_image = self.original_image_set.hdr_image(10)
                    self.align_image_button.setEnabled(True)

                self.update_image_with_filter()
                self.status_label.setText("Bilde ble lastet inn")
                self.status_label.setStyleSheet("background: green")
            except:
                self.status_label.setText(
                    "Ups! Det skjedde en feil ved innlasting av bildet")
                self.status_label.setStyleSheet("background: red")
Ejemplo n.º 26
0
    def test_compute_cytoplasmic_spots_fractions_per_periphery(self):
        image_set = ImageSet(self.repo, path_list=['mrna/arhgdia/3h/'])
        peripheral_fractions = image_set.compute_cytoplasmic_spots_fractions_per_periphery()
        self.assertEqual(peripheral_fractions.shape, (5, 100))
        self.assertTrue(np.all(peripheral_fractions[:, 99] == 1))
        self.assertAlmostEqual(peripheral_fractions.sum(), 719.995179047, places=5)

        image_set = ImageSet(self.repo, path_list=['mrna/arhgdia/4h/'], force2D=True)
        peripheral_fractions = image_set.compute_cytoplasmic_spots_fractions_per_periphery(force2D=True)
        self.assertEqual(peripheral_fractions.shape, (5, 100))
        self.assertTrue(np.all(peripheral_fractions[:, 99] == 1))
Ejemplo n.º 27
0
def build_cytoplasmic_statistics(analysis_repo, statistics_type, molecule_type,
                                 genes, keyorder):
    gene2stat, gene2median, gene2error, gene2confidence_interval = {}, {}, {}, {}

    for gene in genes:
        logger.info("Running {} cytoplasmic {} analysis for {}", molecule_type,
                    statistics_type, gene)
        image_set = ImageSet(analysis_repo,
                             ['{0}/{1}/'.format(molecule_type, gene)])
        if statistics_type == 'centrality':
            if molecule_type == 'mrna':
                gene2stat[
                    gene] = image_set.compute_cytoplasmic_spots_centrality()
            else:
                gene2stat[
                    gene] = image_set.compute_cytoplasmic_intensities_centrality(
                    )
        if statistics_type == 'spread':
            if molecule_type == 'mrna':
                gene2stat[gene] = image_set.compute_cytoplasmic_spots_spread()
            else:
                gene2stat[
                    gene] = image_set.compute_intensities_cytoplasmic_spread()
        if statistics_type == 'centrality':
            gene2median[gene] = np.mean(gene2stat[gene])
            gene2error[gene] = helpers.sem(gene2stat[gene], factor=0)
            lower, higher = helpers.median_confidence_interval(gene2stat[gene])
            gene2confidence_interval[gene] = [lower, higher]

    if statistics_type == 'spread':
        max_entropy = np.max([np.max(gene2stat[k]) for k in gene2stat.keys()])
        for gene in gene2stat.keys():
            gene2stat[gene] = gene2stat[gene] / max_entropy
            gene2median[gene] = np.median(gene2stat[gene])
            gene2error[gene] = helpers.sem(gene2stat[gene], factor=0)
            lower, higher = helpers.median_confidence_interval(gene2stat[gene])
            gene2confidence_interval[gene] = [lower, higher]

    gene2stat = collections.OrderedDict(
        sorted(gene2stat.items(), key=lambda i: keyorder.index(i[0])))
    gene2median = collections.OrderedDict(
        sorted(gene2median.items(), key=lambda i: keyorder.index(i[0])))
    gene2error = collections.OrderedDict(
        sorted(gene2error.items(), key=lambda i: keyorder.index(i[0])))
    gene2confidence_interval = collections.OrderedDict(
        sorted(gene2confidence_interval.items(),
               key=lambda i: keyorder.index(i[0])))
    return gene2median, gene2stat, gene2error, gene2confidence_interval
Ejemplo n.º 28
0
 def test_compute_normalized_quadrant_densities_mrna(self):
     image_set = ImageSet(self.repo, path_list=['mrna/arhgdia/2h/'])
     result1 = image_set.compute_normalised_quadrant_densities(quadrants_num=8)
     num_images = image_set.__sizeof__()
     self.assertEqual(num_images, image_set.__sizeof__())
     self.assertAlmostEqual(result1[result1[:, 1] == 1][:, 0].sum() / num_images,
                            1.0780173273, places=5)  # MTOC quadrant density
     self.assertAlmostEqual(result1[result1[:, 1] == 0][:, 0].sum() / (num_images * 7),
                            1.1263246459, places=5)  # non MTOC quadrant density
     result2 = image_set.compute_normalised_quadrant_densities(quadrants_num=8, stripes=3, stripes_flag=True)
     self.assertAlmostEqual(result2[result2[:, 1] == 1][:, 0].sum() / (3 * num_images),
                            1.0225376792, places=5)  # MTOC quadrant density
     self.assertAlmostEqual(result2[result2[:, 1] == 0][:, 0].sum() / (num_images * 7 * 3),
                            1.08160649427, places=5)  # non MTOC quadrant density
     result3 = image_set.compute_normalised_quadrant_densities(quadrants_num=8, peripheral_flag=True,
                                                               stripes=3, stripes_flag=True)
     self.assertAlmostEqual(result3[result3[:, 1] == 1][:, 0].sum() / (3 * num_images),
                            0.79154809935, places=5)  # MTOC quadrant density
     self.assertAlmostEqual(result3[result3[:, 1] == 0][:, 0].sum() / (num_images * 7 * 3),
                            0.59408432134, places=5)  # non MTOC quadrant density
Ejemplo n.º 29
0
 def test_compute_normalized_quadrant_densities_protein(self):
     image_set = ImageSet(self.repo, path_list=['protein/arhgdia/3h/'])
     result1 = image_set.compute_normalised_quadrant_densities(quadrants_num=8)
     num_images = image_set.__sizeof__()
     self.assertEqual(num_images, image_set.__sizeof__())
     self.assertAlmostEqual(result1[result1[:, 1] == 1][:, 0].sum() / num_images,
                            1.098770606, places=5)  # MTOC quadrant density
     self.assertAlmostEqual(result1[result1[:, 1] == 0][:, 0].sum() / (num_images * 7),
                            0.865466580, places=5)  # non MTOC quadtant density
     result2 = image_set.compute_normalised_quadrant_densities(quadrants_num=8, stripes=3, stripes_flag=True)
     self.assertAlmostEqual(result2[result2[:, 1] == 1][:, 0].sum() / (3 * num_images),
                            0.9803183396, places=5)  # MTOC quadrant density
     self.assertAlmostEqual(result2[result2[:, 1] == 0][:, 0].sum() / (num_images * 7 * 3),
                            0.6801687989, places=5)  # non MTOC quadtant density
     result3 = image_set.compute_normalised_quadrant_densities(quadrants_num=8, peripheral_flag=True,
                                                               stripes=3, stripes_flag=True)
     self.assertAlmostEqual(result3[result3[:, 1] == 1][:, 0].sum() / (3 * num_images),
                            0.126123087, places=5)  # MTOC quadrant density
     self.assertAlmostEqual(result3[result3[:, 1] == 0][:, 0].sum() / (num_images * 7 * 3),
                            0.011356787, places=5)  # non MTOC quadtant density
Ejemplo n.º 30
0
def compute_nuclei_area(gene, analysis_repo, gene_label, timepoint):
    dict_nucleus_area = {"Gene": [], "value": []}
    image_set = ImageSet(analysis_repo, [f"{'mrna'}/{gene}/{timepoint}/"])
    [dict_nucleus_area["value"].append(image.compute_nucleus_area()) for image in image_set.get_images()]
    [dict_nucleus_area["Gene"].append(gene_label) for image in image_set.get_images()]
    return pd.DataFrame(dict_nucleus_area)