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_array(_low_connectivity): global _simulations, _fiber_densities, _array _simulations = load.structured() _simulations = filtering.by_time_points_amount(_simulations, TIME_POINT[_low_connectivity]) _simulations = filtering.by_categories( _simulations, _is_single_cell=False, _is_heterogeneity=True, _is_low_connectivity=_low_connectivity, _is_causality=False, _is_dominant_passive=False, _is_fibrin=False ) _simulations = filtering.by_heterogeneity(_simulations, _std=STD[_low_connectivity]) _simulations = filtering.by_pair_distance(_simulations, _distance=PAIR_DISTANCE) print('Total simulations:', len(_simulations)) _offsets_y = np.arange(start=OFFSET_Y_START, stop=OFFSET_Y_END + OFFSET_Y_STEP, step=OFFSET_Y_STEP) _arguments = [] for _simulation in _simulations: for _offset_y, _cell_id in product(_offsets_y, ['left_cell', 'right_cell']): _arguments.append({ 'simulation': _simulation, 'length_x': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER, 'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER, 'offset_x': OFFSET_X, 'offset_y': _offset_y, 'cell_id': _cell_id, 'direction': 'inside', 'time_point': TIME_POINT[_low_connectivity] }) _fiber_densities = {} with Pool(CPUS_TO_USE) as _p: for _keys, _value in tqdm( _p.imap_unordered(compute.window_fiber_density_time_point, _arguments), total=len(_arguments), desc='Computing windows & fiber densities'): _fiber_densities[(_keys['simulation'], _keys['offset_y'], _keys['cell_id'])] = _value _p.close() _p.join() _arguments = [] for _offset_y_index, _offset_y in enumerate(_offsets_y): _arguments.append((_offset_y_index, _offset_y)) _array = np.zeros(shape=len(_offsets_y)) with Pool(CPUS_TO_USE) as _p: for _answer in tqdm(_p.imap_unordered(compute_data, _arguments), total=len(_arguments), desc='Computing array'): _offset_y_index, _mean = _answer _array[_offset_y_index] = _mean _p.close() _p.join() return _array
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_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_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) _simulations = filtering.by_pair_distance(_simulations, _distance=PAIR_DISTANCE) print('Total simulations:', len(_simulations)) _fiber_densities = compute_fiber_densities(_simulations) _n = 0 _cells_potential_matches = [] _cells_ranks = [] for _simulation_1 in tqdm(_simulations, desc='Main loop'): for _cell_1_id, _cell_1_correct_match_cell_id in zip( ['left_cell', 'right_cell'], ['right_cell', 'left_cell']): _cell_1 = (_simulation_1, _cell_1_id) _cell_1_correct_match = (_simulation_1, _cell_1_correct_match_cell_id) _cell_1_fiber_densities = _fiber_densities[_cell_1] _cell_1_correlations = [] _cell_1_correct_match_correlation = None for _simulation_2 in _simulations: for _cell_2_id in ['left_cell', 'right_cell']: _cell_2 = (_simulation_2, _cell_2_id) # same cell if _cell_1 == _cell_2: continue _cell_2_fiber_densities = _fiber_densities[_cell_2] _correlation = compute_lib.correlation( compute_lib.derivative(_cell_1_fiber_densities, _n=DERIVATIVE), compute_lib.derivative(_cell_2_fiber_densities, _n=DERIVATIVE)) _cell_1_correlations.append(_correlation) # correct match if _cell_2 == _cell_1_correct_match: _cell_1_correct_match_correlation = _correlation # correct match does not exist if _cell_1_correct_match_correlation is None: continue # check matchmaking _cell_1_total_potential_matches = len(_cell_1_correlations) if _cell_1_total_potential_matches > 1: _cell_1_correct_match_rank = 1 for _potential_match_correlation in sorted( _cell_1_correlations, reverse=True): if _cell_1_correct_match_correlation == _potential_match_correlation: break _cell_1_correct_match_rank += 1 _n += 1 _cells_potential_matches.append( _cell_1_total_potential_matches) _cells_ranks.append(_cell_1_correct_match_rank) # results _mean_cells_potential_matches = float(np.mean(_cells_potential_matches)) _mean_correct_match_probability = 1 / _mean_cells_potential_matches _first_place_correct_matches = sum( [1 for _rank in _cells_ranks if _rank == 1]) _first_place_fraction = _first_place_correct_matches / _n print('Matchmaking results:') print('Total cells:', _n) print('Average potential matches per cell:', round(_mean_cells_potential_matches, 2)) print('Average correct match probability:', round(_mean_correct_match_probability, 2)) print('Fraction of first place correct matches:', round(_first_place_fraction, 2)) # plot _x = list(range(MAX_RANK)) _x_text = [str(_rank + 1) for _rank in _x[:-1]] + [str(MAX_RANK) + '+'] _ranks_sums = [0 for _rank in _x] for _rank in _cells_ranks: if _rank < MAX_RANK: _ranks_sums[_rank - 1] += 1 else: _ranks_sums[-1] += 1 _y = np.array(_ranks_sums) / _n _fig = go.Figure(data=go.Bar(x=_x, y=_y, marker={'color': '#2e82bf'}), layout={ 'xaxis': { 'title': 'Correct match correlation rank', 'zeroline': False, 'tickmode': 'array', 'tickvals': _x, 'ticktext': _x_text }, 'yaxis': { 'title': 'Fraction', 'range': [0, 1.1], 'zeroline': False, 'tickmode': 'array', 'tickvals': [0, 0.5, 1] } }) save.to_html(_fig=_fig, _path=os.path.join(paths.PLOTS, save.get_module_name()), _filename='plot') # correct match probability plot _y = [_mean_correct_match_probability] * (MAX_RANK - 1) + [ _mean_correct_match_probability * MAX_RANK ] _fig = go.Figure(data=go.Bar(x=_x, y=_y, marker={'color': '#2e82bf'}), layout={ 'xaxis': { 'title': 'Correct match correlation rank', 'zeroline': False, 'tickmode': 'array', 'tickvals': _x, 'ticktext': _x_text }, 'yaxis': { 'title': 'Fraction', 'range': [0, 1.1], 'zeroline': False, 'tickmode': 'array', 'tickvals': [0, 0.5, 1] } }) save.to_html(_fig=_fig, _path=os.path.join(paths.PLOTS, save.get_module_name()), _filename='plot_correct_match_prob')
def process_all_simulations(_overwrite=False): process_simulations(load.structured(), _overwrite)
def main(): _simulations = load.structured() _simulations = filtering.by_time_points_amount( _simulations, _time_points=SIMULATIONS_TIME_POINTS) _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) _simulations = filtering.by_pair_distances(_simulations, _distances=PAIR_DISTANCE) print('Total simulations:', len(_simulations)) _fiber_densities = compute_fiber_densities(_simulations) _x_arrays = [[] for _i in PAIR_DISTANCE] _y_arrays = [[] for _i in PAIR_DISTANCE] for _distance_index, _distance in enumerate(PAIR_DISTANCE): _distance_simulations = filtering.by_pair_distance(_simulations, _distance=_distance) print('Distance:', _distance, 'Total simulations:', len(_distance_simulations)) for _simulation in tqdm(_distance_simulations, desc='Simulations loop'): for _direction in ['inside', 'outside']: _left_cell_fiber_densities = _fiber_densities[(_simulation, 'left_cell', _direction)] _right_cell_fiber_densities = _fiber_densities[(_simulation, 'right_cell', _direction)] _correlation = compute_lib.correlation( compute_lib.derivative(_left_cell_fiber_densities, _n=DERIVATIVE), compute_lib.derivative(_right_cell_fiber_densities, _n=DERIVATIVE)) if _direction == 'inside': _x_arrays[_distance_index].append(_correlation) else: _y_arrays[_distance_index].append(_correlation) print('Wilcoxon of insides minus outsides around the zero:') print( wilcoxon( np.array(_x_arrays[_distance_index]) - np.array(_y_arrays[_distance_index]))) # 2d plots _colors_array = config.colors(4) _legendgroup_array = ['group_1', 'group_1', 'group_2', 'group_2'] _fig = go.Figure(data=[ go.Scatter(x=_x, y=_y, name='Dist. ' + str(_distance), mode='markers', marker={ 'size': 10, 'color': _color }, legendgroup=_legendgroup) for _x, _y, _distance, _color, _legendgroup in zip( _x_arrays, _y_arrays, PAIR_DISTANCE, _colors_array, _legendgroup_array) ], layout={ 'xaxis': { 'title': 'Inner correlation', 'zeroline': False, 'range': [-1.1, 1.2], 'tickmode': 'array', 'tickvals': [-1, -0.5, 0, 0.5, 1] }, 'yaxis': { 'title': 'Outer correlation', 'zeroline': False, 'range': [-1.1, 1.2], 'tickmode': 'array', 'tickvals': [-1, -0.5, 0, 0.5, 1] }, 'legend': { 'x': 0.1, 'y': 1, 'xanchor': 'left', 'yanchor': 'top', 'bordercolor': 'black', 'borderwidth': 2, 'bgcolor': 'white', 'orientation': 'h' }, '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 } }, { 'type': 'line', 'x0': -1, 'y0': -1, 'x1': 1, 'y1': 1, 'line': { 'color': 'red', 'width': 2 } }] }) save.to_html(_fig=_fig, _path=os.path.join(paths.PLOTS, save.get_module_name()), _filename='plot') for _x, _y, _distance, _color in zip(_x_arrays, _y_arrays, PAIR_DISTANCE, _colors_array): _fig = go.Figure(data=go.Scatter(x=_x, y=_y, name='Dist. ' + str(_distance), mode='markers', marker={ 'size': 10, 'color': _color }, showlegend=True), layout={ 'xaxis': { 'title': 'Inner correlation', 'zeroline': False, 'range': [-1.1, 1.2], 'tickmode': 'array', 'tickvals': [-1, -0.5, 0, 0.5, 1] }, 'yaxis': { 'title': 'Outer correlation', 'zeroline': False, 'range': [-1.1, 1.2], 'tickmode': 'array', 'tickvals': [-1, -0.5, 0, 0.5, 1] }, 'legend': { 'xanchor': 'left', 'x': 0.1, 'yanchor': 'top', 'bordercolor': 'black', 'borderwidth': 2, 'bgcolor': 'white' }, '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 } }, { 'type': 'line', 'x0': -1, 'y0': -1, 'x1': 1, 'y1': 1, 'line': { 'color': 'red', 'width': 2 } }] }) save.to_html(_fig=_fig, _path=os.path.join(paths.PLOTS, save.get_module_name()), _filename='plot_distance_' + str(_distance)) # box plot _box_y_arrays = [[] for _i in PAIR_DISTANCE] for _x_array, _y_array, _distance_index in zip(_x_arrays, _y_arrays, range(len(PAIR_DISTANCE))): for _x, _y in zip(_x_array, _y_array): _point_distance = compute_lib.distance_from_a_point_to_a_line( _line=[-1, -1, 1, 1], _point=[_x, _y]) if _x > _y: _box_y_arrays[_distance_index].append(_point_distance) else: _box_y_arrays[_distance_index].append(-_point_distance) _fig = go.Figure(data=[ go.Box(y=_y, name=str(_distance), boxpoints='all', jitter=1, pointpos=0, line={'width': 1}, fillcolor='white', marker={ 'size': 10, 'color': _color }, showlegend=False) for _y, _distance, _color in zip( _box_y_arrays, PAIR_DISTANCE, _colors_array) ], layout={ 'xaxis': { 'title': 'Pair distance (cell diameter)', 'zeroline': False, 'tickmode': 'array', 'tickvals': PAIR_DISTANCE, 'type': 'category' }, 'yaxis': { 'title': 'Inner minus outer correlation', 'zeroline': False, 'tickmode': 'array', 'tickvals': [-0.2, 0, 0.2, 0.4] } }) save.to_html(_fig=_fig, _path=os.path.join(paths.PLOTS, save.get_module_name()), _filename='plot_box')
def compute_fiber_densities(_alpha=1, _beta=1, _low_connectivity=False): _simulations = load.structured() _simulations = filtering.by_time_points_amount( _simulations, TIME_POINT[_low_connectivity]) _simulations = filtering.by_categories( _simulations, _is_single_cell=False, _is_heterogeneity=True, _is_low_connectivity=_low_connectivity, _is_causality=True, _is_dominant_passive=False, _is_fibrin=False) _simulations = filtering.by_causality(_simulations, _alpha=_alpha, _beta=_beta) _simulations = filtering.by_pair_distance(_simulations, _distance=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': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER, 'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER, 'offset_x': OFFSET_X, 'offset_y': OFFSET_Y, 'cell_id': _cell_id, 'direction': 'inside', 'time_points': TIME_POINT[_low_connectivity] }) _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() _same_correlation_vs_time_lag = {} _same_time_lags_arrays = [[] for _i in TIME_LAGS] _different_time_lags_arrays = [[] for _i in TIME_LAGS] _same_time_lags_highest = [0 for _i in TIME_LAGS] _different_time_lags_highest = [0 for _i in TIME_LAGS] for _same_index in tqdm(range(len(_simulations)), desc='Main loop'): _same_simulation = _simulations[_same_index] _same_left_cell_fiber_densities = _fiber_densities[(_same_simulation, 'left_cell')] _same_right_cell_fiber_densities = _fiber_densities[(_same_simulation, 'right_cell')] # time lag _same_highest_correlation = -1.1 _same_highest_correlation_time_lag_index = 0 _same_correlation_vs_time_lag[_same_simulation] = [] for _time_lag_index, _time_lag in enumerate(TIME_LAGS): if _time_lag > 0: _same_left_cell_fiber_densities_time_lag = _same_left_cell_fiber_densities[: -_time_lag] _same_right_cell_fiber_densities_time_lag = _same_right_cell_fiber_densities[ _time_lag:] elif _time_lag < 0: _same_left_cell_fiber_densities_time_lag = _same_left_cell_fiber_densities[ -_time_lag:] _same_right_cell_fiber_densities_time_lag = _same_right_cell_fiber_densities[: _time_lag] else: _same_left_cell_fiber_densities_time_lag = _same_left_cell_fiber_densities _same_right_cell_fiber_densities_time_lag = _same_right_cell_fiber_densities _same_correlation = compute_lib.correlation( compute_lib.derivative( _same_left_cell_fiber_densities_time_lag, _n=DERIVATIVE), compute_lib.derivative( _same_right_cell_fiber_densities_time_lag, _n=DERIVATIVE)) _same_time_lags_arrays[_time_lag_index].append(_same_correlation) _same_correlation_vs_time_lag[_same_simulation].append( _same_correlation) if _same_correlation > _same_highest_correlation: _same_highest_correlation = _same_correlation _same_highest_correlation_time_lag_index = _time_lag_index _same_time_lags_highest[_same_highest_correlation_time_lag_index] += 1 for _different_index in range(len(_simulations)): if _same_index != _different_index: _different_simulation = _simulations[_different_index] for _same_cell_id, _different_cell_id in product( ['left_cell', 'right_cell'], ['left_cell', 'right_cell']): _same_fiber_densities = \ _fiber_densities[(_same_simulation, _same_cell_id)] _different_fiber_densities = \ _fiber_densities[(_different_simulation, _different_cell_id)] # time lag _different_highest_correlation = -1.1 _different_highest_correlation_time_lag_index = 0 for _time_lag_index, _time_lag in enumerate(TIME_LAGS): if _time_lag > 0: _same_fiber_densities_time_lag = _same_fiber_densities[: -_time_lag] _different_fiber_densities_time_lag = _different_fiber_densities[ _time_lag:] elif _time_lag < 0: _same_fiber_densities_time_lag = _same_fiber_densities[ -_time_lag:] _different_fiber_densities_time_lag = _different_fiber_densities[: _time_lag] else: _same_fiber_densities_time_lag = _same_fiber_densities _different_fiber_densities_time_lag = _different_fiber_densities _different_correlation = compute_lib.correlation( compute_lib.derivative( _same_fiber_densities_time_lag, _n=DERIVATIVE), compute_lib.derivative( _different_fiber_densities_time_lag, _n=DERIVATIVE)) _different_time_lags_arrays[_time_lag_index].append( _different_correlation) if _different_correlation > _different_highest_correlation: _different_highest_correlation = _different_correlation _different_highest_correlation_time_lag_index = _time_lag_index _different_time_lags_highest[ _different_highest_correlation_time_lag_index] += 1 return _same_correlation_vs_time_lag, _same_time_lags_arrays, _different_time_lags_arrays, \ _same_time_lags_highest, _different_time_lags_highest
def main(_directions=None): if _directions is None: _directions = ['inside', 'outside'] _simulations = load.structured() _simulations = filtering.by_time_points_amount( _simulations, _time_points=SIMULATIONS_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, _directions) for _direction in _directions: _y_arrays = [[] for _i in DERIVATIVES] for _simulation in tqdm(_simulations, desc='Simulations loop'): _left_cell_fiber_densities = _fiber_densities[(_simulation, 'left_cell', _direction)] _right_cell_fiber_densities = _fiber_densities[(_simulation, 'right_cell', _direction)] for _derivative_index, _derivative in enumerate(DERIVATIVES): _y_arrays[_derivative_index].append( compute_lib.correlation( compute_lib.derivative(_left_cell_fiber_densities, _n=_derivative), compute_lib.derivative(_right_cell_fiber_densities, _n=_derivative))) print('Direction:', _direction) print('Total pairs:', len(_y_arrays[0])) print('Wilcoxon around the zero') for _y_array, _derivative in zip(_y_arrays, DERIVATIVES): print('Derivative:', _derivative, wilcoxon(_y_array)) # plot _y_title = 'Inner correlation' if _direction == 'inside' else 'Outer correlation' _colors_array = config.colors(3) _fig = go.Figure(data=[ go.Box(y=_y, name=_derivative, boxpoints='all', jitter=1, pointpos=0, line={'width': 1}, fillcolor='white', marker={ 'size': 10, 'color': _color }, opacity=0.7, showlegend=False) for _y, _derivative, _color in zip( _y_arrays, DERIVATIVES_TEXT, _colors_array) ], layout={ 'xaxis': { 'title': 'Fiber density derivative', 'zeroline': False }, 'yaxis': { 'title': _y_title, 'range': [-1, 1], 'zeroline': False, 'tickmode': 'array', 'tickvals': [-1, -0.5, 0, 0.5, 1] } }) save.to_html(_fig=_fig, _path=os.path.join(paths.PLOTS, save.get_module_name()), _filename='plot_direction_' + _direction)
def main(): # TODO: add support for the new fibrin simulations _simulations = load.structured() # single cell print('Single cells') _single_cell_simulations = filtering.by_categories( _simulations, _is_single_cell=True, _is_heterogeneity=None, _is_low_connectivity=False, _is_causality=False, _is_dominant_passive=False) print('\tTotal single cell simulations:', len(_single_cell_simulations)) # no heterogeneity _no_heterogeneity_single_cell_simulations = filtering.by_categories( _simulations, _is_single_cell=True, _is_heterogeneity=False, _is_low_connectivity=False, _is_causality=False, _is_dominant_passive=False) print('\tTotal single cell no heterogeneity:', len(_no_heterogeneity_single_cell_simulations)) # heterogeneity _heterogeneity_single_cell_simulations = filtering.by_categories( _simulations, _is_single_cell=True, _is_heterogeneity=True, _is_low_connectivity=False, _is_causality=False, _is_dominant_passive=False) print('\tTotal single cell heterogeneity:', len(_heterogeneity_single_cell_simulations)) for _std in STDS: _std_simulations = filtering.by_heterogeneity( _heterogeneity_single_cell_simulations, _std=_std) print('\t\tTotal with std. ' + str(_std) + ':', len(_std_simulations)) # cell pairs _cell_pairs_simulations = filtering.by_categories( _simulations, _is_single_cell=False, _is_heterogeneity=None, _is_low_connectivity=False, _is_causality=None, _is_dominant_passive=False) for _distance in DISTANCES: print('\nCell pairs distance:', _distance) # total _distance_simulations = filtering.by_pair_distance( _cell_pairs_simulations, _distance=_distance) print('\tTotal cell pairs simulations:', len(_distance_simulations)) # no heterogeneity _no_heterogeneity_simulations = filtering.by_categories( _distance_simulations, _is_single_cell=False, _is_heterogeneity=False, _is_low_connectivity=False, _is_causality=False, _is_dominant_passive=False) print( '\tTotal cell pairs no heterogeneity (no causality, no dominant):', len(_no_heterogeneity_simulations)) # heterogeneity without causality _filtered_simulations = filtering.by_categories( _distance_simulations, _is_single_cell=False, _is_heterogeneity=True, _is_low_connectivity=False, _is_causality=False, _is_dominant_passive=False) print('\tTotal cell pairs heterogeneity (no causality, no dominant):', len(_filtered_simulations)) for _std in STDS: _std_simulations = filtering.by_heterogeneity( _filtered_simulations, _std=_std) if len(_std_simulations) > 0: print('\t\tTotal with std. ' + str(_std) + ':', len(_std_simulations)) # heterogeneity with causality _filtered_simulations = filtering.by_categories( _distance_simulations, _is_single_cell=False, _is_heterogeneity=True, _is_low_connectivity=False, _is_causality=True, _is_dominant_passive=False) if len(_filtered_simulations) > 0: print('\tTotal cell pairs heterogeneity (causality):', len(_filtered_simulations)) for _alpha, _beta in product(ALPHAS, BETAS): if _alpha != 0 or (_alpha == 0 and _beta == 1): _causality_simulations = filtering.by_causality( _filtered_simulations, _alpha=_alpha, _beta=_beta) if len(_causality_simulations) > 0: print( '\t\tTotal with alpha = ' + str(_alpha) + ', beta = ' + str(_beta) + ':', len(_causality_simulations))
def main(_low_connectivity=False): _simulations = load.structured() _simulations = filtering.by_time_points_amount( _simulations, TIME_POINT[_low_connectivity]) _simulations = filtering.by_categories( _simulations, _is_single_cell=False, _is_heterogeneity=True, _is_low_connectivity=_low_connectivity, _is_causality=False, _is_dominant_passive=False, _is_fibrin=False) _simulations = filtering.by_heterogeneity(_simulations, _std=STD) _simulations = filtering.by_pair_distances(_simulations, _distances=PAIR_DISTANCE) print('Total simulations:', len(_simulations)) _fiber_densities = compute_fiber_densities(_simulations, _low_connectivity) _y_arrays = [[] for _i in PAIR_DISTANCE] for _distance_index, _distance in enumerate(PAIR_DISTANCE): _distance_simulations = filtering.by_pair_distance(_simulations, _distance=_distance) print('Distance:', _distance, 'Total simulations:', len(_distance_simulations)) _higher_same_counter = 0 for _same_index in tqdm(range(len(_distance_simulations)), desc='Main loop'): _same_simulation = _distance_simulations[_same_index] _same_left_cell_fiber_densities = _fiber_densities[( _same_simulation, 'left_cell')] _same_right_cell_fiber_densities = _fiber_densities[( _same_simulation, 'right_cell')] _same_correlation = compute_lib.correlation( compute_lib.derivative(_same_left_cell_fiber_densities, _n=DERIVATIVE), compute_lib.derivative(_same_right_cell_fiber_densities, _n=DERIVATIVE)) for _different_index in range(len(_distance_simulations)): if _same_index != _different_index: _different_simulation = _distance_simulations[ _different_index] for _same_cell_id, _different_cell_id in product( ['left_cell', 'right_cell'], ['left_cell', 'right_cell']): _same_fiber_densities = \ _fiber_densities[(_same_simulation, _same_cell_id)] _different_fiber_densities = \ _fiber_densities[(_different_simulation, _different_cell_id)] _different_correlation = compute_lib.correlation( compute_lib.derivative(_same_fiber_densities, _n=DERIVATIVE), compute_lib.derivative(_different_fiber_densities, _n=DERIVATIVE)) _point_distance = compute_lib.distance_from_a_point_to_a_line( _line=[-1, -1, 1, 1], _point=[_same_correlation, _different_correlation]) if _same_correlation > _different_correlation: _y_arrays[_distance_index].append(_point_distance) _higher_same_counter += 1 else: _y_arrays[_distance_index].append(-_point_distance) print('Total points:', len(_y_arrays[_distance_index])) print('Wilcoxon around the zero:') print(wilcoxon(_y_arrays[_distance_index])) print('Higher same amount:', _higher_same_counter / len(_y_arrays[_distance_index])) # plot _colors_array = config.colors(4) _fig = go.Figure(data=[ go.Box(y=_y, name=str(_distance), boxpoints=False, line={'width': 1}, marker={ 'size': 10, 'color': _color }, showlegend=False) for _y, _distance, _color in zip( _y_arrays, PAIR_DISTANCE, _colors_array) ], layout={ 'xaxis': { 'title': 'Pair distance (cell diameter)', 'zeroline': False, 'tickmode': 'array', 'tickvals': PAIR_DISTANCE, 'type': 'category' }, 'yaxis': { 'title': 'Same minus different correlation', 'range': [-1, 1.1], 'zeroline': False, 'tickmode': 'array', 'tickvals': [-1, -0.5, 0, 0.5, 1] } }) save.to_html(_fig=_fig, _path=os.path.join(paths.PLOTS, save.get_module_name()), _filename='plot')
def main(_low_connectivity=False): _simulations = load.structured() _simulations = filtering.by_time_points_amount( _simulations, TIME_POINT[_low_connectivity]) _simulations = filtering.by_categories( _simulations, _is_single_cell=False, _is_heterogeneity=True, _is_low_connectivity=_low_connectivity, _is_causality=False, _is_dominant_passive=False, _is_fibrin=False) _simulations = filtering.by_heterogeneity(_simulations, _std=STD[_low_connectivity]) _simulations = filtering.by_pair_distances(_simulations, _distances=PAIR_DISTANCE) print('Total simulations:', len(_simulations)) _fiber_densities = compute_fiber_densities(_simulations, _low_connectivity) _same_correlations_array = [] _different_correlations_array = [] for _same_index in tqdm(range(len(_simulations)), desc='Main loop'): _same_simulation = _simulations[_same_index] _same_left_cell_fiber_densities = _fiber_densities[(_same_simulation, 'left_cell')] _same_right_cell_fiber_densities = _fiber_densities[(_same_simulation, 'right_cell')] _same_correlation = compute_lib.correlation( compute_lib.derivative(_same_left_cell_fiber_densities, _n=DERIVATIVE), compute_lib.derivative(_same_right_cell_fiber_densities, _n=DERIVATIVE)) for _different_index in range(len(_simulations)): if _same_index != _different_index: _different_simulation = _simulations[_different_index] for _same_cell_id, _different_cell_id in product( ['left_cell', 'right_cell'], ['left_cell', 'right_cell']): _same_fiber_densities = \ _fiber_densities[(_same_simulation, _same_cell_id)] _different_fiber_densities = \ _fiber_densities[(_different_simulation, _different_cell_id)] _different_correlations_array.append( compute_lib.correlation( compute_lib.derivative(_same_fiber_densities, _n=DERIVATIVE), compute_lib.derivative(_different_fiber_densities, _n=DERIVATIVE))) _same_correlations_array.append(_same_correlation) print('Total points:', len(_same_correlations_array)) _same_minus_different = \ np.array(_same_correlations_array) - np.array(_different_correlations_array) print('Wilcoxon of same minus different around the zero:') print(wilcoxon(_same_minus_different)) print('Higher same amount:', (_same_minus_different > 0).sum() / len(_same_minus_different)) # plot _fig = go.Figure(data=go.Scatter(x=_same_correlations_array, y=_different_correlations_array, mode='markers', marker={ 'size': 5, 'color': '#2e82bf' }, showlegend=False), layout={ 'xaxis': { 'title': 'Same network correlation', 'zeroline': False, 'range': [-1.1, 1.2], 'tickmode': 'array', 'tickvals': [-1, -0.5, 0, 0.5, 1] }, 'yaxis': { 'title': 'Different network 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 } }, { 'type': 'line', 'x0': -1, 'y0': -1, 'x1': 1, 'y1': 1, 'line': { 'color': 'red', 'width': 2 } }] }) save.to_html(_fig=_fig, _path=os.path.join(paths.PLOTS, save.get_module_name()), _filename='plot')
def main(): _simulations = load.structured() _simulations = filtering.by_time_points_amount(_simulations, _time_points=TIME_POINTS) _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) _y_arrays = [[] for _i in DERIVATIVES] for _index_1 in tqdm(range(len(_simulations)), desc='Simulations loop'): _simulation_1 = _simulations[_index_1] _cell_1_fiber_densities = \ [_fiber_densities[(_simulation_1, _direction)] for _direction in ['left', 'right', 'up', 'down']] _cell_1_fiber_densities = np.mean(_cell_1_fiber_densities, axis=0) for _index_2 in range(_index_1 + 1, len(_simulations)): _simulation_2 = _simulations[_index_2] _cell_2_fiber_densities = \ [_fiber_densities[(_simulation_2, _direction)] for _direction in ['left', 'right', 'up', 'down']] _cell_2_fiber_densities = np.mean(_cell_2_fiber_densities, axis=0) for _derivative_index, _derivative in enumerate(DERIVATIVES): _y_arrays[_derivative_index].append( compute_lib.correlation( compute_lib.derivative(_cell_1_fiber_densities, _n=_derivative), compute_lib.derivative(_cell_2_fiber_densities, _n=_derivative))) print('Total points:', len(_y_arrays[0])) print('Wilcoxon around the zero') for _y_array, _derivative in zip(_y_arrays, DERIVATIVES): print('Derivative:', _derivative, wilcoxon(_y_array)) # plot _colors_array = config.colors(3) _fig = go.Figure(data=[ go.Box(y=_y, name=_derivative, boxpoints='all', jitter=1, pointpos=0, line={'width': 1}, fillcolor='white', marker={ 'size': 10, 'color': _color }, opacity=0.7, showlegend=False) for _y, _derivative, _color in zip( _y_arrays, DERIVATIVES_TEXT, _colors_array) ], layout={ 'xaxis': { 'title': 'Fiber density derivative', 'zeroline': False }, 'yaxis': { 'title': 'Correlation', 'range': [-1, 1], 'zeroline': False, 'tickmode': 'array', 'tickvals': [-1, -0.5, 0, 0.5, 1] } }) save.to_html(_fig=_fig, _path=os.path.join(paths.PLOTS, save.get_module_name()), _filename='plot')
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')
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 = load.structured() _simulations = filtering.by_time_points_amount(_simulations, TIME_POINT) _simulations = filtering.by_categories( _simulations, _is_single_cell=False, _is_heterogeneity=None, _is_low_connectivity=False, _is_causality=False, _is_dominant_passive=False, _is_fibrin=False ) _simulations = filtering.by_heterogeneities(_simulations, _stds=STDS) _simulations = filtering.by_pair_distance(_simulations, _distance=PAIR_DISTANCE) print('Total simulations:', len(_simulations)) _fiber_densities = compute_fiber_densities(_simulations) # stds loop _std_communicating = [[] for _i in STDS] _std_non_communicating = [[] for _i in STDS] for _std_index, _std in enumerate(STDS): print('Std:', _std) _simulations_std = filtering.by_heterogeneity(_simulations, _std=_std) print('Total simulations:', len(_simulations_std)) # communicating loop for _simulation in tqdm(_simulations_std, desc='Communicating pairs loop'): _left_cell_fiber_densities = _fiber_densities[(_simulation, 'left_cell')] _right_cell_fiber_densities = _fiber_densities[(_simulation, 'right_cell')] _correlation = compute_lib.correlation( compute_lib.derivative(_left_cell_fiber_densities, _n=DERIVATIVE), compute_lib.derivative(_right_cell_fiber_densities, _n=DERIVATIVE) ) _std_communicating[_std_index].append(_correlation) # non-communicating loop _simulations_indices = range(len(_simulations_std)) for _simulation_1_index in tqdm(_simulations_indices, desc='Non-communicating pairs loop'): _simulation_1 = _simulations_std[_simulation_1_index] for _simulation_2_index in _simulations_indices[_simulation_1_index + 1:]: _simulation_2 = _simulations_std[_simulation_2_index] for _simulation_1_cell_id, _simulation_2_cell_id in product(['left_cell', 'right_cell'], ['left_cell', 'right_cell']): _simulation_1_fiber_densities = _fiber_densities[(_simulation_1, _simulation_1_cell_id)] _simulation_2_fiber_densities = _fiber_densities[(_simulation_2, _simulation_2_cell_id)] _correlation = compute_lib.correlation( compute_lib.derivative(_simulation_1_fiber_densities, _n=DERIVATIVE), compute_lib.derivative(_simulation_2_fiber_densities, _n=DERIVATIVE) ) _std_non_communicating[_std_index].append(_correlation) print('Wilcoxon rank-sum test:', ranksums(_std_communicating[_std_index], _std_non_communicating[_std_index])) # histogram plot, once with and once without the legend _colors_array = config.colors(len(STDS)) for _show_legend in [True, False]: _fig = go.Figure( data=[ *[ go.Histogram( x=_non_communicating, name='STD ' + str(_std), marker={ 'color': _color }, nbinsx=10, histnorm='probability', showlegend=_show_legend ) for _std, _non_communicating, _color in zip(STDS, _std_non_communicating, _colors_array) ], *[ go.Scatter( x=_communicating, y=[0.5 - 0.02 * _std_index for _i in range(len(_communicating))], mode='text', text='●', textfont={ 'color': _color, 'size': 15 }, showlegend=False ) for _std_index, _communicating, _color in zip(range(len(STDS)), _std_communicating, _colors_array) ] ], layout={ 'xaxis': { 'title': 'Correlation', 'zeroline': False }, 'yaxis': { 'title': 'Fraction', 'zeroline': False }, 'legend': { 'xanchor': 'right', 'yanchor': 'top', 'bordercolor': 'black', 'borderwidth': 2 }, 'bargap': 0.1 } ) save.to_html( _fig=_fig, _path=os.path.join(paths.PLOTS, save.get_module_name()), _filename='plot_legend_' + str(_show_legend) )
def main(): _simulations = load.structured() _simulations = filtering.by_time_points_amount(_simulations, TIME_POINTS) _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_pair_distance(_simulations, _distance=PAIR_DISTANCE) _simulations = filtering.by_heterogeneity(_simulations, _std=STD) print('Total simulations:', len(_simulations)) _fiber_densities = compute_fiber_densities(_simulations) _window_distances_communicating = [[] for _i in OFFSETS_X] _window_distances_non_communicating = [[] for _i in OFFSETS_X] # window distances loop for _window_distance_index, _window_distance in enumerate(OFFSETS_X): print('Window distance:', _window_distance) # communicating loop for _simulation in tqdm(_simulations, desc='Communicating loop'): _left_cell_fiber_densities = _fiber_densities[(_simulation, _window_distance, 'left_cell')] _right_cell_fiber_densities = _fiber_densities[(_simulation, _window_distance, 'right_cell')] _correlation = compute_lib.correlation( compute_lib.derivative(_left_cell_fiber_densities, _n=DERIVATIVE), compute_lib.derivative(_right_cell_fiber_densities, _n=DERIVATIVE) ) _window_distances_communicating[_window_distance_index].append(_correlation) # non-communicating loop _simulations_indices = range(len(_simulations)) for _simulation_1_index in tqdm(_simulations_indices, desc='Non-communicating pairs loop'): _simulation_1 = _simulations[_simulation_1_index] for _simulation_2_index in _simulations_indices[_simulation_1_index + 1:]: _simulation_2 = _simulations[_simulation_2_index] for _simulation_1_cell_id, _simulation_2_cell_id in product(['left_cell', 'right_cell'], ['left_cell', 'right_cell']): _simulation_1_fiber_densities = \ _fiber_densities[(_simulation_1, _window_distance, _simulation_1_cell_id)] _simulation_2_fiber_densities = \ _fiber_densities[(_simulation_2, _window_distance, _simulation_2_cell_id)] _correlation = compute_lib.correlation( compute_lib.derivative(_simulation_1_fiber_densities, _n=DERIVATIVE), compute_lib.derivative(_simulation_2_fiber_densities, _n=DERIVATIVE) ) _window_distances_non_communicating[_window_distance_index].append(_correlation) # rank sums print('Wilcoxon rank-sum tests between communicating and non-communicating:', ranksums(_window_distances_communicating[_window_distance_index], _window_distances_non_communicating[_window_distance_index])) # plot _data = [] _colors_array = config.colors(2) for _communicating, _communicating_text, _pair_distances, _color in \ zip([True, False], ['Communicating', 'Non-communicating'], [_window_distances_communicating, _window_distances_non_communicating], _colors_array): _y = [] _x = [] for _window_distance_index, _window_distance in enumerate(OFFSETS_X): _y += _pair_distances[_window_distance_index] _x += [_window_distance for _i in _pair_distances[_window_distance_index]] _data.append( go.Box( y=_y, x=_x, name=_communicating_text, boxpoints='all' if _communicating else False, jitter=1, pointpos=0, line={ 'width': 1 }, fillcolor='white', marker={ 'size': 10, 'color': _color }, opacity=0.7 ) ) _fig = go.Figure( data=_data, layout={ 'xaxis': { 'title': 'Window distance (cell diameter)', 'zeroline': False, 'tickmode': 'array', 'tickvals': OFFSETS_X, 'type': 'category' }, 'yaxis': { 'title': 'Correlation', 'range': [-1, 1], 'zeroline': False, 'tickmode': 'array', 'tickvals': [-1, -0.5, 0, 0.5, 1] }, 'boxmode': 'group', 'legend': { 'xanchor': 'right', 'yanchor': 'top', 'bordercolor': 'black', 'borderwidth': 2 } } ) save.to_html( _fig=_fig, _path=os.path.join(paths.PLOTS, save.get_module_name()), _filename='plot' )
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_time_points_amount( _simulations, _time_points=SIMULATIONS_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) _kpss_y_arrays = [[] for _i in DERIVATIVES] _adf_y_arrays = [[] for _i in DERIVATIVES] for _simulation in tqdm(_simulations, desc='Simulations loop'): for _cell_id in ['left_cell', 'right_cell']: _cell_fiber_densities = _fiber_densities[(_simulation, _cell_id)] for _derivative_index, _derivative in enumerate(DERIVATIVES): _cell_fiber_densities_derivative = compute_lib.derivative( _cell_fiber_densities, _n=_derivative) with warnings.catch_warnings(): warnings.simplefilter('ignore', category=InterpolationWarning) _, _kpss_p_value, _, _ = kpss( _cell_fiber_densities_derivative, nlags='legacy') _kpss_y_arrays[_derivative_index].append(_kpss_p_value) _, _adf_p_value, _, _, _, _ = adfuller( _cell_fiber_densities_derivative) _adf_y_arrays[_derivative_index].append(_adf_p_value) # print results print('KPSS:') for _derivative_index, _derivative in enumerate(DERIVATIVES): _stationary_count = len([ _value for _value in _kpss_y_arrays[_derivative_index] if _value > 0.05 ]) print( 'Derivative:', _derivative, 'Stationary:', str(_stationary_count / len(_kpss_y_arrays[_derivative_index]) * 100) + '%') print('ADF:') for _derivative_index, _derivative in enumerate(DERIVATIVES): _stationary_count = len([ _value for _value in _adf_y_arrays[_derivative_index] if _value < 0.05 ]) print( 'Derivative:', _derivative, 'Stationary:', str(_stationary_count / len(_adf_y_arrays[_derivative_index]) * 100) + '%') # plot _colors_array = config.colors(3) for _test_name, _y_title, _y_tickvals, _p_value_line, _y_arrays in \ zip( ['kpss', 'adf'], ['KPSS test p-value', 'ADF test p-value'], [[0.05, 0.1], [0.05, 1]], [0.05, 0.05], [_kpss_y_arrays, _adf_y_arrays] ): _fig = go.Figure(data=[ go.Box(y=_y, name=_derivative, boxpoints='all', jitter=1, pointpos=0, line={'width': 1}, fillcolor='white', marker={ 'size': 10, 'color': _color }, opacity=0.7, showlegend=False) for _y, _derivative, _color in zip( _y_arrays, DERIVATIVES_TEXT, _colors_array) ], layout={ 'xaxis': { 'title': 'Fiber density derivative', 'zeroline': False }, 'yaxis': { 'title': _y_title, 'zeroline': False, 'tickmode': 'array', 'tickvals': _y_tickvals }, 'shapes': [{ 'type': 'line', 'x0': DERIVATIVES[0] - 0.75, 'y0': _p_value_line, 'x1': DERIVATIVES[-1] + 0.75, 'y1': _p_value_line, 'line': { 'color': 'red', 'width': 2, 'dash': 'dash' } }] }) save.to_html(_fig=_fig, _path=os.path.join(paths.PLOTS, save.get_module_name()), _filename='plot_' + _test_name)
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')