Esempio n. 1
0
  def save_region_pipeline(self, primary_channel_name=None, secondary_channel_name=None):
    # 1. make unique key
    unique_key = '{}{}-{}'.format(primary_channel_name, secondary_channel_name, random_string())

    # 2. format and save file
    pipeline_text = region_pipeline(unique_key, primary_channel_name, secondary_channel_name)
    with open(os.path.join(self.pipeline_path, 'regions.cppipe')) as open_pipeline_file:
      open_pipeline_file.write(pipeline_text)

    return unique_key
Esempio n. 2
0
  def save_marker_pipeline(self, series_name=None, primary_channel_name=None, secondary_channel_name=None):
    # 1. make unique key
    unique = random_string()
    unique_key = '{}{}-{}'.format(primary_channel_name, secondary_channel_name, unique)

    # 2. format and save file
    pipeline_text = marker_pipeline('{}_s{}_{}_'.format(self.name, series_name, unique), unique_key, primary_channel_name, secondary_channel_name)
    with open(os.path.join(self.pipeline_path, 'markers.cppipe'), 'w+') as open_pipeline_file:
      open_pipeline_file.write(pipeline_text)

    return unique, unique_key
Esempio n. 3
0
  def segment(self, marker_channel_name='-zcomp', threshold_correction_factor=1.2, background=True):

    unique = random_string() # defines a single identifier for this run
    unique_key = '{}{}-{}'.format(marker_channel_name, self.name, unique)

    # setup
    print('getting marker channel')
    marker_channel = self.composite.channels.get(name=marker_channel_name)

    # 1. create primary from markers with marker_channel
    print('running primary')
    marker_channel_primary_name = marker_channel.primary(unique=unique)

    # 2. create pipeline and run
    print('run pipeline')
    self.composite.experiment.save_marker_pipeline(series_name=self.composite.series.name, primary_channel_name=marker_channel_primary_name, secondary_channel_name=self.name, threshold_correction_factor=threshold_correction_factor, background=background, unique=unique, unique_key=unique_key)
    self.composite.experiment.run_pipeline(series_ts=self.composite.series.ts)

    print('import masks')
    # 3. import masks and create new mask channel
    cp_out_file_list = [f for f in os.listdir(self.composite.experiment.cp_path) if (unique_key in f and '.tiff' in f)]
    # make new channel that gets put in mask path
    cp_template = self.composite.templates.get(name='cp')
    mask_template = self.composite.templates.get(name='mask')
    mask_channel = self.composite.mask_channels.create(name=unique_key)
    region_mask_channel = self.composite.mask_channels.get(name__contains=self.composite.current_region_unique)

    for cp_out_file in cp_out_file_list:
      array = imread(os.path.join(self.composite.experiment.cp_path, cp_out_file))
      metadata = cp_template.dict(cp_out_file)
      mask_channel.get_or_create_mask(array, int(metadata['t']))

    print('import data files')
    # 4. import datafiles and access data
    data_file_list = [f for f in os.listdir(self.composite.experiment.cp_path) if (unique in f and '.csv' in f)]
    for df_name in data_file_list:
      data_file, data_file_created, status = self.composite.get_or_create_data_file(self.composite.experiment.cp_path, df_name)

    # 5. create cells and cell instances from tracks
    cell_data_file = self.composite.data_files.get(id_token=unique, data_type='Cells')
    data = cell_data_file.load()

    # load masks and associate with grayscale id's
    for t in range(self.composite.series.ts):
      mask_mask = mask_channel.masks.get(t=t)
      mask = mask_mask.load()

      # load region mask
      region_mask_mask = region_mask_channel.masks.get(t=t)
      region_mask = region_mask_mask.load()

      t_data = list(filter(lambda d: int(d['ImageNumber'])-1==t, data))

      markers = marker_channel.markers.filter(track_instance__t=t)
      for marker in markers:
        # 1. create cell
        cell, cell_created = self.composite.experiment.cells.get_or_create(series=self.composite.series, track=marker.track)

        # 2. create cell instance
        cell_instance, cell_instance_created = cell.instances.get_or_create(experiment=cell.experiment,
                                                                            series=cell.series,
                                                                            track_instance=marker.track_instance)

        # 3. create cell mask
        gray_value_id = mask[marker.r, marker.c]
        region_gray_value_id = region_mask[marker.r, marker.c]
        region_instance = self.composite.series.region_instances.filter(region_track_instance__t=t, mode_gray_value_id=region_gray_value_id)
        if region_instance:
          region_instance = region_instance[0]
        else:
          region_instance = None
          for ri in self.composite.series.region_instances.filter(region_track_instance__t=t):
            gray_value_ids = [ri_mask.gray_value_id for ri_mask in ri.masks.all()]
            if region_instance is None and region_gray_value_id in gray_value_ids:
              region_instance = ri

        if gray_value_id!=0:
          cell_mask = cell_instance.masks.create(experiment=cell.experiment,
                                                 series=cell.series,
                                                 cell=cell,
                                                 region=region_instance.region if region_instance is not None else None,
                                                 region_instance=region_instance if region_instance is not None else None,
                                                 channel=mask_channel,
                                                 mask=mask_mask,
                                                 marker=marker,
                                                 gray_value_id=gray_value_id)

          cell_mask_data = list(filter(lambda d: int(d['ObjectNumber'])==cell_mask.gray_value_id, t_data))[0]

          # 4. assign data
          cell_mask.r = cell_mask.marker.r
          cell_mask.c = cell_mask.marker.c
          cell_mask.t = t
          cell_mask.AreaShape_Area = float(cell_mask_data['AreaShape_Area']) if str(cell_mask_data['AreaShape_Area']) != 'nan' else -1.0
          cell_mask.AreaShape_Compactness = float(cell_mask_data['AreaShape_Compactness']) if str(cell_mask_data['AreaShape_Compactness']) != 'nan' else -1.0
          cell_mask.AreaShape_Eccentricity = float(cell_mask_data['AreaShape_Eccentricity']) if str(cell_mask_data['AreaShape_Eccentricity']) != 'nan' else -1.0
          cell_mask.AreaShape_EulerNumber = float(cell_mask_data['AreaShape_EulerNumber']) if str(cell_mask_data['AreaShape_EulerNumber']) != 'nan' else -1.0
          cell_mask.AreaShape_Extent = float(cell_mask_data['AreaShape_Extent']) if str(cell_mask_data['AreaShape_Extent']) != 'nan' else -1.0
          cell_mask.AreaShape_FormFactor = float(cell_mask_data['AreaShape_FormFactor']) if str(cell_mask_data['AreaShape_FormFactor']) != 'nan' else -1.0
          cell_mask.AreaShape_MajorAxisLength = float(cell_mask_data['AreaShape_MajorAxisLength']) if str(cell_mask_data['AreaShape_MajorAxisLength']) != 'nan' else -1.0
          cell_mask.AreaShape_MaximumRadius = float(cell_mask_data['AreaShape_MaximumRadius']) if str(cell_mask_data['AreaShape_MaximumRadius']) != 'nan' else -1.0
          cell_mask.AreaShape_MeanRadius = float(cell_mask_data['AreaShape_MeanRadius']) if str(cell_mask_data['AreaShape_MeanRadius']) != 'nan' else -1.0
          cell_mask.AreaShape_MedianRadius = float(cell_mask_data['AreaShape_MedianRadius']) if str(cell_mask_data['AreaShape_MedianRadius']) != 'nan' else -1.0
          cell_mask.AreaShape_MinorAxisLength = float(cell_mask_data['AreaShape_MinorAxisLength']) if str(cell_mask_data['AreaShape_MinorAxisLength']) != 'nan' else -1.0
          cell_mask.AreaShape_Orientation = float(cell_mask_data['AreaShape_Orientation']) if str(cell_mask_data['AreaShape_Orientation']) != 'nan' else -1.0
          cell_mask.AreaShape_Perimeter = float(cell_mask_data['AreaShape_Perimeter']) if str(cell_mask_data['AreaShape_Perimeter']) != 'nan' else -1.0
          cell_mask.AreaShape_Solidity = float(cell_mask_data['AreaShape_Solidity']) if str(cell_mask_data['AreaShape_Solidity']) != 'nan' else -1.0

          cell_mask.save()

          # for now
          cell_instance.set_from_masks(unique)

        else:
          # for now
          cell_instance.set_from_markers()

    # 6. calculate cell velocities
    print('calculating velocities...')
    for cell in self.composite.experiment.cells.all():
      cell.calculate_velocities()
      cell.calculate_confidences()

    return unique
Esempio n. 4
0
  def segment_regions(self, region_marker_channel_name='-zbf', threshold_correction_factor=1.2, background=True):

    unique = random_string() # defines a single identifier for this run
    unique_key = '{}{}-{}'.format(region_marker_channel_name, self.name, unique)

    # setup
    region_marker_channel = self.composite.channels.get(name=region_marker_channel_name)

    # 1. create region primary
    print('running primary')
    region_marker_channel_primary_name = region_marker_channel.region_primary(unique=unique)

    # 2. create pipeline and run
    self.composite.experiment.save_region_pipeline(series_name=self.composite.series.name, primary_channel_name=region_marker_channel_primary_name, secondary_channel_name=self.name, threshold_correction_factor=threshold_correction_factor, background=background, unique=unique, unique_key=unique_key)
    self.composite.experiment.run_pipeline(series_ts=self.composite.series.ts, key='regions')

    # 3. import masks
    print('import masks')
    cp_out_file_list = [f for f in os.listdir(self.composite.experiment.cp_path) if (unique_key in f and '.tiff' in f)]
    # make new channel that gets put in mask path
    cp_template = self.composite.templates.get(name='cp')
    mask_template = self.composite.templates.get(name='mask')
    mask_channel = self.composite.mask_channels.create(name=unique_key)

    for cp_out_file in cp_out_file_list:
      array = imread(os.path.join(self.composite.experiment.cp_path, cp_out_file))
      metadata = cp_template.dict(cp_out_file)
      mask_channel.get_or_create_mask(array, int(metadata['t']))

    # 4. create regions and tracks
    print('create regions and tracks...')
    mask_mask = mask_channel.masks.get(t=0) # this is only the first mask. All others will have the same gray values.
    mask = mask_mask.load()

    for t in range(self.composite.series.ts):
      # mask_mask = mask_channel.masks.get(t=t) # this can be used if the regions are properly tracked.
      # mask = mask_mask.load()

      region_markers = region_marker_channel.region_markers.filter(region_track_instance__t=t)
      for region_marker in region_markers:
        # 1. create cell
        region, region_created = self.composite.experiment.regions.get_or_create(series=self.composite.series, region_track=region_marker.region_track, name=region_marker.region_track.name)

        # 2. create cell instance
        region_instance, region_instance_created = region.instances.get_or_create(experiment=region.experiment,
                                                                                  series=region.series,
                                                                                  region_track_instance=region_marker.region_track_instance)

        # 3. create cell mask
        # gray value id is only taken from the first frame since the regions are tracked
        # and will retain their id.
        gray_value_id = mask[region_marker.r, region_marker.c]
        region_mask = region_instance.masks.create(experiment=region.experiment,
                                                   series=region.series,
                                                   region=region,
                                                   mask=mask_mask)
        region_mask.gray_value_id = gray_value_id
        region_mask.save()

      for region_track_instance in region_marker_channel.region_track_instances.filter(t=t):
        gray_value_ids = [region_mask.gray_value_id for region_mask in region_track_instance.region_instance.masks.filter(mask=mask_mask)]
        region_track_instance.region_instance.mode_gray_value_id = int(mode(gray_value_ids)[0][0])
        region_track_instance.region_instance.save()

    self.composite.current_region_unique = unique
    self.composite.save()

    return unique