def main(): _tuples = [('Metastasis_Airyscan_210401_sgAXL', 2, 'cells_1_2', True), ('Metastasis_Airyscan_210401_sgAXL', 3, 'cells_1_3', True), ('Metastasis_Airyscan_210401_sgAXL', 5, 'cells_0_1', True), ('Metastasis_Airyscan_210401_sgAXL', 9, 'cells_0_5', True), ('Metastasis_Airyscan_210401_sgAXL', 11, 'cells_1_2', True), ('Metastasis_Airyscan_210401_sgAXL', 14, 'cells_0_1', True), ('Metastasis_Airyscan_210401_sgAXL', 15, 'cells_1_2', True)] for _tuple in _tuples: _e, _s, _g, _b = _tuple _p = load.group_properties(_e, _s, _g) _p['band'] = _b _group_structured_path = paths.structured(_e, _s, _g) _properties_json_path = os.path.join(_group_structured_path, 'properties.json') save_lib.to_json(_p, _properties_json_path) print(_tuple) _g_fake = 'fake_' + _g.split('cells_')[1] _group_structured_path = paths.structured(_e, _s, _g_fake) _properties_json_path = os.path.join(_group_structured_path, 'properties.json') if os.path.isfile(_properties_json_path): print(_g_fake) _p = load.group_properties(_e, _s, _g_fake) _p['band'] = _b save_lib.to_json(_p, _properties_json_path)
def process_group(_experiment, _series_id, _group, _overwrite=False): _group_properties = load.group_properties(_experiment, _series_id, _group) if not _overwrite and _group_properties['band'] is not None: return if 'static' in _group: return elif 'fake' in _group: _group_real = 'cells_' + _group.split('fake_')[1] else: _group_real = _group _group_real_properties = load.group_properties(_experiment, _series_id, _group_real) _time_frames_amount = len(_group_real_properties['time_points']) _fiber_density = [] for _time_frame in [ 0, int(round(_time_frames_amount / 2)), _time_frames_amount - 1 ]: _left_cell_coordinates = _group_real_properties['time_points'][ _time_frame]['left_cell']['coordinates'] _right_cell_coordinates = _group_real_properties['time_points'][ _time_frame]['right_cell']['coordinates'] _cell_diameter_x = \ AVERAGE_CELL_DIAMETER_IN_MICRONS / _group_real_properties['time_points'][_time_frame]['resolutions']['x'] _cell_diameter_y = \ AVERAGE_CELL_DIAMETER_IN_MICRONS / _group_real_properties['time_points'][_time_frame]['resolutions']['y'] _cell_diameter_z = \ AVERAGE_CELL_DIAMETER_IN_MICRONS / _group_real_properties['time_points'][_time_frame]['resolutions']['z'] _x1 = (_left_cell_coordinates['x'] + _right_cell_coordinates['x']) / 2 - _cell_diameter_x / 2 _x2 = _x1 + _cell_diameter_x _y1 = (_left_cell_coordinates['y'] + _right_cell_coordinates['y']) / 2 - _cell_diameter_y / 2 _y2 = _y1 + _cell_diameter_y _z1 = (_left_cell_coordinates['z'] + _right_cell_coordinates['z']) / 2 - _cell_diameter_z / 2 _z2 = _z1 + _cell_diameter_z _window = [_x1, _y1, _z1, _x2, _y2, _z2] _fiber_density = compute.window_fiber_density( _experiment=_experiment, _series_id=_series_id, _group=_group_real, _time_frame=_time_frame, _window=[int(round(_value)) for _value in _window])[:2] _fiber_density.append(_fiber_density) if _fiber_density[0] < _fiber_density[1] < _fiber_density[2]: _band = True else: _band = False print(_experiment, _series_id, _group, _band, sep='\t') _group_properties['band'] = _band _group_structured_path = paths.structured(_experiment, _series_id, _group) _properties_json_path = os.path.join(_group_structured_path, 'properties.json') save_lib.to_json(_group_properties, _properties_json_path)
def main(): _experiment = 'LiveDead_201220' _band = False _tuples = load.experiment_groups_as_tuples(_experiment) print('Total tuples:', len(_tuples)) for _tuple in _tuples: print(_tuple) _experiment, _series_id, _group = _tuple _properties = load.group_properties(_experiment, _series_id, _group) _properties['band'] = _band _group_structured_path = paths.structured(_experiment, _series_id, _group) _properties_json_path = os.path.join(_group_structured_path, 'properties.json') save_lib.to_json(_properties, _properties_json_path)
def process_group(_experiment, _series_id, _cells_coordinates, _cell_1_id, _cell_2_id, _series_image_by_time_frames, _resolutions, _real_cells=True, _fake_cell_1_id=None, _fake_cell_2_id=None, _x_change=0, _y_change=0, _z_change=0, _overwrite=False): _time_frames_data = [] _left_cell_id = None _right_cell_id = None _time_frames_amount = min( len([_value for _value in _cells_coordinates[_cell_1_id] if _value is not None]), len([_value for _value in _cells_coordinates[_cell_2_id] if _value is not None]) ) # smooth coordinates _cells_coordinates_cell_1_smoothed = compute.smooth_coordinates_in_time( [_value for _value in _cells_coordinates[_cell_1_id] if _value is not None], _n=SMOOTH_AMOUNT ) _cells_coordinates_cell_2_smoothed = compute.smooth_coordinates_in_time( [_value for _value in _cells_coordinates[_cell_2_id] if _value is not None], _n=SMOOTH_AMOUNT ) if _real_cells: _group = 'cells_' + str(_cell_1_id) + '_' + str(_cell_2_id) elif _fake_cell_1_id is None: _group = 'fake_' + str(_cell_1_id) + '_' + str(_cell_2_id) else: _group = 'static_' + str(_fake_cell_1_id) + '_' + str(_fake_cell_2_id) # check if needed (missing time-point / properties file) if not _overwrite: _missing = False for _time_frame in range(_time_frames_amount): _time_frame_pickle_path = paths.structured(_experiment, _series_id, _group, _time_frame) if not os.path.isfile(_time_frame_pickle_path): _missing = True break _group_structured_path = paths.structured(_experiment, _series_id, _group) _properties_json_path = os.path.join(_group_structured_path, 'properties.json') if not os.path.isfile(_properties_json_path): _missing = True if not _missing: return # running for each time point for _time_frame in range(_time_frames_amount): _time_frame_image = _series_image_by_time_frames[_time_frame] _cell_1_coordinates = [_value for _value in _cells_coordinates_cell_1_smoothed[_time_frame]] _cell_2_coordinates = [_value for _value in _cells_coordinates_cell_2_smoothed[_time_frame]] # update coordinates if needed if any([_x_change != 0, _y_change != 0, _z_change != 0]): _cell_1_coordinates = [ _cell_1_coordinates[0] + _x_change, _cell_1_coordinates[1] + _y_change, _cell_1_coordinates[2] + _z_change ] _cell_2_coordinates = [ _cell_2_coordinates[0] + _x_change, _cell_2_coordinates[1] + _y_change, _cell_2_coordinates[2] + _z_change ] # choose left and right cells if _time_frame == 0: if _cell_1_coordinates[0] <= _cell_2_coordinates[0]: _left_cell_id, _right_cell_id = _cell_1_id, _cell_2_id else: _right_cell_id, _left_cell_id = _cell_1_id, _cell_2_id print(_experiment, 'Series ' + str(_series_id), 'Cell 1 #:', _cell_1_id, 'Cell 2 #:', _cell_2_id, 'Time point:', _time_frame, sep='\t') # set coordinates if _left_cell_id == _cell_1_id: _left_cell_coordinates, _right_cell_coordinates = _cell_1_coordinates, _cell_2_coordinates else: _right_cell_coordinates, _left_cell_coordinates = _cell_1_coordinates, _cell_2_coordinates # compute padding _helper_coordinates = (_left_cell_coordinates[0] + 1, _left_cell_coordinates[1]) _angle = compute.angle_between_three_points( _right_cell_coordinates, _left_cell_coordinates, _helper_coordinates ) _padding_x, _padding_y = compute.axes_padding(_2d_image_shape=_time_frame_image[0].shape, _angle=_angle) _left_cell_coordinates[0] += _padding_x _left_cell_coordinates[1] += _padding_y _right_cell_coordinates[0] += _padding_x _right_cell_coordinates[1] += _padding_y # rotate image and change axes _time_frame_image_rotated = np.array([rotate(_z, _angle) for _z in _time_frame_image]) _time_frame_image_swapped = np.swapaxes(_time_frame_image_rotated, 0, 1) if SHOW_PLOTS: plt.imshow(_time_frame_image_rotated[int(round(_left_cell_coordinates[2]))]) plt.show() plt.imshow(_time_frame_image_rotated[int(round(_right_cell_coordinates[2]))]) plt.show() # update coordinates _image_center = compute.image_center_coordinates(_image_shape=reversed(_time_frame_image_rotated[0].shape)) _left_cell_coordinates = compute.rotate_point_around_another_point( _point=_left_cell_coordinates, _angle_in_radians=math.radians(_angle), _around_point=_image_center ) _right_cell_coordinates = compute.rotate_point_around_another_point( _point=_right_cell_coordinates, _angle_in_radians=math.radians(_angle), _around_point=_image_center ) _fixed_y = (_left_cell_coordinates[1] + _right_cell_coordinates[1]) / 2 # y is now z _left_cell_coordinates[1] = _left_cell_coordinates[2] _right_cell_coordinates[1] = _right_cell_coordinates[2] # z is now y _left_cell_coordinates[2] = _fixed_y _right_cell_coordinates[2] = _fixed_y if SHOW_PLOTS: plt.imshow(_time_frame_image_swapped[int(round(_left_cell_coordinates[2]))]) plt.show() # swap resolutions _new_resolutions = { 'x': _resolutions['x'], 'y': _resolutions['z'], 'z': _resolutions['y'] } # second rotate, compute padding _helper_coordinates = (_left_cell_coordinates[0] + 1, _left_cell_coordinates[1]) _angle = compute.angle_between_three_points( _right_cell_coordinates, _left_cell_coordinates, _helper_coordinates ) _padding_x, _padding_y = compute.axes_padding(_2d_image_shape=_time_frame_image_swapped[0].shape, _angle=_angle) _left_cell_coordinates[0] += _padding_x _left_cell_coordinates[1] += _padding_y _right_cell_coordinates[0] += _padding_x _right_cell_coordinates[1] += _padding_y # rotate image _time_frame_image_swapped_rotated = np.array([rotate(_z, _angle) for _z in _time_frame_image_swapped]) # convert to 8 bit color depth if CONVERT_TO_COLOR_DEPTH_8_BIT: _time_frame_image_swapped_rotated = \ np.rint(_time_frame_image_swapped_rotated / (math.pow(2, 16) - 1) * (math.pow(2, 8) - 1)).astype(np.uint8) # update coordinates _image_center = compute.image_center_coordinates( _image_shape=reversed(_time_frame_image_swapped_rotated[0].shape)) _left_cell_coordinates = compute.rotate_point_around_another_point( _point=_left_cell_coordinates, _angle_in_radians=math.radians(_angle), _around_point=_image_center ) _right_cell_coordinates = compute.rotate_point_around_another_point( _point=_right_cell_coordinates, _angle_in_radians=math.radians(_angle), _around_point=_image_center ) _fixed_y = (_left_cell_coordinates[1] + _right_cell_coordinates[1]) / 2 _left_cell_coordinates[1] = _fixed_y _right_cell_coordinates[1] = _fixed_y if SHOW_PLOTS: if _time_frame == 0 or _time_frame == 50 or _time_frame == 150: plt.imshow(_time_frame_image_swapped_rotated[int(round(_left_cell_coordinates[2]))]) plt.show() # update resolutions _angle = abs(_angle) _new_resolution_x = (_angle / 90) * _new_resolutions['y'] + ((90 - _angle) / 90) * _new_resolutions['x'] _new_resolution_y = (_angle / 90) * _new_resolutions['x'] + ((90 - _angle) / 90) * _new_resolutions['y'] _new_resolutions['x'] = _new_resolution_x _new_resolutions['y'] = _new_resolution_y _image_z, _image_y, _image_x = _time_frame_image_swapped_rotated.shape if not 0 <= _left_cell_coordinates[0] < _image_x or not \ 0 <= _left_cell_coordinates[1] < _image_y or not \ 0 <= _left_cell_coordinates[2] < _image_z: break if not 0 <= _right_cell_coordinates[0] < _image_x or not \ 0 <= _right_cell_coordinates[1] < _image_y or not \ 0 <= _right_cell_coordinates[2] < _image_z: break # add to array _time_frames_data.append({ 'left_cell': { 'coordinates': { 'x': _left_cell_coordinates[0], 'y': _left_cell_coordinates[1], 'z': _left_cell_coordinates[2] } }, 'right_cell': { 'coordinates': { 'x': _right_cell_coordinates[0], 'y': _right_cell_coordinates[1], 'z': _right_cell_coordinates[2] } }, 'resolutions': _new_resolutions }) # save to pickle _time_frame_pickle_path = paths.structured(_experiment, _series_id, _group, _time_frame) save_lib.to_pickle(_time_frame_image_swapped_rotated, _time_frame_pickle_path) # save properties if _real_cells: _band = None _fake = False _static = False elif _fake_cell_1_id is None: _based_on_properties = load.group_properties(_experiment, _series_id, 'cells_' + _group.split('fake_')[1]) _band = _based_on_properties['band'] _fake = True _static = False else: _band = False _fake = True _static = True _properties_data = { 'experiment': _experiment, 'series_id': _series_id, 'cells_ids': { 'left_cell': _left_cell_id, 'right_cell': _right_cell_id }, 'time_points': _time_frames_data, 'band': _band, 'fake': _fake, 'static': _static } _group_structured_path = paths.structured(_experiment, _series_id, _group) _properties_json_path = os.path.join(_group_structured_path, 'properties.json') save_lib.to_json(_properties_data, _properties_json_path)
def process_group(_arguments): _time_frames_data = [] _time_frames_amount = \ len([_value for _value in _arguments['cell_coordinates'][_arguments['cell_id']] if _value is not None]) # smooth coordinates _cells_coordinates_cell_smoothed = compute.smooth_coordinates_in_time( [ _value for _value in _arguments['cell_coordinates'][_arguments['cell_id']] if _value is not None ], _n=SMOOTH_AMOUNT) if _arguments['cell_type'] == 'real': _group = 'cell_' + str(_arguments['cell_id']) + '_' + str(_arguments['degrees_xy']) + '_' + \ str(_arguments['degrees_z']) elif _arguments['cell_type'] == 'fake': _group = 'fake_' + str(_arguments['cell_id']) + '_' + str(_arguments['degrees_xy']) + '_' + \ str(_arguments['degrees_z']) elif _arguments['cell_type'] == 'static': _group = 'static_' + str(_arguments['cell_id']) + '_' + str(_arguments['degrees_xy']) + '_' + \ str(_arguments['degrees_z']) else: raise Exception('No such cell type') # check if needed (missing time-point / properties file) if not _arguments['overwrite']: _missing = False for _time_frame in range(_time_frames_amount): _time_frame_pickle_path = \ paths.structured(_arguments['experiment'], _arguments['series_id'], _group, _time_frame) if not os.path.isfile(_time_frame_pickle_path): _missing = True break _group_structured_path = paths.structured(_arguments['experiment'], _arguments['series_id'], _group) _properties_json_path = os.path.join(_group_structured_path, 'properties.json') if not os.path.isfile(_properties_json_path): _missing = True if not _missing: return # load image if needed if 'series_image_by_time_frames' not in _arguments: _series_image_path = \ paths.serieses(_arguments['experiment'], _arguments['series_id']) _series_image = tifffile.imread(_series_image_path) _arguments['series_image_by_time_frames'] = [ np.array([ _z[IMAGE_FIBER_CHANNEL_INDEX] for _z in _series_image[_time_frame] ]) for _time_frame in range(_series_image.shape[0]) ] # running for each time point for _time_frame in range(_time_frames_amount): _time_frame_image = _arguments['series_image_by_time_frames'][ _time_frame] _cell_coordinates = [ _value for _value in _cells_coordinates_cell_smoothed[_time_frame] ] # update coordinates if needed if 'x_change' in _arguments: _cell_coordinates[0] += _arguments['x_change'] if 'y_change' in _arguments: _cell_coordinates[1] += _arguments['y_change'] if 'z_change' in _arguments: _cell_coordinates[2] += _arguments['z_change'] # compute padding xy _padding_x, _padding_y = \ compute.axes_padding(_2d_image_shape=_time_frame_image[0].shape, _angle=_arguments['degrees_xy']) _cell_coordinates[0] += _padding_x _cell_coordinates[1] += _padding_y # rotate image and change axes _time_frame_image_rotated = np.array( [rotate(_z, _arguments['degrees_xy']) for _z in _time_frame_image]) _time_frame_image_swapped = np.swapaxes(_time_frame_image_rotated, 0, 1) if SHOW_PLOTS: plt.imshow(_time_frame_image_rotated[int( round(_cell_coordinates[2]))]) plt.show() # update coordinates _image_center = compute.image_center_coordinates( _image_shape=reversed(_time_frame_image_rotated[0].shape)) _cell_coordinates = compute.rotate_point_around_another_point( _point=_cell_coordinates, _angle_in_radians=math.radians(_arguments['degrees_xy']), _around_point=_image_center) # y is now z, z is now y _cell_coordinates[1], _cell_coordinates[2] = _cell_coordinates[ 2], _cell_coordinates[1] if SHOW_PLOTS: plt.imshow(_time_frame_image_swapped[int( round(_cell_coordinates[2]))]) plt.show() # swap resolutions _new_resolutions = { 'x': _arguments['resolutions']['x'], 'y': _arguments['resolutions']['z'], 'z': _arguments['resolutions']['y'] } # second rotate, compute padding z _padding_x, _padding_y = \ compute.axes_padding(_2d_image_shape=_time_frame_image_swapped[0].shape, _angle=_arguments['degrees_z']) _cell_coordinates[0] += _padding_x _cell_coordinates[1] += _padding_y # rotate image _time_frame_image_swapped_rotated = \ np.array([rotate(_z, _arguments['degrees_z']) for _z in _time_frame_image_swapped]) # update coordinates _image_center = compute.image_center_coordinates( _image_shape=reversed(_time_frame_image_swapped_rotated[0].shape)) _cell_coordinates = compute.rotate_point_around_another_point( _point=_cell_coordinates, _angle_in_radians=math.radians(_arguments['degrees_z']), _around_point=_image_center) if SHOW_PLOTS: if _time_frame == 0 or _time_frame == 50 or _time_frame == 150: plt.imshow(_time_frame_image_swapped_rotated[int( round(_cell_coordinates[2]))]) plt.show() # update resolutions _angle = abs(_arguments['degrees_z']) _new_resolution_x = (_angle / 90) * _new_resolutions['y'] + ( (90 - _angle) / 90) * _new_resolutions['x'] _new_resolution_y = (_angle / 90) * _new_resolutions['x'] + ( (90 - _angle) / 90) * _new_resolutions['y'] _new_resolutions['x'] = _new_resolution_x _new_resolutions['y'] = _new_resolution_y _image_z, _image_y, _image_x = _time_frame_image_swapped_rotated.shape if not 0 <= _cell_coordinates[0] < _image_x or not \ 0 <= _cell_coordinates[1] < _image_y or not \ 0 <= _cell_coordinates[2] < _image_z: break # add to array _time_frames_data.append({ 'cell': { 'coordinates': { 'x': _cell_coordinates[0], 'y': _cell_coordinates[1], 'z': _cell_coordinates[2] } }, 'resolutions': _new_resolutions }) # save to pickle _time_frame_pickle_path = \ paths.structured(_arguments['experiment'], _arguments['series_id'], _group, _time_frame) save_lib.to_pickle(_time_frame_image_swapped_rotated, _time_frame_pickle_path) # save properties if _arguments['cell_type'] == 'real': _fake = False _static = False elif _arguments['cell_type'] == 'fake': _based_on_properties = \ load.group_properties(_arguments['experiment'], _arguments['series_id'], 'cell_' + _group.split('fake_')[1]) _fake = True _static = False elif _arguments['cell_type'] == 'static': _fake = True _static = True else: raise Exception('No such cell type') _properties_data = { 'experiment': _arguments['experiment'], 'series_id': _arguments['series_id'], 'cell_id': _arguments['cell_id'], 'time_points': _time_frames_data, 'fake': _fake, 'static': _static } _group_structured_path = paths.structured(_arguments['experiment'], _arguments['series_id'], _group) _properties_json_path = os.path.join(_group_structured_path, 'properties.json') save_lib.to_json(_properties_data, _properties_json_path)
def process_series(_experiment, _series_id, _overwrite=False): _normalization_path = paths.normalization(_experiment, _series_id) if not _overwrite and os.path.isfile(_normalization_path): return _series_image_first_time_frame = load.series_image(_experiment, _series_id)[0] _image_properties = load.image_properties(_experiment, _series_id) _cells = load.objects_time_frame_file_data(_experiment, _series_id, _time_frame=1) _cell_diameter_x = AVERAGE_CELL_DIAMETER_IN_MICRONS / _image_properties[ 'resolutions']['x'] _cell_diameter_y = AVERAGE_CELL_DIAMETER_IN_MICRONS / _image_properties[ 'resolutions']['y'] _cell_diameter_z = AVERAGE_CELL_DIAMETER_IN_MICRONS / _image_properties[ 'resolutions']['z'] _z_shape, _y_shape, _x_shape = _series_image_first_time_frame.shape _z_step, _y_step, _x_step = [ int(round(_value * STEP_PERCENTAGE)) for _value in [_z_shape, _y_shape, _x_shape] ] _averages = [] for _z, _y, _x in product(range(0, _z_shape, _z_step), range(0, _y_shape, _y_step), range(0, _x_shape, _x_step)): _x1 = _x - _cell_diameter_x / 2 _x2 = _x1 + _cell_diameter_x _y1 = _y - _cell_diameter_y / 2 _y2 = _y1 + _cell_diameter_y _z1 = _z - _cell_diameter_z / 2 _z2 = _z1 + _cell_diameter_z _x1, _y1, _z1, _x2, _y2, _z2 = [ int(round(_value)) for _value in [_x1, _y1, _z1, _x2, _y2, _z2] ] # window is in borders if all([ _x1 >= 0, _x2 < _x_shape, _y1 >= 0, _y2 < _y_shape, _z1 >= 0, _z2 < _z_shape ]): # make sure no cells are around _in_window = False for _cell in _cells: _cell_x, _cell_y, _cell_z = _cell # cover entire box around the cell for _value_x, _value_y, _value_z in product( CELL_BORDERS_VALUES, CELL_BORDERS_VALUES, CELL_BORDERS_VALUES): if is_in_window(_x1, _y1, _z1, _x2, _y2, _z2, _cell_x + _cell_diameter_x * _value_x, _cell_y + _cell_diameter_y * _value_y, _cell_z + _cell_diameter_z * _value_z): _in_window = True break # one cell is enough if _in_window: break # one cell is enough if _in_window: continue # no cells are around, compute _pixels = _series_image_first_time_frame[_z1:_z2, _y1:_y2, _x1:_x2] # convert to 8 bit color depth if CONVERT_TO_COLOR_DEPTH_8_BIT: _pixels = np.rint(_pixels / (math.pow(2, 16) - 1) * (math.pow(2, 8) - 1)).astype(np.uint8) _averages.append(np.mean(_pixels)) # compute average and std of averages _average, _std = np.mean(_averages), np.std(_averages) # save _normalization = {'average': _average, 'std': _std} save_lib.to_json(_normalization, _normalization_path) print('Saved', _experiment, _series_id, sep='\t')
def image_properties(_experiment, _series_id, _image_properties): _experiment_path = paths.image_properties(_experiment) os.mkdir(_experiment_path) if not os.path.isdir(_experiment_path) else None _path = os.path.join(_experiment_path, 'series_' + str(_series_id) + '.json') save_lib.to_json(_image_properties, _path)
def process_real_fake(_experiment, _series_id, _cells_coordinates, _cell_1_id, _cell_2_id, _based_on_cell_id, _fake_cell_x, _fake_cell_y, _series_image_by_time_frames, _resolutions, _overwrite=False): _time_frames_data = [] _left_cell_id = None _right_cell_id = None _time_frames_amount = len([ _value for _value in _cells_coordinates[_based_on_cell_id] if _value is not None ]) # smooth coordinates _cells_coordinates_based_on_cell_smoothed = compute.smooth_coordinates_in_time( [ _value for _value in _cells_coordinates[_based_on_cell_id] if _value is not None ], _n=SMOOTH_AMOUNT) _cells_coordinates_fake_cell_smoothed = compute.smooth_coordinates_in_time( [ _value for _value in _cells_coordinates[_based_on_cell_id] if _value is not None ], _n=SMOOTH_AMOUNT) _group = 'cells_' + str(_cell_1_id) + '_' + str(_cell_2_id) + '_real_' + str(_based_on_cell_id) + \ '_fake_' + str(_based_on_cell_id) # check if needed (missing time-point / properties file) if not _overwrite: _missing = False for _time_frame in range(_time_frames_amount): _time_frame_pickle_path = paths.structured(_experiment, _series_id, _group, _time_frame) if not os.path.isfile(_time_frame_pickle_path): _missing = True break _group_structured_path = paths.structured(_experiment, _series_id, _group) _properties_json_path = os.path.join(_group_structured_path, 'properties.json') if not os.path.isfile(_properties_json_path): _missing = True if not _missing: return # compute change _x_change = _fake_cell_x - _cells_coordinates_based_on_cell_smoothed[0][0] _y_change = _fake_cell_y - _cells_coordinates_based_on_cell_smoothed[0][1] # running for each time point for _time_frame in range(_time_frames_amount): _time_frame_image = _series_image_by_time_frames[_time_frame] _based_on_cell_coordinates = [ _value for _value in _cells_coordinates_based_on_cell_smoothed[_time_frame] ] _fake_cell_coordinates = [ _value for _value in _cells_coordinates_fake_cell_smoothed[_time_frame] ] # update coordinates of fake _fake_cell_coordinates = [ _based_on_cell_coordinates[0] + _x_change, _based_on_cell_coordinates[1] + _y_change, _based_on_cell_coordinates[2] ] # choose left and right cells if _time_frame == 0: if _based_on_cell_coordinates[0] <= _fake_cell_coordinates[0]: _left_cell_id, _right_cell_id = _cell_1_id, _cell_2_id else: _right_cell_id, _left_cell_id = _cell_1_id, _cell_2_id print(_experiment, 'Series ' + str(_series_id), 'Cell 1 #:', _cell_1_id, 'Cell 2 #:', _cell_2_id, 'Based on cell #:', _based_on_cell_id, 'Time point:', _time_frame, sep='\t') # set coordinates if _left_cell_id == _cell_1_id: _left_cell_coordinates, _right_cell_coordinates = _based_on_cell_coordinates, _fake_cell_coordinates else: _right_cell_coordinates, _left_cell_coordinates = _based_on_cell_coordinates, _fake_cell_coordinates # compute padding _helper_coordinates = (_left_cell_coordinates[0] + 1, _left_cell_coordinates[1]) _angle = compute.angle_between_three_points(_right_cell_coordinates, _left_cell_coordinates, _helper_coordinates) _padding_x, _padding_y = compute.axes_padding( _2d_image_shape=_time_frame_image[0].shape, _angle=_angle) _left_cell_coordinates[0] += _padding_x _left_cell_coordinates[1] += _padding_y _right_cell_coordinates[0] += _padding_x _right_cell_coordinates[1] += _padding_y # rotate image and change axes _time_frame_image_rotated = np.array( [rotate(_z, _angle) for _z in _time_frame_image]) _time_frame_image_swapped = np.swapaxes(_time_frame_image_rotated, 0, 1) if SHOW_PLOTS: plt.imshow(_time_frame_image_rotated[int( round(_left_cell_coordinates[2]))]) plt.show() plt.imshow(_time_frame_image_rotated[int( round(_right_cell_coordinates[2]))]) plt.show() # update coordinates _image_center = compute.image_center_coordinates( _image_shape=reversed(_time_frame_image_rotated[0].shape)) _left_cell_coordinates = compute.rotate_point_around_another_point( _point=_left_cell_coordinates, _angle_in_radians=math.radians(_angle), _around_point=_image_center) _right_cell_coordinates = compute.rotate_point_around_another_point( _point=_right_cell_coordinates, _angle_in_radians=math.radians(_angle), _around_point=_image_center) _fixed_y = (_left_cell_coordinates[1] + _right_cell_coordinates[1]) / 2 # y is now z _left_cell_coordinates[1] = _left_cell_coordinates[2] _right_cell_coordinates[1] = _right_cell_coordinates[2] # z is now y _left_cell_coordinates[2] = _fixed_y _right_cell_coordinates[2] = _fixed_y if SHOW_PLOTS: plt.imshow(_time_frame_image_swapped[int( round(_left_cell_coordinates[2]))]) plt.show() # swap resolutions _new_resolutions = { 'x': _resolutions['x'], 'y': _resolutions['z'], 'z': _resolutions['y'] } # second rotate, compute padding _helper_coordinates = (_left_cell_coordinates[0] + 1, _left_cell_coordinates[1]) _angle = compute.angle_between_three_points(_right_cell_coordinates, _left_cell_coordinates, _helper_coordinates) _padding_x, _padding_y = compute.axes_padding( _2d_image_shape=_time_frame_image_swapped[0].shape, _angle=_angle) _left_cell_coordinates[0] += _padding_x _left_cell_coordinates[1] += _padding_y _right_cell_coordinates[0] += _padding_x _right_cell_coordinates[1] += _padding_y # rotate image _time_frame_image_swapped_rotated = np.array( [rotate(_z, _angle) for _z in _time_frame_image_swapped]) # update coordinates _image_center = compute.image_center_coordinates( _image_shape=reversed(_time_frame_image_swapped_rotated[0].shape)) _left_cell_coordinates = compute.rotate_point_around_another_point( _point=_left_cell_coordinates, _angle_in_radians=math.radians(_angle), _around_point=_image_center) _right_cell_coordinates = compute.rotate_point_around_another_point( _point=_right_cell_coordinates, _angle_in_radians=math.radians(_angle), _around_point=_image_center) _fixed_y = (_left_cell_coordinates[1] + _right_cell_coordinates[1]) / 2 _left_cell_coordinates[1] = _fixed_y _right_cell_coordinates[1] = _fixed_y if SHOW_PLOTS: if _time_frame == 0 or _time_frame == 50 or _time_frame == 150: plt.imshow(_time_frame_image_swapped_rotated[int( round(_left_cell_coordinates[2]))]) plt.show() # update resolutions _angle = abs(_angle) _new_resolution_x = (_angle / 90) * _new_resolutions['y'] + ( (90 - _angle) / 90) * _new_resolutions['x'] _new_resolution_y = (_angle / 90) * _new_resolutions['x'] + ( (90 - _angle) / 90) * _new_resolutions['y'] _new_resolutions['x'] = _new_resolution_x _new_resolutions['y'] = _new_resolution_y # TODO: if the cell coordinates are out of image write it somewhere, so when checking for "overwrite" # it will know when to stop _image_z, _image_y, _image_x = _time_frame_image_swapped_rotated.shape if not 0 <= _left_cell_coordinates[0] < _image_x or not \ 0 <= _left_cell_coordinates[1] < _image_y or not \ 0 <= _left_cell_coordinates[2] < _image_z: break if not 0 <= _right_cell_coordinates[0] < _image_x or not \ 0 <= _right_cell_coordinates[1] < _image_y or not \ 0 <= _right_cell_coordinates[2] < _image_z: break # add to array _time_frames_data.append({ 'left_cell': { 'coordinates': { 'x': _left_cell_coordinates[0], 'y': _left_cell_coordinates[1], 'z': _left_cell_coordinates[2] } }, 'right_cell': { 'coordinates': { 'x': _right_cell_coordinates[0], 'y': _right_cell_coordinates[1], 'z': _right_cell_coordinates[2] } }, 'resolutions': _new_resolutions }) # save to pickle _time_frame_pickle_path = paths.structured(_experiment, _series_id, _group, _time_frame) save_lib.to_pickle(_time_frame_image_swapped_rotated, _time_frame_pickle_path) # save properties _based_on_properties = \ load.group_properties(_experiment, _series_id, 'cells_' + str(_cell_1_id) + '_' + str(_cell_2_id)) _properties_data = { 'experiment': _experiment, 'series_id': _series_id, 'cells_ids': { 'left_cell': _left_cell_id, 'right_cell': _right_cell_id }, 'time_points': _time_frames_data, 'band': _based_on_properties['band'], 'fake': False, 'static': False, 'real_fake': True } _group_structured_path = paths.structured(_experiment, _series_id, _group) _properties_json_path = os.path.join(_group_structured_path, 'properties.json') save_lib.to_json(_properties_data, _properties_json_path)