def remove_poses(experiment_root): experiment_annotations = load_data.read_annotations(experiment_root) for position, position_annotations in experiment_annotations.items(): timepoint_annotations = position_annotations[1] for timepoint, timepoint_annotation in timepoint_annotations.items(): timepoint_annotation['pose'] = (None, None) load_data.write_annotations(experiment_root, experiment_annotations)
def replace_annotation(experiment_root, annotation_type, old_annotation_values, new_annotation_value, annotation_dir='annotations'): reinit = input( 'press y and enter to delete annotations; press enter to exit. ') if reinit.lower() == 'y': if not isinstance(old_annotation_values, collections.Iterable): old_annotation_values = list(old_annotation_values) if isinstance(old_annotation_values, str): old_annotation_values = [old_annotation_values] experiment_annotations = load_data.read_annotations( experiment_root, annotation_dir=annotation_dir) for position, position_annotations in experiment_annotations.items(): for timepoint, timepoint_annotations in position_annotations[ 1].items(): if annotation_type in timepoint_annotations and timepoint_annotations[ annotation_type] in old_annotation_values: timepoint_annotations[ annotation_type] = new_annotation_value load_data.write_annotations(experiment_root, experiment_annotations, annotation_dir=annotation_dir) return
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 replace_annotation(experiment_root, annotation_type, old_annotation_values, new_annotation_value, annotation_dir='annotations'): if not isinstance(old_annotation_values, collections.Iterable): old_annotation_values = list(old_annotation_values) if isinstance(old_annotation_values, str): old_annotation_values = [old_annotation_values] experiment_annotations = load_data.read_annotations( experiment_root, annotation_dir=annotation_dir) for position, position_annotations in experiment_annotations.items(): for timepoint, timepoint_annotations in position_annotations[1].items( ): if annotation_type in timepoint_annotations and timepoint_annotations[ annotation_type] in old_annotation_values: timepoint_annotations[annotation_type] = new_annotation_value load_data.write_annotations(experiment_root, experiment_annotations, annotation_dir=annotation_dir)
def propagate_stages(experiment_root, verbose=False): ''' Modifies experiment annotations by propagating stage information forward in time across annotated timepoints. Somewhat deprecated by process_data.propagate_worm_stages/update_annotations; however, useful for the case in which one wants to propagate stages and can't assume that stages are monotonically increasing with time. ''' annotations = load_data.read_annotations(experiment_root) for position_name, (position_annotations, timepoint_annotations) in annotations.items(): running_stage = None changed = [] encountered_stages = [] for timepoint, timepoint_info in timepoint_annotations.items(): already_encountered = timepoint_info.get( 'stage') in encountered_stages stage_set = timepoint_info.get('stage') is not None if running_stage is None: # Either first timepoint or all the annotations up to now are null running_stage = timepoint_info.get('stage') elif timepoint_info.get( 'stage' ) != running_stage and stage_set and not already_encountered: running_stage = timepoint_info['stage'] if stage_set and not already_encountered: encountered_stages.append(timepoint_info['stage']) if not stage_set and running_stage is not None: # Also handles the case that we are working with an excluded position timepoint_info['stage'] = running_stage changed.append(timepoint) elif stage_set and timepoint_info[ 'stage'] != running_stage and already_encountered: timepoint_info['stage'] = running_stage changed.append(timepoint) if verbose and changed: print(f'{position_name}: {changed}') annotations = load_data.write_annotations(experiment_root, annotations)
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')
Example: positions = read_annotations('my_experiment') position_annotations, timepoint_annotations = positions['009'] life_stage = timepoint_annotations['2017-04-23t0122']['stage'] """ experiment_root = pathlib.Path(experiment_root) positions = collections.OrderedDict() for annotation_file in sorted( experiment_root.glob(annotation_subdir + '/*.pickle')): worm_name = annotation_file.stem positions[worm_name] = load_data.read_annotation_file(annotation_file) return positions def transfer_timepoint_annotations(target_annotations, source_annotations, kw): for position in target_annotations: assert target_annotations[position][1].keys( ) == source_annotations[position][1].keys() for timepoint in target_annotations[position][1]: target_annotations[position][1][timepoint][ kw] = source_annotations[position][1][timepoint][kw] return target_annotations expt_dir = '/mnt/9karray/Sinha_Drew/20180518_spe-9_Run_3' DS_annotations = read_annotations(expt_dir) LT_annotations = read_annotations(expt_dir, 'annotations_larval') DS_annotations = transfer_timepoint_annotations(DS_annotations, LT_annotations, 'stage') load_data.write_annotations(expt_dir, DS_annotations)