def compute_data(): _simulations = load.structured() _simulations = filtering.by_time_points_amount(_simulations, _time_points=TIME_POINTS) _simulations = filtering.by_categories(_simulations, _is_single_cell=False, _is_heterogeneity=False, _is_low_connectivity=False, _is_causality=False, _is_dominant_passive=False, _is_fibrin=False) _simulations = filtering.by_pair_distance(_simulations, _distance=PAIR_DISTANCE) print('Total simulations:', len(_simulations)) _fiber_densities = compute_fiber_densities(_simulations) _simulations_fiber_densities = [[] for _i in range(TIME_POINTS)] for _simulation in tqdm(_simulations, desc='Main loop'): _normalization = load.normalization(_simulation) for _time_point in range(TIME_POINTS): for _cell_id in ['left_cell', 'right_cell']: _fiber_density = _fiber_densities[(_simulation, _cell_id)][_time_point] _normalized_fiber_density = compute_lib.z_score( _fiber_density, _normalization['average'], _normalization['std']) _simulations_fiber_densities[_time_point].append( _normalized_fiber_density) print('Total pairs:', len(_simulations_fiber_densities[0])) return _simulations_fiber_densities
def compute_cell_pairs(_low_connectivity): _x_array = [] _y_array = [] _names_array = [] _max_offsets_x = [] for _distance in PAIR_DISTANCE: print('Pair distance ' + str(_distance)) _simulations = load.structured() _simulations = filtering.by_categories( _simulations, _is_single_cell=False, _is_heterogeneity=False, _is_low_connectivity=_low_connectivity, _is_causality=False, _is_dominant_passive=False, _is_fibrin=False) _simulations = filtering.by_pair_distance(_simulations, _distance=_distance) _simulations = filtering.by_time_points_amount( _simulations, _time_points=TIME_POINT[_low_connectivity]) _offsets_x = np.arange(start=0, stop=_distance / 2 - 0.25 - QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER, step=OFFSET_X_STEP) if len(_offsets_x) > len(_max_offsets_x): _max_offsets_x = _offsets_x _fiber_densities = compute_fiber_densities(_simulations, _offsets_x, _low_connectivity) _pair_distance_fiber_densities = [[] for _i in range(len(_offsets_x))] for _simulation in _simulations: _offset_index = 0 _normalization = load.normalization(_simulation) for _offset_x in _offsets_x: for _cell_id in ['left_cell', 'right_cell']: _fiber_density = _fiber_densities[(_simulation, _offset_x, _cell_id)] _normalized_fiber_density = compute_lib.z_score( _fiber_density, _normalization['average'], _normalization['std']) _pair_distance_fiber_densities[_offset_index].append( _normalized_fiber_density) _offset_index += 1 _x_array.append(_offsets_x) _y_array.append(_pair_distance_fiber_densities) _names_array.append('Pair distance ' + str(_distance)) return _names_array, _x_array, _y_array
def compute_data(_arguments): _offset_y_index, _offset_y = _arguments _fiber_densities_array = [] for _simulation in _simulations: _simulation_normalization = load.normalization(_simulation) for _cell_id in ['left_cell', 'right_cell']: _fiber_density = _fiber_densities[(_simulation, _offset_y, _cell_id)] _normalized_fiber_density = compute_lib.z_score( _x=_fiber_density, _average=_simulation_normalization['average'], _std=_simulation_normalization['std'] ) _fiber_densities_array.append(_normalized_fiber_density) if len(_fiber_densities_array) > 0: return _offset_y_index, np.mean(_fiber_densities_array) else: return _offset_y_index, None
def compute_simulations_data(_low_connectivity): _simulations = simulations_load.structured() _simulations = simulations_filtering.by_time_points_amount( _simulations, SIMULATIONS_TIME_POINT[_low_connectivity]) _simulations = simulations_filtering.by_categories( _simulations, _is_single_cell=False, _is_heterogeneity=False, _is_low_connectivity=_low_connectivity, _is_causality=False, _is_dominant_passive=False, _is_fibrin=False) _simulations = simulations_filtering.by_pair_distance( _simulations, _distance=PAIR_DISTANCE) print('Total simulations:', len(_simulations)) _fiber_densities = compute_simulations_fiber_densities( _simulations, _low_connectivity) _simulations_fiber_densities = [[] for _i in range(len(SIMULATIONS_OFFSETS_X)) ] for _simulation in tqdm(_simulations, desc='Simulations Loop'): _normalization = simulations_load.normalization(_simulation) for _offset_x_index, _offset_x in enumerate(SIMULATIONS_OFFSETS_X): for _cell_id in ['left_cell', 'right_cell']: _fiber_density = _fiber_densities[(_simulation, _offset_x, _cell_id)] _normalized_fiber_density = compute_lib.z_score( _fiber_density, _normalization['average'], _normalization['std']) _simulations_fiber_densities[_offset_x_index].append( _normalized_fiber_density) return _simulations_fiber_densities
def compute_simulations_data(_low_connectivity): _simulations = simulations_load.structured() _simulations = simulations_filtering.by_time_points_amount( _simulations, _time_points=SIMULATIONS_TIME_POINT[_low_connectivity] ) _simulations = simulations_filtering.by_categories( _simulations, _is_single_cell=True, _is_heterogeneity=False, _is_low_connectivity=_low_connectivity, _is_causality=False, _is_dominant_passive=False, _is_fibrin=False ) _fiber_densities = compute_simulations_fiber_densities(_simulations, _low_connectivity) _simulations_fiber_densities = [[] for _i in range(len(OFFSETS_X))] for _simulation in tqdm(_simulations, desc='Simulations Loop'): _normalization = simulations_load.normalization(_simulation) for _offset_x_index, _offset_x in enumerate(OFFSETS_X): _direction_fiber_densities = [] for _direction in ['left', 'right', 'up', 'down']: _fiber_density = _fiber_densities[(_simulation, _offset_x, _direction)] _normalized_fiber_density = compute_lib.z_score( _fiber_density, _normalization['average'], _normalization['std'] ) _direction_fiber_densities.append(_normalized_fiber_density) _simulations_fiber_densities[_offset_x_index].append(np.mean(_direction_fiber_densities)) return _simulations_fiber_densities
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 main(): _simulations = load.structured() _simulations = filtering.by_categories(_simulations, _is_single_cell=False, _is_heterogeneity=HETEROGENEITY, _is_low_connectivity=False, _is_causality=False, _is_dominant_passive=False, _is_fibrin=False) _simulations = filtering.by_pair_distances(_simulations, PAIR_DISTANCE) print('Total simulations:', len(_simulations)) _simulations_by_distances = organize.by_pair_distance(_simulations) for _distance in _simulations_by_distances: print('Distance ', _distance, ', total simulations:', len(_simulations_by_distances[_distance])) _fiber_densities = compute_fiber_densities(_simulations) _heatmap_fiber = [] _heatmap_fiber_change = [] for _simulation in _simulations: _simulation_normalization = load.normalization(_simulation) for _cell_id in ['left_cell', 'right_cell']: _cell_fiber_densities = _fiber_densities[(_simulation, _cell_id)] # not enough data if len(_cell_fiber_densities) < DERIVATIVE + 1: continue _z_score_fiber_density = compute_lib.z_score_array( _array=_cell_fiber_densities, _average=_simulation_normalization['average'], _std=_simulation_normalization['std']) _heatmap_fiber += _z_score_fiber_density[DERIVATIVE:] _heatmap_fiber_change += compute_lib.derivative( _z_score_fiber_density, _n=DERIVATIVE) print( compute_lib.correlation(_heatmap_fiber, _heatmap_fiber_change, _with_p_value=True)) if PLOT: _y_shape = int(round((Y_LABELS_END - Y_LABELS_START) * Y_BINS)) _x_shape = int(round((X_LABELS_END - X_LABELS_START) * X_BINS)) _total_points = 0 _z_array = np.zeros(shape=(_y_shape, _x_shape)) for _y, _x in zip(_heatmap_fiber_change, _heatmap_fiber): _y_rounded, _x_rounded = int(round(_y * Y_BINS)), int( round(_x * X_BINS)) _y_index, _x_index = int(_y_rounded - Y_LABELS_START * Y_BINS), int(_x_rounded - X_LABELS_START * X_BINS) if 0 <= _y_index < _z_array.shape[ 0] and 0 <= _x_index < _z_array.shape[1]: _z_array[_y_index][_x_index] += 1 _total_points += 1 _z_array = _z_array / _total_points if not CONDITIONAL_NORMALIZATION: _z_array[_z_array == 0] = None else: _z_array_plot = np.zeros(shape=np.array(_z_array).shape) for _fiber_index, _fiber_density_z_score in enumerate(_z_array): _sum = np.sum(_fiber_density_z_score) for _change_index, _change_z_score in enumerate( _fiber_density_z_score): _z_array_plot[_fiber_index][_change_index] = ( _change_z_score / _sum) if _sum != 0 else 0 _z_array_plot[_z_array_plot == 0] = None _fig = go.Figure( data=go.Heatmap(x=np.arange(start=X_LABELS_START, stop=X_LABELS_END, step=1 / X_BINS), y=np.arange(start=Y_LABELS_START, stop=Y_LABELS_END, step=1 / Y_BINS), z=_z_array, colorscale='Viridis', colorbar={ 'tickmode': 'array', 'tickvals': [0, 0.025, 0.05], 'ticktext': ['0', 'Fraction', '0.05'], 'tickangle': -90 }, zmin=Z_MIN, zmax=Z_MAX[CONDITIONAL_NORMALIZATION]), layout={ 'xaxis': { 'title': 'fiber densities z-score', 'zeroline': False }, 'yaxis': { 'title': 'Change in fiber<br>density (z-score)', 'zeroline': False }, 'shapes': [{ 'type': 'line', 'x0': X_LABELS_START, 'y0': Y_LABELS_START, 'x1': X_LABELS_END, 'y1': Y_LABELS_START, 'line': { 'color': 'black', 'width': 2 } }, { 'type': 'line', 'x0': X_LABELS_START, 'y0': Y_LABELS_START, 'x1': X_LABELS_START, 'y1': Y_LABELS_END, 'line': { 'color': 'black', 'width': 2 } }] }) save.to_html(_fig=_fig, _path=os.path.join(paths.PLOTS, save.get_module_name()), _filename='plot_direction_' + DIRECTION)
def main(): _simulations = load.structured() _simulations = filtering.by_time_points_amount(_simulations, _time_points=TIME_POINTS) _simulations = filtering.by_categories(_simulations, _is_single_cell=False, _is_heterogeneity=False, _is_low_connectivity=False, _is_causality=False, _is_dominant_passive=False, _is_fibrin=False) _simulations = filtering.by_pair_distance(_simulations, _distance=PAIR_DISTANCE) print('Total simulations:', len(_simulations)) _fiber_densities = compute_fiber_densities(_simulations) _y_arrays = [[] for _i in DERIVATIVES] for _simulation in tqdm(_simulations, desc='Simulations loop'): _normalization = load.normalization(_simulation) for _cell_id in ['left_cell', 'right_cell']: _cell_fiber_densities = _fiber_densities[(_simulation, _cell_id)] _cell_fiber_densities_normalized = compute_lib.z_score_array( _array=_cell_fiber_densities, _average=_normalization['average'], _std=_normalization['std']) for _derivative_index, _derivative in enumerate(DERIVATIVES): _y_arrays[_derivative_index].append( compute_lib.derivative(_cell_fiber_densities_normalized, _n=_derivative)) print('Total cells:', len(_y_arrays[0])) # plot for _derivative, _y_array in zip(DERIVATIVES, _y_arrays): _fig = go.Figure( data=go.Scatter(x=list(range(TIME_POINTS))[::TIME_POINTS_STEP], y=np.mean(_y_array, axis=0)[::TIME_POINTS_STEP], name='Fiber density (z-score)', error_y={ 'type': 'data', 'array': np.std(_y_array, axis=0)[::TIME_POINTS_STEP], 'thickness': 1, 'color': DERIVATIVES_COLORS[_derivative] }, mode='markers', marker={ 'size': 15, 'color': DERIVATIVES_COLORS[_derivative] }, opacity=0.7), layout={ 'xaxis': { 'title': 'Cell contraction (%)', 'zeroline': False }, 'yaxis': { 'title': 'Fiber density (z-score)', 'zeroline': False, 'tickmode': 'array', 'tickvals': DERIVATIVES_Y_TICKVALS[_derivative] }, 'shapes': [{ 'type': 'line', 'x0': -2, 'y0': (np.mean(_y_array, axis=0)[0] - np.std(_y_array, axis=0)[0]) * 1.5, 'x1': 53, 'y1': (np.mean(_y_array, axis=0)[0] - np.std(_y_array, axis=0)[0]) * 1.5, 'line': { 'color': 'black', 'width': 2 } }, { 'type': 'line', 'x0': -2, 'y0': (np.mean(_y_array, axis=0)[0] - np.std(_y_array, axis=0)[0]) * 1.5, 'x1': -2, 'y1': (np.mean(_y_array, axis=0)[-1] + np.std(_y_array, axis=0)[-1]), 'line': { 'color': 'black', 'width': 2 } }] }) save.to_html(_fig=_fig, _path=os.path.join(paths.PLOTS, save.get_module_name()), _filename='plot_derivative_' + str(_derivative))
def main(): # simulations print('Simulations') _simulations = simulations_load.structured() _simulations = simulations_filtering.by_time_points_amount( _simulations, _time_points=SIMULATIONS_TIME_POINTS) _simulations = simulations_filtering.by_categories( _simulations, _is_single_cell=True, _is_heterogeneity=False, _is_low_connectivity=False, _is_causality=False, _is_dominant_passive=False, _is_fibrin=False) print('Total simulations:', len(_simulations)) _fiber_densities = compute_simulations_fiber_densities(_simulations) _simulations_fiber_densities = [[] for _i in range(SIMULATIONS_TIME_POINTS)] for _simulation in tqdm(_simulations, desc='Simulations Loop'): _normalization = simulations_load.normalization(_simulation) for _time_frame in range(SIMULATIONS_TIME_POINTS): _direction_fiber_densities = [] for _direction in ['left', 'right', 'up', 'down']: _fiber_density = _fiber_densities[(_simulation, _direction)][_time_frame] _normalized_fiber_density = compute_lib.z_score( _fiber_density, _normalization['average'], _normalization['std']) _direction_fiber_densities.append(_normalized_fiber_density) _simulations_fiber_densities[_time_frame].append( np.mean(_direction_fiber_densities)) print('Total simulations cells:', len(_simulations_fiber_densities[0])) # experiments print('Experiments') _experiments_fiber_densities = compute_experiments_fiber_densities() # plot _fig = go.Figure( data=[ go.Scatter( x=list(range(SIMULATIONS_TIME_POINTS))[::SIMULATIONS_STEP], y=[np.mean(_array) for _array in _simulations_fiber_densities ][::SIMULATIONS_STEP], name='Simulations', error_y={ 'type': 'data', 'array': [ np.std(_array) for _array in _simulations_fiber_densities ][::SIMULATIONS_STEP], 'thickness': 1, 'color': '#005b96' }, mode='markers', marker={ 'size': 15, 'color': '#005b96' }, opacity=0.7), go.Scatter( x=np.array(range(EXPERIMENTS_TIME_FRAMES)) * EXPERIMENTS_TEMPORAL_RESOLUTION, xaxis='x2', y=[np.mean(_array) for _array in _experiments_fiber_densities], name='Experiments', error_y={ 'type': 'data', 'array': [ np.std(_array) for _array in _experiments_fiber_densities ], 'thickness': 1, 'color': '#ea8500' }, mode='markers', marker={ 'size': 15, 'color': '#ea8500' }, opacity=0.7) ], layout={ 'xaxis': { 'title': 'Cell contraction (%)', 'titlefont': { 'color': '#005b96' }, 'tickfont': { 'color': '#005b96' }, 'zeroline': False }, 'xaxis2': { 'title': 'Time (minutes)', 'titlefont': { 'color': '#ea8500' }, 'tickfont': { 'color': '#ea8500' }, # 'anchor': 'free', 'overlaying': 'x', 'side': 'bottom', 'showgrid': False, 'zeroline': False }, 'yaxis': { 'title': 'Fiber density (z-score)', # 'domain': [0.3, 1], 'range': [-1.7, 14], 'zeroline': False, 'tickmode': 'array', 'tickvals': [0, 4, 8, 12] }, 'legend': { 'xanchor': 'left', 'x': 0.1, 'yanchor': 'top', 'bordercolor': 'black', 'borderwidth': 2 }, 'shapes': [{ 'type': 'line', 'x0': -2, 'y0': -1.5, 'x1': 53, 'y1': -1.5, 'line': { 'color': 'black', 'width': 2 } }, { 'type': 'line', 'x0': -2, 'y0': -1.5, 'x1': -2, 'y1': 14, 'line': { 'color': 'black', 'width': 2 } }] }) save.to_html(_fig=_fig, _path=os.path.join(paths_lib.PLOTS, save.get_module_name()), _filename='plot')