Esempio n. 1
0
 def _read_component_image(self, filename):
     stack = read_tiff_stack(filename)
     channels = []
     for raw in stack:
         meta = raw['raw_meta']
         image_type, image_description = self._parse_image_description(
             meta['ImageDescription'])
         #image_type, image_description = self._parse_image_description(meta['ImageDescription'])
         if 'ImageType' not in image_description: continue
         if image_description['ImageType'] == 'ReducedResolution': continue
         if 'Name' not in image_description: continue
         channel_label = image_description['Name']
         image_id = uuid4().hex
         if self._storage_type is not None:
             self._images[image_id] = raw['raw_image'].astype(
                 self._storage_type)
         else:
             self._images[image_id] = raw['raw_image']
         channels.append((channel_label, image_id))
     df = pd.DataFrame(channels, columns=['channel_label', 'image_id'])
     temp = self.get_data('measurement_channels').drop(
         columns=['image_id']).reset_index().merge(df,
                                                   on='channel_label',
                                                   how='left')
     self.set_data('measurement_channels', temp.set_index('channel_index'))
     return
Esempio n. 2
0
 def _read_seg_images(self, memb_seg_image_file, nuc_seg_image_file):
     segmentation_names = []
     memb = make_binary_image_array(
         read_tiff_stack(memb_seg_image_file)[0]['raw_image'])
     memb_id = uuid4().hex
     self._images[memb_id] = memb.astype(int)
     segmentation_names.append(['Membrane', memb_id])
     nuc = read_tiff_stack(nuc_seg_image_file)[0]['raw_image']
     nuc_id = uuid4().hex
     self._images[nuc_id] = nuc.astype(int)
     segmentation_names.append(['Nucleus', nuc_id])
     #_mask_key = pd.DataFrame(mask_names,columns=['mask_label','image_id'])
     #_mask_key.index.name = 'db_id'
     #self.set_data('mask_images',_mask_key)
     _segmentation_key = pd.DataFrame(
         segmentation_names, columns=['segmentation_label', 'image_id'])
     _segmentation_key.index.name = 'db_id'
     self.set_data('segmentation_images', _segmentation_key)
Esempio n. 3
0
    def set_line_area(self, line_image, area_image, steps=20, verbose=False):
        drawn_binary = np.zeros(self.shape)
        if os.path.exists(line_image):
            drawn_binary = read_tiff_stack(line_image)[0]['raw_image']
            drawn_binary = make_binary_image_array(drawn_binary)
        elif verbose:
            sys.stderr.write(
                "Skipping the invasive margin since there is none present.\n")
        image_id1 = uuid4().hex
        self._images[image_id1] = drawn_binary

        area_binary = read_tiff_stack(area_image)[0]['raw_image']
        area_binary = make_binary_image_array(area_binary)
        image_id2 = uuid4().hex
        self._images[image_id2] = area_binary
        df = pd.DataFrame({
            'custom_label': ['Drawn', 'Area'],
            'image_id': [image_id1, image_id2]
        })
        df.index.name = 'db_id'
        self.set_data('custom_images', df)

        grown = binary_image_dilation(drawn_binary, steps=steps)
        processed_image = self.get_image(self.processed_image_id).astype(
            np.uint8)
        margin_binary = grown & processed_image
        tumor_binary = (area_binary & (~grown)) & processed_image
        stroma_binary = (~(
            (tumor_binary | grown) & processed_image)) & processed_image

        d = {
            'Margin': margin_binary,
            'Tumor': tumor_binary,
            'Stroma': stroma_binary
        }
        self.set_regions(d)
        return d
Esempio n. 4
0
 def _read_component_image(self, filename):
     stack = read_tiff_stack(filename)
     channels = []
     for i, raw in enumerate(stack):
         meta = raw['raw_meta']['image_description']
         markers = [x.split('=')[1] for x in meta.split('\n')[1:]]
         channel_label = markers[i]
         image_id = uuid4().hex
         self._images[image_id] = raw['raw_image'].astype(
             self._storage_type)
         channels.append((channel_label, image_id))
     df = pd.DataFrame(channels, columns=['channel_label', 'image_id'])
     temp = self.get_data('measurement_channels').drop(
         columns=['image_id']).reset_index().merge(df,
                                                   on='channel_label',
                                                   how='left')
     self.set_data('measurement_channels', temp.set_index('channel_index'))
     return
Esempio n. 5
0
 def _read_component_image(self, filename, channel_abbreviations):
     stack = read_tiff_stack(filename)
     channels = []
     for index, v in enumerate(stack):
         img = v['raw_image']
         meta = v['raw_meta']
         image_description = json.loads(meta['ImageDescription'])
         channel_label = image_description['channel.target']
         image_id = uuid4().hex
         channels.append((channel_label, image_id))
         self._images[image_id] = img  # saving image
     df = pd.DataFrame(channels, columns=['channel_label', 'image_id'])
     df['channel_abbreviation'] = df['channel_label'].apply(
         lambda x: x
         if x not in channel_abbreviations else channel_abbreviations[x])
     df.index.name = 'channel_index'
     self.set_data('measurement_channels', df)
     return
Esempio n. 6
0
    def set_area(self,
                 area_image,
                 custom_mask_name,
                 other_mask_name,
                 verbose=False):
        area_binary = read_tiff_stack(area_image)[0]['raw_image']
        area_binary = make_binary_image_array(area_binary)
        image_id = uuid4().hex
        self._images[image_id] = area_binary
        df = pd.DataFrame({'custom_label': ['Area'], 'image_id': [image_id]})
        df.index.name = 'db_id'
        self.set_data('custom_images', df)

        processed_image = self.get_image(self.processed_image_id).astype(
            np.uint8)
        #margin_binary = grown&processed_image
        tumor_binary = area_binary & processed_image
        stroma_binary = (~(tumor_binary & processed_image)) & processed_image
        d = {custom_mask_name: tumor_binary, other_mask_name: stroma_binary}
        self.set_regions(d)
        return d
Esempio n. 7
0
    def _read_binary_seg_image(self, filename):
        stack = read_tiff_stack(filename)
        mask_names = []
        segmentation_names = []
        for raw in stack:
            meta = raw['raw_meta']
            image_type, image_description = self._parse_image_description(
                meta['ImageDescription'])
            #print("parsing image description")
            #print("meta: "+str(meta))
            #print("image_type: "+str(image_type))
            #print("image_description: "+str(image_description))
            image_id = uuid4().hex
            if image_type == 'SegmentationImage':
                ### Handle if its a segmentation
                self._images[image_id] = raw['raw_image'].astype(int)
                segmentation_names.append(
                    [image_description['CompartmentType'], image_id])
            else:
                ### Otherwise it is a mask
                self._images[image_id] = raw['raw_image'].astype(int)
                mask_names.append([image_type, image_id])
                ### If we have a Tissue Class Map we should process it into regions
                if image_type == 'TissueClassMap':
                    # Process the Tissue Class Map
                    self._process_tissue_class_map(
                        image_description, raw['raw_image'].astype(int))

        _mask_key = pd.DataFrame(mask_names,
                                 columns=['mask_label', 'image_id'])
        _mask_key.index.name = 'db_id'
        self.set_data('mask_images', _mask_key)
        _segmentation_key = pd.DataFrame(
            segmentation_names, columns=['segmentation_label', 'image_id'])
        _segmentation_key.index.name = 'db_id'
        self.set_data('segmentation_images', _segmentation_key)
Esempio n. 8
0
    def _read_data(self, annotations, mibi_cell_labels_tif_path):
        """ Read in the image data from MIBI using mibitracker

        :param cell_seg_data_file:
        :type string:

        """
        cell_labels = read_tiff_stack(
            mibi_cell_labels_tif_path)[0]['raw_image']
        if self.verbose:
            sys.stderr.write(
                "Calling mibitracker cell total intensity scores\n")
        #_seg = mibi_segmentation.extract_cell_dataframe(cell_labels, mibi_tiff.read(mibi_component_tif_path), mode='total')
        #print(_seg)
        #if 'Tissue Category' not in _seg: _seg['Tissue Category'] = 'Any'

        _cells = pd.DataFrame(annotations).set_index('cell_index')

        ###########
        # Set dummy values for the cell phenotypes
        _phenotypes = _cells.copy().drop(columns=['x', 'y'])
        #_phenotypes['phenotype_label'] = np.nan
        _phenotypes = _phenotypes.loc[:, ['phenotype_label']]
        _phenotypes_present = pd.Series(
            _phenotypes['phenotype_label'].unique()).tolist()
        _phenotypes_present = [np.nan]
        _phenotype_list = pd.DataFrame(
            {'phenotype_label': _cells['phenotype_label'].unique()})
        _phenotype_list.index.name = 'phenotype_index'
        _phenotype_list = _phenotype_list.reset_index()
        _phenotypes = _phenotypes.reset_index().merge(
            _phenotype_list, on='phenotype_label').set_index('cell_index')
        _phenotype_list = _phenotype_list.set_index('phenotype_index')

        #Assign 'phenotypes' in a way that ensure we retain the pre-defined column structure
        self.set_data('phenotypes', _phenotype_list)
        if self.verbose:
            sys.stderr.write("Finished assigning phenotype list.\n")

        _phenotypes = _phenotypes.drop(
            columns=['phenotype_label']).applymap(int)

        # Now we can add to cells our phenotype indecies
        _cells = _cells.merge(_phenotypes,
                              left_index=True,
                              right_index=True,
                              how='left')

        ###########
        # Set the cell_regions

        _cell_regions = _cells[['x']].copy()
        _cell_regions['region_label'] = 'Any'
        _cell_regions = _cell_regions.drop(columns=['x'])

        _cell_regions = _cell_regions.reset_index().merge(
            self.get_data('regions')[['region_label']].reset_index(),
            on='region_label')
        _cell_regions = _cell_regions.drop(
            columns=['region_label']).set_index('cell_index')

        # Now we can add to cells our region indecies
        _cells = _cells.merge(_cell_regions,
                              left_index=True,
                              right_index=True,
                              how='left').drop(columns=['phenotype_label'])

        # Assign 'cells' in a way that ensures we retain our pre-defined column structure. Should throw a warning if anything is wrong
        self.set_data('cells', _cells)
        if self.verbose:
            sys.stderr.write(
                "Finished setting the cell list regions are set.\n")

        ###########
        # Get the intensity measurements - sets 'measurement_channels', 'measurement_statistics', 'measurement_features', and 'cell_measurements'

        #'cell_measurements':{'index':'measurement_index',
        #                     'columns':['cell_index','statistic_index','feature_index','channel_index','value']},
        #'measurement_features':{'index':'feature_index',
        #                        'columns':['feature_label']},
        #'measurement_statistics':{'index':'statistic_index',
        #                          'columns':['statistic_label']},

        #_seg2 = _seg.reset_index().set_index(['label','area','x_centroid','y_centroid']).\
        #    stack().reset_index().rename(columns={'level_4':'marker',0:'Total'})
        #_seg2['feature_label'] = 'Total'

        #if self.verbose: sys.stderr.write("Calling mibitracker circular_sectors with 4 sectors\n")
        #_seg3 = mibi_segmentation.extract_cell_dataframe(cell_labels, mibi_tiff.read(mibi_component_tif_path), mode='circular_sectors', num_sectors=4).\
        #    reset_index().set_index(['label','area','x_centroid','y_centroid']).\
        #    stack().reset_index().rename(columns={'level_4':'marker',0:'Total'})
        #_seg3['feature_label'] = 'Circular Sectors 4'

        #if self.verbose: sys.stderr.write("Calling mibitracker circular_sectors with 8 sectors\n")
        #_seg4 = mibi_segmentation.extract_cell_dataframe(cell_labels, mibi_tiff.read(mibi_component_tif_path), mode='circular_sectors', num_sectors=8).\
        #    reset_index().set_index(['label','area','x_centroid','y_centroid']).\
        #    stack().reset_index().rename(columns={'level_4':'marker',0:'Total'})
        #_seg4['feature_label'] = 'Circular Sectors 8'

        #_segs = pd.concat([_seg2,_seg3,_seg4])
        #_segs['Mean'] = _segs.apply(lambda x: x['Total']/x['area'],1)
        #_segs = _segs.rename(columns={'label':'cell_index','marker':'channel_label'}).\
        #    drop(columns=['area','x_centroid','y_centroid']).set_index(['cell_index','channel_label','feature_label']).\
        #    stack().reset_index().rename(columns={0:'value','level_3':'statistic_label'})

        _measurement_statistics = pd.DataFrame([[0,'Total']],columns=['statistic_index','statistic_label']).\
                                     set_index('statistic_index')
        self.set_data('measurement_statistics', _measurement_statistics)

        _measurement_features = pd.DataFrame({'feature_label': ['Total']})
        _measurement_features.index.name = 'feature_index'
        self.set_data('measurement_features', _measurement_features)

        _cell_measurements = pd.DataFrame(
            (),
            columns=[
                'cell_index', 'statistic_index', 'feature_index',
                'channel_index', 'value'
            ])
        _cell_measurements.index.name = 'measurement_index'
        self.set_data('cell_measurements', _cell_measurements)

        if self.verbose:
            sys.stderr.write("Finished setting the measurements.\n")
        ###########

        return
Esempio n. 9
0
    def _read_seg_image(self, mibi_cell_labels_tif_path,
                        generate_processed_area_image,
                        processed_area_image_steps):
        # Do our segmentation first
        cell_labels = read_tiff_stack(
            mibi_cell_labels_tif_path)[0]['raw_image']
        image_id = uuid4().hex
        self._images[image_id] = cell_labels
        segmentation_names = [['Membrane', image_id]]
        _segmentation_key = pd.DataFrame(
            segmentation_names, columns=['segmentation_label', 'image_id'])
        _segmentation_key.index.name = 'db_id'
        self.set_data('segmentation_images', _segmentation_key)

        # make the cell map to be a copy of the membrane segmentation
        cell_map_id = uuid4().hex
        self._images[cell_map_id] = cell_labels.copy()
        increment = self.get_data('segmentation_images').index.max() + 1
        extra = pd.DataFrame(
            pd.Series(
                dict({
                    'db_id': increment,
                    'segmentation_label': 'cell_map',
                    'image_id': cell_map_id
                }))).T
        extra = pd.concat(
            [self.get_data('segmentation_images'),
             extra.set_index('db_id')])
        self.set_data('segmentation_images', extra)

        # make the edge map incrementally
        edge_map_id = uuid4().hex
        self._images[edge_map_id] = image_edges(cell_labels)
        increment = self.get_data('segmentation_images').index.max() + 1
        extra = pd.DataFrame(
            pd.Series(
                dict({
                    'db_id': increment,
                    'segmentation_label': 'edge_map',
                    'image_id': edge_map_id
                }))).T
        extra = pd.concat(
            [self.get_data('segmentation_images'),
             extra.set_index('db_id')])
        self.set_data('segmentation_images', extra)

        image_id = uuid4().hex
        mask_names = [['ProcessRegionImage', image_id]]
        processed = make_binary_image_array(np.ones(cell_labels.shape))
        self._images[image_id] = processed
        if self.verbose:
            sys.stderr.write("size of image: " + str(processed.sum().sum()) +
                             "\n")
        if generate_processed_area_image:
            if self.verbose:
                sys.stderr.write(
                    "Creating approximate processed_image_area by watershed.\n"
                )
            processed = binary_image_dilation(
                make_binary_image_array(cell_labels),
                steps=processed_area_image_steps)
            self._images[image_id] = processed
        _mask_key = pd.DataFrame(mask_names,
                                 columns=['mask_label', 'image_id'])
        _mask_key.index.name = 'db_id'
        self.set_data('mask_images', _mask_key)
        self.set_processed_image_id(image_id)
        self._images[self.processed_image_id] = processed.astype(np.int8)
        if self.verbose:
            sys.stderr.write("size of processed image: " +
                             str(processed.sum().sum()) + "\n")