def run_canonical_measurements(experiment_dir):
    '''Run standard measurements on the specified experiment directory'''
    experiment_dir = pathlib.Path(experiment_dir)

    process_data.update_annotations(experiment_dir)

    position_features = ['stage_x', 'stage_y', 'starting_stage_z', 'notes']
    annotations = load_data.read_annotations(experiment_dir)
    annotations = load_data.filter_annotations(annotations,
                                               load_data.filter_excluded)
    annotations = load_data.filter_annotations(annotations,
                                               filter_worm_positions)

    #print('warning: Im using a custom filter function')
    #annotations = load_data.filter_annotations(annotations, lambda name, pa, ta: name < '25') # Remove me later

    if any([
            'lawn_area' in position_annotations
            for (position_annotations,
                 timepoint_annotations) in annotations.items()
    ]):
        position_features.append('lawn_area')

    make_basic_measurements(experiment_dir, annotations)

    process_data.collate_data(experiment_dir,
                              position_features=position_features)

    return
    make_pose_measurements(experiment_dir, annotations)

    process_data.collate_data(experiment_dir,
                              position_features=position_features)

    #make_mask_measurements(experiment_dir, annotations)

    image_channels = elegant_hacks.get_image_channels(experiment_dir)
    print(f'Image channels: {image_channels}')

    if 'bf_1' in image_channels:
        print('Found multipass movement channel bf_1; making measurements')
        make_multipass_measurements(experiment_dir, annotations)

    process_data.collate_data(
        experiment_dir, position_features=position_features
    )  # For convenience since autofluorescence can take a little while....

    if 'green_yellow_excitation_autofluorescence' in image_channels or 'autofluorescence' in image_channels:
        fl_measurement_name = 'autofluorescence' if 'autofluorescence' in image_channels else 'green_yellow_excitation_autofluorescence'
        print(
            f'Found autofluorescence channel {fl_measurement_name}; making measurements'
        )

        make_af_measurements(experiment_dir,
                             annotations,
                             fl_measurement_name=fl_measurement_name)

    process_data.collate_data(experiment_dir,
                              position_features=position_features)
Ejemplo n.º 2
0
def rw_load_position_images(rw, expt_dir, position, channels='bf'):
    def position_filter(position_name, position_annotations,
                        timepoint_annotations):
        return position_name == position

    process_data.update_annotations(expt_dir)
    position_images = load_data.scan_positions(expt_dir,
                                               position_filter,
                                               channels=channels)
    image_files = position_images[position].values()
    rw.add_image_files_to_flipbook(image_files)
Ejemplo n.º 3
0
def compile_annotations_from_tsv(experiment_root):
    if type(experiment_root) is str:
        experiment_root = pathlib.Path(experiment_root.replace('\\ ', ' '))
    _check_metadata_for_timepoints(experiment_root)
    process_data.update_annotations(experiment_root)

    with (experiment_root /
          'experiment_metadata.json').open('r') as mdata_file:
        experiment_metadata = json.load(mdata_file)

    annotation_data = {}
    with list(experiment_root.glob('*.tsv'))[0].open('r') as annotation_file:
        reader = csv.reader(annotation_file, delimiter='\t')
        _ = reader.__next__()  # Header

        for line in reader:
            position = line[0][1:]  # Starts with '\'
            notes = line[-1]

            annotation_data[position] = {}
            if 'DEAD' in notes:
                for field, frame_num in zip(previous_annotations, line[1:]):
                    annotation_data[position][field] = experiment_metadata[
                        'timepoints'][int(frame_num)]
            annotation_data[position]['notes'] = line[-1]

    annotations = load_data.read_annotations(experiment_root)

    # Note: process_data.propagate_worm_stages assumes that stages are monotonically increasing.
    # To make use of propagate_worm_stages, prepopulate only the timepoints where there's a transition.
    for position, (position_annotations,
                   timepoint_annotations) in annotations.items():
        if 'DEAD' in annotation_data[position]['notes']:
            first_timepoint = list(timepoint_annotations.keys())[0]
            timepoint_annotations[first_timepoint]['stage'] = 'egg'

            for field in previous_annotations:
                transition_timepoint = annotation_data[position][field]
                timepoint_annotations[transition_timepoint]['stage'] = field
            position_annotations['exclude'] = False
        else:
            position_annotations['exclude'] = True
        position_annotations['notes'] = annotation_data[position]['notes']
    load_data.write_annotations(experiment_root, annotations)
    process_data.annotate(
        experiment_root,
        position_annotators=[process_data.propagate_worm_stage])
def process_experiment_with_filter(experiment_root,
                                   model,
                                   image_filter,
                                   mask_root=None,
                                   overwrite_existing=False,
                                   channels='bf',
                                   make_masks=True,
                                   do_annotations=True):
    '''
         image_filter - filter for scan_experiment_dir
    '''

    if mask_root is None:
        mask_root = pathlib.Path(experiment_root) / 'derived_data' / 'mask'

    # Temporary hacks until migration to new elegant complete (while zpl-9000 no longer updates annotations automatically)
    process_data.update_annotations(experiment_root)
    elegant_hacks.propagate_stages(experiment_root)

    start_t = time.time()
    positions = load_data.scan_experiment_dir(experiment_root,
                                              timepoint_filter=image_filter,
                                              channels=channels)
    scan_t = time.time()
    print(f'scanning done after {(scan_t-start_t)} s')

    if make_masks:
        process = segment_images.segment_positions(positions,
                                                   model,
                                                   mask_root,
                                                   use_gpu=True,
                                                   overwrite_existing=False)
        if process.stderr:
            print(f'Errors during segmentation: {process.stderr}'
                  )  #raise Exception)
            #raise Exception()
        segment_t = time.time()
        print(f'segmenting done after {(segment_t-scan_t)} s')
        with (mask_root / 'notes.txt').open('a+') as notes_file:
            notes_file.write(
                f'{datetime.datetime.today().strftime("%Y-%m-%dt%H%M")} These masks were segmented with model {model}\n'
            )
    else:
        print(f'No segmenting performed')

    if do_annotations:
        annotations = load_data.read_annotations(experiment_root)
        metadata = load_data.read_metadata(experiment_root)
        age_factor = metadata.get('age_factor', 1)
        width_estimator = worm_widths.WidthEstimator.from_experiment_metadata(
            metadata, age_factor)
        segment_images.annotate_poses_from_masks(positions, mask_root,
                                                 annotations,
                                                 overwrite_existing,
                                                 width_estimator)
        load_data.write_annotations(experiment_root, annotations)

        annotation_t = time.time()
        print(
            f'annotation done after {(annotation_t - segment_t)} s')  # ~3.5 hr
    else:
        print('No annotations done')
Ejemplo n.º 5
0
    ]  #, elegant_filters.filter_live_animals] #, elegant_filters.filter_after_timepoint('2019-05-20t0000')] #, elegant_filters.select_worms(['15']), elegant_filters.filter_live_animals, elegant_filters.filter_by_stage('adult')] #, elegant_filters.filter_after_timepoint('2019-02-02t1200')]
    channels = [
        'bf'
    ]  #, 'gfp'] #, 'autofluorescence'] #, 'green_yellow_excitation_autofluorescence'] # First one is the one used to load poses when specified.

    # Only use one RisWidget for annotations
    rw_defined = 'rw' in globals()
    global rw
    if not rw_defined: rw = ris_widget.RisWidget()

    # Allows one to restart annotating in the same ris_widget window
    if hasattr(rw, 'annotator'):
        rw.annotator.close()
        del (rw.annotator)

    process_data.update_annotations(expt_dir)
    experiment_annotations = load_data.read_annotations(
        expt_dir, annotation_dir=annotation_dir)

    if timepoint_filters:
        experiment_annotations = load_data.filter_annotations(
            experiment_annotations,
            elegant_filters.compose_timepoint_filters(*timepoint_filters))

    expt_pos = load_data.scan_experiment_dir(
        expt_dir,
        channels=channels,
        timepoint_filter=lambda position_name, timepoint_name:
        not experiment_annotations or
        (position_name in experiment_annotations and timepoint_name in
         experiment_annotations[position_name][1]))