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 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_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')
Ejemplo n.º 4
0
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')
Ejemplo n.º 5
0
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')
Ejemplo n.º 7
0
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)
        )
Ejemplo n.º 8
0
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_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')