def by_time_points_amount(_simulations, _time_points, _exactly=False): if _exactly: return [ _simulation for _simulation in _simulations if len(load.properties(_simulation)['time_points']) == _time_points ] else: return [ _simulation for _simulation in _simulations if len(load.properties(_simulation)['time_points']) >= _time_points ]
def window_fiber_density_time_point(_arguments): if 'properties' not in _arguments: _arguments['properties'] = properties(_arguments['simulation']) _time_point_properties = _arguments['properties']['time_points'][ _arguments['time_point']] if _arguments['direction'] == 'inside': _new_direction = 'right' if _arguments[ 'cell_id'] == 'left_cell' else 'left' elif _arguments['direction'] == 'outside': _new_direction = 'left' if _arguments[ 'cell_id'] == 'left_cell' else 'right' else: _new_direction = _arguments['direction'] _time_point_window = window( _length_x=_arguments['length_x'] * CELL_DIAMETER, _length_y=_arguments['length_y'] * CELL_DIAMETER, _offset_x=_arguments['offset_x'] * CELL_DIAMETER, _offset_y=_arguments['offset_y'] * CELL_DIAMETER, _cell_coordinates=_time_point_properties[ _arguments['cell_id']]['coordinates'], _cell_diameter=_time_point_properties[ _arguments['cell_id']]['diameter'], _direction=_new_direction) if 'print' in _arguments and _arguments['print']: print('Computing:', _arguments['simulation'], _arguments['cell_id'], 'window', _time_point_window, 'direction', _arguments['direction'], 'tp', _arguments['time_point']) _window_fiber_density = \ quantification_window_fiber_density(_arguments['simulation'], _arguments['time_point'], _time_point_window) return _arguments, _window_fiber_density
def window_fiber_density_by_time(_arguments): _simulation_properties = properties(_arguments['simulation']) _windows = [] for _time_point in range( min(_arguments['time_points'], len(_simulation_properties['time_points']))): _arguments['time_point'] = _time_point _windows.append(window_fiber_density_time_point(_arguments)[1]) return _arguments, _windows
def by_pair_distance(_simulations, _reverse=False): _organized_simulations = {} for _simulation in _simulations: _simulation_properties = load.properties(_simulation) _pair_distance = compute.pair_distance(_simulation_properties) if _pair_distance in _organized_simulations: _organized_simulations[_pair_distance].append(_simulation) else: _organized_simulations[_pair_distance] = [_simulation] return { _distance: _organized_simulations[_distance] for _distance in sorted(_organized_simulations.keys(), reverse=_reverse) }
def main(): _simulations = load.structured() _simulations = filtering.by_categories( _simulations, _is_single_cell=False, _is_heterogeneity=None, _is_low_connectivity=False, _is_causality=CAUSALITY, _is_dominant_passive=DOMINANT_PASSIVE, _is_fibrin=False) _simulations = filtering.by_pair_distances(_simulations, _distances=PAIR_DISTANCE) print('Total simulations:', len(_simulations)) _arguments = [] for _simulation in _simulations: for _cell_id in ['left_cell', 'right_cell']: _arguments.append({ 'simulation': _simulation, 'length_x': config.QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER, 'length_y': config.QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER, 'offset_x': 0, 'offset_y': 0, 'cell_id': _cell_id, 'direction': 'inside', 'time_points': TIME_POINTS }) _fiber_densities = {} with Pool(CPUS_TO_USE) as _p: for _keys, _value in tqdm(_p.imap_unordered( compute.window_fiber_density_by_time, _arguments), total=len(_arguments), desc='Computing windows & fiber densities'): _fiber_densities[(_keys['simulation'], _keys['cell_id'])] = _value _p.close() _p.join() _simulations_by_heterogeneity = organize.by_heterogeneity(_simulations) _headers = [ 'time_point', 'simulation', 'pair_distance_in_cell_diameter', 'heterogeneity_std', 'left_cell_fiber_density', 'left_cell_fiber_density_z_score', 'right_cell_fiber_density', 'right_cell_fiber_density_z_score', ] _csv_path = os.path.join(paths.OUTPUTS, 'simulations_density_cell_pairs.csv') with open(_csv_path, 'w', newline='') as _csv_file: _csv_writer = csv.writer(_csv_file) _csv_writer.writerow(_headers) for _simulation_std in _simulations_by_heterogeneity: print('STD:', _simulation_std) _std_simulations = _simulations_by_heterogeneity[_simulation_std] for _simulation in tqdm(_std_simulations, desc='Simulations loop'): _properties = load.properties(_simulation) _pair_distance = compute.pair_distance(_properties) _average, _std = load.normalization(_simulation).values() _left_cell_fiber_densities = _fiber_densities[(_simulation, 'left_cell')] _right_cell_fiber_densities = _fiber_densities[(_simulation, 'right_cell')] for _time_point, (_left_cell_fiber_density, _right_cell_fiber_density) in \ enumerate(zip(_left_cell_fiber_densities, _right_cell_fiber_densities)): _left_cell_fiber_density_z_score = compute_lib.z_score( _left_cell_fiber_density, _average, _std) _right_cell_fiber_density_z_score = compute_lib.z_score( _right_cell_fiber_density, _average, _std) _csv_writer.writerow([ _time_point, _simulation, _pair_distance, _simulation_std, _left_cell_fiber_density, _left_cell_fiber_density_z_score, _right_cell_fiber_density, _right_cell_fiber_density_z_score ])
def by_pair_distances(_simulations, _distances): return [ _simulation for _simulation in _simulations if pair_distance(load.properties(_simulation)) in _distances ]
def main(): _simulations = load.structured() _simulations = filtering.by_time_points_amount(_simulations, TIME_POINT) _simulations = filtering.by_categories(_simulations, _is_single_cell=False, _is_heterogeneity=True, _is_low_connectivity=False, _is_causality=False, _is_dominant_passive=False, _is_fibrin=False) _simulations = filtering.by_heterogeneity(_simulations, _std=STD) print('Total simulations:', len(_simulations)) _fiber_densities = compute_fiber_densities(_simulations) _correlations = [] _pair_distances = [] for _simulation in tqdm(_simulations, desc='Simulations loop'): _properties = load.properties(_simulation) _left_cell_fiber_densities = _fiber_densities[(_simulation, 'left_cell')] _right_cell_fiber_densities = _fiber_densities[(_simulation, 'right_cell')] _correlations.append( compute_lib.correlation( compute_lib.derivative(_left_cell_fiber_densities, _n=DERIVATIVE), compute_lib.derivative(_right_cell_fiber_densities, _n=DERIVATIVE))) _pair_distances.append(compute.pair_distance(_properties)) print('Total pairs:', len(_correlations)) print( 'Pearson:', compute_lib.correlation(_correlations, _pair_distances, _with_p_value=True)) # plot _fig = go.Figure( data=go.Scatter(x=_pair_distances, y=_correlations, mode='markers', marker={ 'size': 10, 'color': 'black' }, showlegend=False), layout={ 'xaxis': { 'title': 'Pair distance (cell diameter)', 'zeroline': False, # 'tickmode': 'array', # 'tickvals': [4, 6, 8, 10] }, 'yaxis': { 'title': 'Correlation', 'zeroline': False, 'range': [-1.1, 1.2], 'tickmode': 'array', 'tickvals': [-1, -0.5, 0, 0.5, 1] }, # 'shapes': [ # { # 'type': 'line', # 'x0': -1, # 'y0': -1, # 'x1': -1, # 'y1': 1, # 'line': { # 'color': 'black', # 'width': 2 # } # }, # { # 'type': 'line', # 'x0': -1, # 'y0': -1, # 'x1': 1, # 'y1': -1, # 'line': { # 'color': 'black', # 'width': 2 # } # } # ] }) save.to_html(_fig=_fig, _path=os.path.join(paths.PLOTS, save.get_module_name()), _filename='plot')