Exemplo n.º 1
0
def compute_data(_tuples, _arguments, _padding_y_by, _padding_z_by, _space_y_by, _space_z_by):
    _windows_dictionary, _windows_to_compute = \
        compute.windows(_arguments, _keys=['experiment', 'series_id', 'group', 'cell_id'])
    _fiber_densities = compute.fiber_densities(_windows_to_compute, _subtract_border=True, _padding_y_by=_padding_y_by,
                                               _padding_z_by=_padding_z_by, _space_y_by=_space_y_by,
                                               _space_z_by=_space_z_by)

    _experiments_fiber_densities = {
        _key: [_fiber_densities[_tuple] for _tuple in _windows_dictionary[_key]]
        for _key in _windows_dictionary
    }

    _correlations = []
    for _tuple in _tuples:
        _experiment, _series, _group = _tuple

        _left_cell_fiber_densities = _experiments_fiber_densities[(_experiment, _series, _group, 'left_cell')]
        _right_cell_fiber_densities = _experiments_fiber_densities[(_experiment, _series, _group, 'right_cell')]

        _properties = load.group_properties(_experiment, _series, _group)
        _left_cell_fiber_densities = compute.remove_blacklist(
            _experiment,
            _series,
            _properties['cells_ids']['left_cell'],
            _left_cell_fiber_densities
        )
        _right_cell_fiber_densities = compute.remove_blacklist(
            _experiment,
            _series,
            _properties['cells_ids']['right_cell'],
            _right_cell_fiber_densities
        )

        _left_cell_fiber_densities_filtered, _right_cell_fiber_densities_filtered = \
            compute.longest_same_indices_shared_in_borders_sub_array(
                _left_cell_fiber_densities, _right_cell_fiber_densities
            )

        # ignore small arrays
        if len(_left_cell_fiber_densities_filtered) < compute.minimum_time_frames_for_correlation(_experiment):
            continue

        _correlation = compute_lib.correlation(
            compute_lib.derivative(_left_cell_fiber_densities_filtered, _n=DERIVATIVE),
            compute_lib.derivative(_right_cell_fiber_densities_filtered, _n=DERIVATIVE)
        )

        _correlations.append(_correlation)

    return np.mean(_correlations)
def main():
    _experiments = all_experiments()
    _experiments = filtering.by_categories(_experiments=_experiments,
                                           _is_single_cell=True,
                                           _is_high_temporal_resolution=False,
                                           _is_bleb=False,
                                           _is_dead_dead=False,
                                           _is_live_dead=False,
                                           _is_bead=False,
                                           _is_metastasis=False)

    _tuples = load.experiments_groups_as_tuples(_experiments)
    _tuples = filtering.by_time_frames_amount(
        _tuples, compute.density_time_frame(_experiments[0]))
    _tuples = filtering.by_main_cell(_tuples)

    _arguments = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        _time_frame = compute.density_time_frame(_experiment)
        for _direction in ['left', 'right']:
            _arguments.append({
                'experiment': _experiment,
                'series_id': _series_id,
                'group': _group,
                'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                'offset_x': OFFSET_X,
                'offset_y': OFFSET_Y,
                'offset_z': OFFSET_Z,
                'cell_id': 'cell',
                'direction': _direction,
                'time_points': _time_frame
            })

    _windows_dictionary, _windows_to_compute = \
        compute.windows(_arguments, _keys=['experiment', 'series_id', 'group', 'direction'])
    _fiber_densities = compute.fiber_densities(_windows_to_compute,
                                               _subtract_border=True)

    _tuples = organize.by_single_cell_id(_tuples)
    print('Total tuples:', len(_tuples))
    _experiments_ids = list(_tuples.keys())

    _y_arrays = [[] for _i in DERIVATIVES]
    for _index_1 in tqdm(range(len(_experiments_ids)), desc='Main loop'):
        _tuple_1 = _experiments_ids[_index_1]
        _experiment_1, _series_id_1, _cell_id_1 = _tuple_1
        _fiber_densities_1 = compute_single_cell_mean(
            _experiment=_experiment_1,
            _series_id=_series_id_1,
            _cell_tuples=_tuples[_tuple_1],
            _windows_dictionary=_windows_dictionary,
            _fiber_densities=_fiber_densities)
        for _index_2 in range(_index_1 + 1, len(_experiments_ids)):
            _tuple_2 = _experiments_ids[_index_2]
            _experiment_2, _series_id_2, _cell_id_2 = _tuple_2
            _fiber_densities_2 = compute_single_cell_mean(
                _experiment=_experiment_2,
                _series_id=_series_id_2,
                _cell_tuples=_tuples[_tuple_2],
                _windows_dictionary=_windows_dictionary,
                _fiber_densities=_fiber_densities)
            for _derivative_index, _derivative in enumerate(DERIVATIVES):
                _y_arrays[_derivative_index].append(
                    compute_lib.correlation(
                        compute_lib.derivative(_fiber_densities_1,
                                               _n=_derivative),
                        compute_lib.derivative(_fiber_densities_2,
                                               _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 compute_fiber_densities(_band=True, _high_temporal_resolution=False):
    _experiments = all_experiments()
    _experiments = filtering.by_categories(
        _experiments=_experiments,
        _is_single_cell=False,
        _is_high_temporal_resolution=_high_temporal_resolution,
        _is_bleb=False,
        _is_dead_dead=False,
        _is_live_dead=False,
        _is_bead=False,
        _is_metastasis=False)

    _tuples = load.experiments_groups_as_tuples(_experiments)
    _tuples = filtering.by_pair_distance_range(_tuples, PAIR_DISTANCE_RANGE)
    _tuples = filtering.by_real_pairs(_tuples)
    _tuples = filtering.by_band(_tuples, _band=_band)
    print('Total tuples:', len(_tuples))

    _arguments = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        _latest_time_frame = compute.latest_time_frame_before_overlapping(
            _experiment, _series_id, _group, OFFSET_X)
        for _cell_id in ['left_cell', 'right_cell']:
            _arguments.append({
                'experiment': _experiment,
                'series_id': _series_id,
                'group': _group,
                'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                'offset_x': OFFSET_X,
                'offset_y': OFFSET_Y,
                'offset_z': OFFSET_Z,
                'cell_id': _cell_id,
                'direction': 'inside',
                'time_points': _latest_time_frame
            })

    _windows_dictionary, _windows_to_compute = compute.windows(
        _arguments, _keys=['experiment', 'series_id', 'group', 'cell_id'])
    _fiber_densities = compute.fiber_densities(_windows_to_compute,
                                               _subtract_border=True)

    _experiments_fiber_densities = {
        _key:
        [_fiber_densities[_tuple] for _tuple in _windows_dictionary[_key]]
        for _key in _windows_dictionary
    }

    _tuples_by_experiment = organize.by_experiment(_tuples)

    _distances_from_y_equal_x = []
    _z_positions_array = []
    for _experiment in _tuples_by_experiment:
        print('Experiment:', _experiment)
        _experiment_tuples = _tuples_by_experiment[_experiment]

        for _same_index in tqdm(range(len(_experiment_tuples)),
                                desc='Main loop'):
            _same_tuple = _experiment_tuples[_same_index]
            _same_experiment, _same_series, _same_group = _same_tuple

            _same_left_cell_fiber_densities = \
                _experiments_fiber_densities[
                    (_same_experiment, _same_series, _same_group, 'left_cell')
                ]
            _same_right_cell_fiber_densities = \
                _experiments_fiber_densities[
                    (_same_experiment, _same_series, _same_group, 'right_cell')
                ]

            _same_properties = \
                load.group_properties(_same_experiment, _same_series, _same_group)
            _same_left_cell_fiber_densities = compute.remove_blacklist(
                _same_experiment, _same_series,
                _same_properties['cells_ids']['left_cell'],
                _same_left_cell_fiber_densities)
            _same_right_cell_fiber_densities = compute.remove_blacklist(
                _same_experiment, _same_series,
                _same_properties['cells_ids']['right_cell'],
                _same_right_cell_fiber_densities)

            _same_left_cell_fiber_densities_filtered, _same_right_cell_fiber_densities_filtered = \
                compute.longest_same_indices_shared_in_borders_sub_array(
                    _same_left_cell_fiber_densities, _same_right_cell_fiber_densities
                )

            # ignore small arrays
            if len(_same_left_cell_fiber_densities_filtered
                   ) < compute.minimum_time_frames_for_correlation(
                       _same_experiment):
                continue

            _same_correlation = compute_lib.correlation(
                compute_lib.derivative(
                    _same_left_cell_fiber_densities_filtered, _n=DERIVATIVE),
                compute_lib.derivative(
                    _same_right_cell_fiber_densities_filtered, _n=DERIVATIVE))

            _same_group_mean_z_position = \
                compute.group_mean_z_position_from_substrate(_same_experiment, _same_series, _same_group)

            for _different_index in range(len(_experiment_tuples)):
                if _same_index != _different_index:
                    _different_tuple = _experiment_tuples[_different_index]
                    _different_experiment, _different_series, _different_group = \
                        _different_tuple
                    for _same_cell_id, _different_cell_id in product(
                        ['left_cell', 'right_cell'],
                        ['left_cell', 'right_cell']):
                        _same_fiber_densities = _experiments_fiber_densities[(
                            _same_experiment, _same_series, _same_group,
                            _same_cell_id)]
                        _different_fiber_densities = _experiments_fiber_densities[
                            (_different_experiment, _different_series,
                             _different_group, _different_cell_id)]

                        _different_properties = load.group_properties(
                            _different_experiment, _different_series,
                            _different_group)
                        _same_fiber_densities = compute.remove_blacklist(
                            _same_experiment, _same_series,
                            _same_properties['cells_ids'][_same_cell_id],
                            _same_fiber_densities)
                        _different_fiber_densities = compute.remove_blacklist(
                            _different_experiment, _different_series,
                            _different_properties['cells_ids']
                            [_different_cell_id], _different_fiber_densities)

                        _same_fiber_densities_filtered, _different_fiber_densities_filtered = \
                            compute.longest_same_indices_shared_in_borders_sub_array(
                                _same_fiber_densities, _different_fiber_densities
                            )

                        # ignore small arrays
                        if len(_same_fiber_densities_filtered
                               ) < compute.minimum_time_frames_for_correlation(
                                   _different_experiment):
                            continue

                        _different_correlation = compute_lib.correlation(
                            compute_lib.derivative(
                                _same_fiber_densities_filtered, _n=DERIVATIVE),
                            compute_lib.derivative(
                                _different_fiber_densities_filtered,
                                _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:
                            _distances_from_y_equal_x.append(_point_distance)
                        else:
                            _distances_from_y_equal_x.append(-_point_distance)

                        _z_positions_array.append(_same_group_mean_z_position)

    print('Total points:', len(_distances_from_y_equal_x))
    print('Wilcoxon of distances from y = x around the zero:')
    print(wilcoxon(_distances_from_y_equal_x))
    print(
        'Pearson correlation of distances from y = x and z position distances:'
    )
    print(
        compute_lib.correlation(_distances_from_y_equal_x,
                                _z_positions_array,
                                _with_p_value=True))

    return _distances_from_y_equal_x, _z_positions_array
Exemplo n.º 4
0
def main(_real_cells=True,
         _static=False,
         _dead_dead=False,
         _live_dead=False,
         _dead=False,
         _live=False,
         _bead=False,
         _metastasis=False,
         _bleb=False,
         _bleb_amount_um=None,
         _band=True,
         _high_temporal_resolution=False,
         _offset_y=0.5):
    _experiments = all_experiments()
    _experiments = filtering.by_categories(
        _experiments=_experiments,
        _is_single_cell=False,
        _is_high_temporal_resolution=_high_temporal_resolution,
        _is_bleb=_bleb,
        _is_dead_dead=_dead_dead,
        _is_live_dead=_live_dead,
        _is_bead=_bead,
        _is_metastasis=_metastasis)

    _tuples = load.experiments_groups_as_tuples(_experiments)
    _tuples = filtering.by_pair_distance_range(_tuples, PAIR_DISTANCE_RANGE)
    _tuples = filtering.by_real_pairs(_tuples, _real_pairs=_real_cells)
    _tuples = filtering.by_fake_static_pairs(_tuples,
                                             _fake_static_pairs=_static)
    if _dead_dead is not False or _live_dead is not False:
        _tuples = filtering.by_dead_live(_tuples, _dead=_dead, _live=_live)
    _tuples = filtering.by_band(_tuples, _band=_band)
    if _bleb:
        _tuples = filtering.by_bleb_amount_um(_tuples,
                                              _amount_um=_bleb_amount_um)
    print('Total tuples:', len(_tuples))

    _arguments = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        _latest_time_frame = compute.latest_time_frame_before_overlapping(
            _experiment, _series_id, _group, OFFSET_X)
        for _cell_id in ['left_cell', 'right_cell']:
            _arguments.append({
                'experiment': _experiment,
                'series_id': _series_id,
                'group': _group,
                'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                'offset_x': OFFSET_X,
                'offset_y': _offset_y,
                'offset_z': OFFSET_Z,
                'cell_id': _cell_id,
                'direction': 'inside',
                'time_points': _latest_time_frame
            })

    _windows_dictionary, _windows_to_compute = compute.windows(
        _arguments, _keys=['experiment', 'series_id', 'group', 'cell_id'])
    _fiber_densities = compute.fiber_densities(_windows_to_compute,
                                               _subtract_border=True)

    _experiments_fiber_densities = {
        _key:
        [_fiber_densities[_tuple] for _tuple in _windows_dictionary[_key]]
        for _key in _windows_dictionary
    }

    _tuples_by_experiment = organize.by_experiment(_tuples)

    _n = 0
    _cells_ranks = []
    for _experiment in _tuples_by_experiment:
        print('Experiment:', _experiment)
        _experiment_tuples = _tuples_by_experiment[_experiment]

        for _pivot_tuple in tqdm(_experiment_tuples, desc='Main loop'):
            _pivot_experiment, _pivot_series_id, _pivot_group = _pivot_tuple
            _pivot_experiment_properties = load.group_properties(
                _pivot_experiment, _pivot_series_id, _pivot_group)

            for _pivot_cell_id, _pivot_cell_correct_match_cell_id in \
                    zip(['left_cell', 'right_cell'], ['right_cell', 'left_cell']):
                _pivot_cell = (_pivot_experiment, _pivot_series_id,
                               _pivot_group, _pivot_cell_id)
                _pivot_cell_correct_match_cell = (
                    _pivot_experiment, _pivot_series_id, _pivot_group,
                    _pivot_cell_correct_match_cell_id)
                _pivot_cell_fiber_densities = _experiments_fiber_densities[
                    _pivot_cell]
                _pivot_cell_fiber_densities = compute.remove_blacklist(
                    _pivot_experiment, _pivot_series_id,
                    _pivot_experiment_properties['cells_ids'][_pivot_cell_id],
                    _pivot_cell_fiber_densities)

                _pivot_cell_correlations = []

                # correct match
                _pivot_cell_correct_match_fiber_densities = _experiments_fiber_densities[
                    _pivot_cell_correct_match_cell]
                _pivot_cell_correct_match_fiber_densities = compute.remove_blacklist(
                    _pivot_experiment, _pivot_series_id,
                    _pivot_experiment_properties['cells_ids']
                    [_pivot_cell_correct_match_cell_id],
                    _pivot_cell_correct_match_fiber_densities)
                _pivot_cell_fiber_densities_filtered, _pivot_cell_correct_match_fiber_densities_filtered = \
                    compute.longest_same_indices_shared_in_borders_sub_array(
                        _pivot_cell_fiber_densities, _pivot_cell_correct_match_fiber_densities
                    )
                # ignore small arrays
                if len(_pivot_cell_fiber_densities_filtered
                       ) < compute.minimum_time_frames_for_correlation(
                           _pivot_experiment):
                    continue
                _correlation = compute_lib.correlation(
                    compute_lib.derivative(
                        _pivot_cell_fiber_densities_filtered, _n=DERIVATIVE),
                    compute_lib.derivative(
                        _pivot_cell_correct_match_fiber_densities_filtered,
                        _n=DERIVATIVE))
                _pivot_cell_correlations.append(_correlation)
                _pivot_cell_correct_match_correlation = _correlation

                # create list of potential cells
                _candidate_tuples = []
                for _candidate_tuple in _experiment_tuples:
                    _candidate_experiment, _candidate_series_id, _candidate_group = _candidate_tuple
                    for _candidate_cell_id in ['left_cell', 'right_cell']:
                        _candidate_cell = (_candidate_experiment,
                                           _candidate_series_id,
                                           _candidate_group,
                                           _candidate_cell_id)

                        # skip if same cell or correct match
                        if _candidate_cell == _pivot_cell or _candidate_cell == _pivot_cell_correct_match_cell:
                            continue

                        _candidate_tuples.append(_candidate_cell)

                # compare with each potential candidate, until reached the maximum or nothing to compare with
                while len(_pivot_cell_correlations
                          ) < POTENTIAL_MATCHES and len(_candidate_tuples) > 0:

                    # sample randomly
                    _candidate_cell = random.choice(_candidate_tuples)
                    _candidate_experiment, _candidate_series_id, _candidate_group, _candidate_cell_id = _candidate_cell
                    _candidate_experiment_properties = load.group_properties(
                        _candidate_experiment, _candidate_series_id,
                        _candidate_group)

                    _candidate_cell_fiber_densities = _experiments_fiber_densities[
                        _candidate_cell]
                    _candidate_cell_fiber_densities = compute.remove_blacklist(
                        _candidate_experiment, _candidate_series_id,
                        _candidate_experiment_properties['cells_ids']
                        [_candidate_cell_id], _candidate_cell_fiber_densities)

                    _pivot_cell_fiber_densities_filtered, _candidate_cell_fiber_densities_filtered = \
                        compute.longest_same_indices_shared_in_borders_sub_array(
                            _pivot_cell_fiber_densities, _candidate_cell_fiber_densities
                        )

                    # ignore small arrays
                    if len(_pivot_cell_fiber_densities_filtered
                           ) < compute.minimum_time_frames_for_correlation(
                               _pivot_experiment):
                        _candidate_tuples.remove(_candidate_cell)
                        continue

                    _correlation = compute_lib.correlation(
                        compute_lib.derivative(
                            _pivot_cell_fiber_densities_filtered,
                            _n=DERIVATIVE),
                        compute_lib.derivative(
                            _candidate_cell_fiber_densities_filtered,
                            _n=DERIVATIVE))

                    _pivot_cell_correlations.append(_correlation)

                # nothing to compare with
                if len(_pivot_cell_correlations) == 1:
                    continue

                # check matchmaking
                _pivot_cell_correct_match_rank = 1
                for _potential_match_correlation in sorted(
                        _pivot_cell_correlations, reverse=True):
                    if _pivot_cell_correct_match_correlation == _potential_match_correlation:
                        break
                    _pivot_cell_correct_match_rank += 1

                _n += 1
                _cells_ranks.append(_pivot_cell_correct_match_rank)

    # results
    _correct_match_probability = 1 / 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('Correct match probability:', round(_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': '#ea8500'}),
                     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_real_' + str(_real_cells) + '_static_' +
                 str(_static) + '_dead_dead_' + str(_dead_dead) +
                 '_live_dead_' + str(_live_dead) + '_dead_' + str(_dead) +
                 '_live_' + str(_live) + '_bead_' + str(_bead) +
                 '_metastasis_' + str(_metastasis) + '_bleb_' + str(_bleb) +
                 str(_bleb_amount_um) + '_band_' + str(_band) + '_high_time_' +
                 str(_high_temporal_resolution) + '_y_' + str(_offset_y))

    # correct match probability plot
    _y = [_correct_match_probability] * (MAX_RANK - 1) + [
        _correct_match_probability * (POTENTIAL_MATCHES - MAX_RANK + 1)
    ]
    _fig = go.Figure(data=go.Bar(x=_x, y=_y, marker={'color': '#ea8500'}),
                     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_real_' + str(_real_cells) + '_static_' +
                 str(_static) + '_dead_dead_' + str(_dead_dead) +
                 '_live_dead_' + str(_live_dead) + '_dead_' + str(_dead) +
                 '_live_' + str(_live) + '_bead_' + str(_bead) +
                 '_metastasis_' + str(_metastasis) + '_bleb_' + str(_bleb) +
                 str(_bleb_amount_um) + '_band_' + str(_band) + '_high_time_' +
                 str(_high_temporal_resolution) + '_y_' + str(_offset_y) +
                 '_correct_match_prob')
Exemplo n.º 5
0
def main(_real_cells=True, _static=False, _band=True, _high_temporal_resolution=False):
    _experiments = all_experiments()
    _experiments = filtering.by_categories(
        _experiments=_experiments,
        _is_single_cell=False,
        _is_high_temporal_resolution=_high_temporal_resolution,
        _is_bleb=False,
        _is_dead_dead=False,
        _is_live_dead=False,
        _is_bead=False,
        _is_metastasis=False
    )

    _tuples = load.experiments_groups_as_tuples(_experiments)
    _tuples = filtering.by_pair_distance_range(_tuples, PAIR_DISTANCE_RANGE)
    _tuples = filtering.by_real_pairs(_tuples, _real_pairs=_real_cells)
    _tuples = filtering.by_fake_static_pairs(_tuples, _fake_static_pairs=_static)
    _tuples = filtering.by_band(_tuples, _band=_band)
    _tuples = filtering.by_time_frames_amount(_tuples, compute.minimum_time_frames_for_correlation(_experiments[0]))
    print('Total tuples:', len(_tuples))

    print('Density')
    _arguments = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        _time_frame = compute.minimum_time_frames_for_correlation(_experiment)
        for _cell_id in ['left_cell', 'right_cell']:
            _arguments.append({
                'experiment': _experiment,
                'series_id': _series_id,
                'group': _group,
                'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                'offset_x': OFFSET_X,
                'offset_y': OFFSET_Y_DENSITY,
                'offset_z': OFFSET_Z,
                'cell_id': _cell_id,
                'direction': 'inside',
                'time_point': _time_frame - 1
            })

    _windows_dictionary, _windows_to_compute = \
        compute.windows(_arguments, _keys=['experiment', 'series_id', 'group', 'cell_id'])
    _densities_fiber_densities = compute.fiber_densities(_windows_to_compute, _subtract_border=False)

    _densities_experiments_fiber_densities = {
        _key: [_densities_fiber_densities[_tuple] for _tuple in _windows_dictionary[_key]]
        for _key in _windows_dictionary
    }

    print('Correlations')
    _arguments = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        _latest_time_frame = compute.latest_time_frame_before_overlapping(_experiment, _series_id, _group, OFFSET_X)
        for _cell_id in ['left_cell', 'right_cell']:
            _arguments.append({
                'experiment': _experiment,
                'series_id': _series_id,
                'group': _group,
                'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                'offset_x': OFFSET_X,
                'offset_y': OFFSET_Y_CORRELATION,
                'offset_z': OFFSET_Z,
                'cell_id': _cell_id,
                'direction': 'inside',
                'time_points': _latest_time_frame
            })

    _windows_dictionary, _windows_to_compute = \
        compute.windows(_arguments, _keys=['experiment', 'series_id', 'group', 'cell_id'])
    _correlations_fiber_densities = compute.fiber_densities(_windows_to_compute, _subtract_border=True)

    _correlations_experiments_fiber_densities = {
        _key: [_correlations_fiber_densities[_tuple] for _tuple in _windows_dictionary[_key]]
        for _key in _windows_dictionary
    }

    _densities = []
    _correlations = []
    for _tuple in tqdm(_tuples, desc='Main loop'):
        _experiment, _series_id, _group = _tuple

        # density
        _left_cell_fiber_density = \
            _densities_experiments_fiber_densities[(_experiment, _series_id, _group, 'left_cell')][0]
        _right_cell_fiber_density = \
            _densities_experiments_fiber_densities[(_experiment, _series_id, _group, 'right_cell')][0]

        # not relevant
        if _left_cell_fiber_density[1] or _right_cell_fiber_density[1]:
            continue

        _normalization = load.normalization_series_file_data(_experiment, _series_id)
        _left_cell_fiber_density_normalized = compute_lib.z_score(
            _x=_left_cell_fiber_density[0],
            _average=_normalization['average'],
            _std=_normalization['std']
        )
        _right_cell_fiber_density_normalized = compute_lib.z_score(
            _x=_right_cell_fiber_density[0],
            _average=_normalization['average'],
            _std=_normalization['std']
        )

        _density = (_left_cell_fiber_density_normalized + _right_cell_fiber_density_normalized) / 2

        # correlation
        _left_cell_fiber_densities = \
            _correlations_experiments_fiber_densities[(_experiment, _series_id, _group, 'left_cell')]
        _right_cell_fiber_densities = \
            _correlations_experiments_fiber_densities[(_experiment, _series_id, _group, 'right_cell')]

        _properties = load.group_properties(_experiment, _series_id, _group)
        _left_cell_fiber_densities = compute.remove_blacklist(
            _experiment, _series_id, _properties['cells_ids']['left_cell'], _left_cell_fiber_densities)
        _right_cell_fiber_densities = compute.remove_blacklist(
            _experiment, _series_id, _properties['cells_ids']['right_cell'], _right_cell_fiber_densities)

        _left_cell_fiber_densities_filtered, _right_cell_fiber_densities_filtered = \
            compute.longest_same_indices_shared_in_borders_sub_array(
                _left_cell_fiber_densities, _right_cell_fiber_densities
            )

        # ignore small arrays
        if len(_left_cell_fiber_densities_filtered) < compute.minimum_time_frames_for_correlation(_experiment):
            continue

        _correlation = compute_lib.correlation(
            compute_lib.derivative(_left_cell_fiber_densities_filtered, _n=DERIVATIVE),
            compute_lib.derivative(_right_cell_fiber_densities_filtered, _n=DERIVATIVE)
        )

        _densities.append(_density)
        _correlations.append(_correlation)

    print('Total tuples:', len(_densities))
    print('Pearson correlation of densities and correlations:')
    print(compute_lib.correlation(_densities, _correlations, _with_p_value=True))

    # plot
    _fig = go.Figure(
        data=go.Scatter(
            x=_densities,
            y=_correlations,
            mode='markers',
            marker={
                'size': 5,
                'color': '#ea8500'
            },
            showlegend=False
        ),
        layout={
            'xaxis': {
                'title': 'Fiber density (z-score)',
                'zeroline': False,
                'range': [-1.1, 15.2],
                # 'tickmode': 'array',
                # 'tickvals': [-1, -0.5, 0, 0.5, 1]
            },
            'yaxis': {
                'title': 'Correlation',
                'zeroline': False,
                'range': [-1.1, 1.2],
                'tickmode': 'array',
                'tickvals': [-1, -0.5, 0, 0.5, 1]
            },
            'shapes': [
                {
                    'type': 'line',
                    'x0': 0,
                    'y0': -1,
                    'x1': 0,
                    'y1': 1,
                    'line': {
                        'color': 'black',
                        'width': 2
                    }
                },
                {
                    'type': 'line',
                    'x0': 0,
                    'y0': -1,
                    'x1': 15,
                    'y1': -1,
                    'line': {
                        'color': 'black',
                        'width': 2
                    }
                }
            ]
        }
    )

    save.to_html(
        _fig=_fig,
        _path=os.path.join(paths.PLOTS, save.get_module_name()),
        _filename='plot_real_' + str(_real_cells) + '_static_' + str(_static) + '_band_' + str(_band) +
                  '_high_time_' + str(_high_temporal_resolution) + '_y_density_' + str(OFFSET_Y_DENSITY) +
                  '_y_correlation_' + str(OFFSET_Y_CORRELATION)
    )
Exemplo n.º 6
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')
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')
Exemplo n.º 8
0
def main(_band=True):
    _experiments = all_experiments()
    _experiments = filtering.by_categories(
        _experiments=_experiments,
        _is_single_cell=False,
        _is_high_temporal_resolution=None,
        _is_bleb=True,
        _is_dead_dead=False,
        _is_live_dead=False,
        _is_bead=False,
        _is_metastasis=False
    )

    _tuples = load.experiments_groups_as_tuples(_experiments)
    _tuples = filtering.by_pair_distance_range(_tuples, _distance_range=PAIR_DISTANCE_RANGE)
    _tuples = filtering.by_real_pairs(_tuples, _real_pairs=REAL_CELLS)
    _tuples = filtering.by_fake_static_pairs(_tuples, _fake_static_pairs=STATIC)
    _tuples = filtering.by_band(_tuples, _band=_band)
    _tuples = filtering.by_bleb_from_start(_tuples, _from_start=False)
    print('Total tuples:', len(_tuples))

    _arguments = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        _latest_time_frame = compute.latest_time_frame_before_overlapping(_experiment, _series_id, _group, OFFSET_X)
        for _cell_id in ['left_cell', 'right_cell']:
            _arguments.append({
                'experiment': _experiment,
                'series_id': _series_id,
                'group': _group,
                'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                'offset_x': OFFSET_X,
                'offset_y': OFFSET_Y,
                'offset_z': OFFSET_Z,
                'cell_id': _cell_id,
                'direction': 'inside',
                'time_points': _latest_time_frame
            })

    _windows_dictionary, _windows_to_compute = compute.windows(_arguments,
                                                               _keys=['experiment', 'series_id', 'group', 'cell_id'])
    _fiber_densities = compute.fiber_densities(_windows_to_compute, _subtract_border=True)

    _experiments_fiber_densities = {
        _key: [_fiber_densities[_tuple] for _tuple in _windows_dictionary[_key]]
        for _key in _windows_dictionary
    }

    _n_pairs = 0
    _before_correlations = []
    _after_correlations = []
    for _tuple in tqdm(_tuples, desc='Experiments loop'):
        _experiment, _series_id, _group = _tuple

        _left_cell_fiber_densities = \
            _experiments_fiber_densities[(_experiment, _series_id, _group, 'left_cell')]
        _right_cell_fiber_densities = \
            _experiments_fiber_densities[(_experiment, _series_id, _group, 'right_cell')]

        _properties = load.group_properties(_experiment, _series_id, _group)
        _left_cell_fiber_densities = compute.remove_blacklist(
            _experiment, _series_id, _properties['cells_ids']['left_cell'], _left_cell_fiber_densities)
        _right_cell_fiber_densities = compute.remove_blacklist(
            _experiment, _series_id, _properties['cells_ids']['right_cell'], _right_cell_fiber_densities)

        _before_left_cell_fiber_densities = \
            _left_cell_fiber_densities[:AFTER_BLEB_INJECTION_FIRST_TIME_FRAME[_experiment]]
        _before_right_cell_fiber_densities = \
            _right_cell_fiber_densities[:AFTER_BLEB_INJECTION_FIRST_TIME_FRAME[_experiment]]

        _after_left_cell_fiber_densities = \
            _left_cell_fiber_densities[AFTER_BLEB_INJECTION_FIRST_TIME_FRAME[_experiment]:]
        _after_right_cell_fiber_densities = \
            _right_cell_fiber_densities[AFTER_BLEB_INJECTION_FIRST_TIME_FRAME[_experiment]:]

        _before_left_cell_fiber_densities_filtered, _before_right_cell_fiber_densities_filtered = \
            compute.longest_same_indices_shared_in_borders_sub_array(
                _before_left_cell_fiber_densities, _before_right_cell_fiber_densities)

        _after_left_cell_fiber_densities_filtered, _after_right_cell_fiber_densities_filtered = \
            compute.longest_same_indices_shared_in_borders_sub_array(
                _after_left_cell_fiber_densities, _after_right_cell_fiber_densities)

        # ignore small arrays
        _minimum_time_frame_for_correlation = compute.minimum_time_frames_for_correlation(_experiment)
        if len(_before_left_cell_fiber_densities_filtered) < _minimum_time_frame_for_correlation or \
                len(_after_left_cell_fiber_densities_filtered) < _minimum_time_frame_for_correlation:
            continue

        _n_pairs += 1

        _before_correlations.append(compute_lib.correlation(
            compute_lib.derivative(_before_left_cell_fiber_densities_filtered, _n=DERIVATIVE),
            compute_lib.derivative(_before_right_cell_fiber_densities_filtered, _n=DERIVATIVE)
        ))
        _after_correlations.append(compute_lib.correlation(
            compute_lib.derivative(_after_left_cell_fiber_densities_filtered, _n=DERIVATIVE),
            compute_lib.derivative(_after_right_cell_fiber_densities_filtered, _n=DERIVATIVE)
        ))

    print('Total pairs:', _n_pairs)
    _before_minus_after = np.array(_before_correlations) - np.array(_after_correlations)
    print('Wilcoxon of before minus after around the zero:')
    print(wilcoxon(_before_minus_after))
    print('Higher before amount:', (_before_minus_after > 0).sum() / len(_before_minus_after))

    # plot
    _fig = go.Figure(
        data=go.Scatter(
            x=_before_correlations,
            y=_after_correlations,
            mode='markers',
            marker={
                'size': 5,
                'color': '#ea8500'
            },
            showlegend=False
        ),
        layout={
            'xaxis': {
                'title': 'Correlation before bleb',
                'zeroline': False,
                'range': [-1.1, 1.2],
                'tickmode': 'array',
                'tickvals': [-1, -0.5, 0, 0.5, 1]
            },
            'yaxis': {
                'title': 'Correlation after bleb',
                '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'
    )
Exemplo n.º 9
0
def main(_high_temporal_resolution=True):
    _experiments = all_experiments()
    _experiments = filtering.by_categories(
        _experiments=_experiments,
        _is_single_cell=False,
        _is_high_temporal_resolution=_high_temporal_resolution,
        _is_bleb=False,
        _is_dead_dead=False,
        _is_live_dead=False,
        _is_bead=False,
        _is_metastasis=False)

    _tuples = load.experiments_groups_as_tuples(_experiments)
    _tuples = filtering.by_pair_distance_range(_tuples, PAIR_DISTANCE_RANGE)
    _tuples = filtering.by_real_pairs(_tuples)
    _tuples = filtering.by_band(_tuples)
    print('Total tuples:', len(_tuples))

    _arguments = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        for _cell_id in ['left_cell', 'right_cell']:
            _latest_time_frame = compute.latest_time_frame_before_overlapping(
                _experiment, _series_id, _group, OFFSET_X)
            _arguments.append({
                'experiment': _experiment,
                'series_id': _series_id,
                'group': _group,
                'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                'offset_x': OFFSET_X,
                'offset_y': OFFSET_Y,
                'offset_z': OFFSET_Z,
                'cell_id': _cell_id,
                'direction': 'inside',
                'time_points': _latest_time_frame
            })
            if ALIGNMENT_OFFSET_Y != OFFSET_Y:
                _arguments.append({
                    'experiment': _experiment,
                    'series_id': _series_id,
                    'group': _group,
                    'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                    'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                    'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                    'offset_x': OFFSET_X,
                    'offset_y': ALIGNMENT_OFFSET_Y,
                    'offset_z': OFFSET_Z,
                    'cell_id': _cell_id,
                    'direction': 'inside',
                    'time_points': _latest_time_frame
                })

    _windows_dictionary, _windows_to_compute = \
        compute.windows(_arguments, _keys=['experiment', 'series_id', 'group', 'cell_id', 'offset_y'])
    _fiber_densities = compute.fiber_densities(_windows_to_compute)

    _experiments_fiber_densities = {
        _key:
        [_fiber_densities[_tuple] for _tuple in _windows_dictionary[_key]]
        for _key in _windows_dictionary
    }

    _experiments_fiber_densities_aligned = align_by_z_score(
        _tuples, _experiments_fiber_densities)
    _tuples_by_experiment = organize.by_experiment(_tuples)

    _same_correlations_array = []
    _different_correlations_array = []
    _valid_tuples = []
    for _experiment in _tuples_by_experiment:
        print('Experiment:', _experiment)
        _experiment_tuples = _tuples_by_experiment[_experiment]

        for _same_index in tqdm(range(len(_experiment_tuples)),
                                desc='Main loop'):
            _same_tuple = _experiment_tuples[_same_index]
            _same_experiment, _same_series, _same_group = _same_tuple

            _same_left_cell_fiber_densities = \
                _experiments_fiber_densities_aligned[
                    (_same_experiment, _same_series, _same_group, 'left_cell')
                ]
            _same_right_cell_fiber_densities = \
                _experiments_fiber_densities_aligned[
                    (_same_experiment, _same_series, _same_group, 'right_cell')
                ]

            _same_properties = \
                load.group_properties(_same_experiment, _same_series, _same_group)
            _same_left_cell_fiber_densities = compute.remove_blacklist(
                _same_experiment, _same_series,
                _same_properties['cells_ids']['left_cell'],
                _same_left_cell_fiber_densities)
            _same_right_cell_fiber_densities = compute.remove_blacklist(
                _same_experiment, _same_series,
                _same_properties['cells_ids']['right_cell'],
                _same_right_cell_fiber_densities)

            _same_left_cell_fiber_densities_filtered, _same_right_cell_fiber_densities_filtered = \
                compute.longest_same_indices_shared_in_borders_sub_array(
                    _same_left_cell_fiber_densities, _same_right_cell_fiber_densities
                )

            # ignore small arrays
            if len(_same_left_cell_fiber_densities_filtered
                   ) < compute.minimum_time_frames_for_correlation(
                       _same_experiment):
                continue

            _same_correlation = compute_lib.correlation(
                compute_lib.derivative(
                    _same_left_cell_fiber_densities_filtered, _n=DERIVATIVE),
                compute_lib.derivative(
                    _same_right_cell_fiber_densities_filtered, _n=DERIVATIVE))
            for _different_index in range(len(_experiment_tuples)):
                if _same_index != _different_index:
                    _different_tuple = _experiment_tuples[_different_index]
                    _different_experiment, _different_series, _different_group = \
                        _different_tuple
                    for _same_cell_id, _different_cell_id in product(
                        ['left_cell', 'right_cell'],
                        ['left_cell', 'right_cell']):
                        _same_fiber_densities = _experiments_fiber_densities_aligned[
                            (_same_experiment, _same_series, _same_group,
                             _same_cell_id)]
                        _different_fiber_densities = _experiments_fiber_densities_aligned[
                            (_different_experiment, _different_series,
                             _different_group, _different_cell_id)]

                        _different_properties = load.group_properties(
                            _different_experiment, _different_series,
                            _different_group)
                        _same_fiber_densities = compute.remove_blacklist(
                            _same_experiment, _same_series,
                            _same_properties['cells_ids'][_same_cell_id],
                            _same_fiber_densities)
                        _different_fiber_densities = compute.remove_blacklist(
                            _different_experiment, _different_series,
                            _different_properties['cells_ids']
                            [_different_cell_id], _different_fiber_densities)

                        _same_fiber_densities_filtered, _different_fiber_densities_filtered = \
                            compute.longest_same_indices_shared_in_borders_sub_array(
                                _same_fiber_densities, _different_fiber_densities
                            )

                        # ignore small arrays
                        if len(_same_fiber_densities_filtered
                               ) < compute.minimum_time_frames_for_correlation(
                                   _different_experiment):
                            continue

                        _different_correlation = compute_lib.correlation(
                            compute_lib.derivative(
                                _same_fiber_densities_filtered, _n=DERIVATIVE),
                            compute_lib.derivative(
                                _different_fiber_densities_filtered,
                                _n=DERIVATIVE))

                        _same_correlations_array.append(_same_correlation)
                        _different_correlations_array.append(
                            _different_correlation)

                        if _same_tuple not in _valid_tuples:
                            _valid_tuples.append(_same_tuple)

    print('Total tuples:', len(_valid_tuples))
    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': '#ea8500'
                                     },
                                     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_high_time_' + str(_high_temporal_resolution))
Exemplo n.º 10
0
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(_band=True):
    _experiments = all_experiments()
    _experiments = filtering.by_categories(_experiments=_experiments,
                                           _is_single_cell=False,
                                           _is_high_temporal_resolution=None,
                                           _is_bleb=True,
                                           _is_dead_dead=False,
                                           _is_live_dead=False,
                                           _is_bead=False,
                                           _is_metastasis=False)

    _tuples = load.experiments_groups_as_tuples(_experiments)
    _tuples = filtering.by_pair_distance_range(
        _tuples, _distance_range=PAIR_DISTANCE_RANGE)
    _tuples = filtering.by_real_pairs(_tuples, _real_pairs=REAL_CELLS)
    _tuples = filtering.by_fake_static_pairs(_tuples,
                                             _fake_static_pairs=STATIC)
    _tuples = filtering.by_band(_tuples, _band=_band)
    _tuples = filtering.by_bleb_from_start(_tuples, _from_start=False)
    print('Total tuples:', len(_tuples))

    _arguments = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        _latest_time_frame = compute.latest_time_frame_before_overlapping(
            _experiment, _series_id, _group, OFFSET_X)
        for _cell_id in ['left_cell', 'right_cell']:
            _arguments.append({
                'experiment': _experiment,
                'series_id': _series_id,
                'group': _group,
                'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                'offset_x': OFFSET_X,
                'offset_y': OFFSET_Y,
                'offset_z': OFFSET_Z,
                'cell_id': _cell_id,
                'direction': 'inside',
                'time_points': _latest_time_frame
            })

    _windows_dictionary, _windows_to_compute = compute.windows(
        _arguments, _keys=['experiment', 'series_id', 'group', 'cell_id'])
    _fiber_densities = compute.fiber_densities(_windows_to_compute,
                                               _subtract_border=True)

    _experiments_fiber_densities = {
        _key:
        [_fiber_densities[_tuple] for _tuple in _windows_dictionary[_key]]
        for _key in _windows_dictionary
    }

    _tuples_by_experiment = organize.by_experiment(_tuples)

    # same (before, after), different (before, after)
    _correlations = [[[], []], [[], []]]
    _valid_real_tuples = []
    for _experiment in _tuples_by_experiment:
        print('Experiment:', _experiment)
        _experiment_tuples = _tuples_by_experiment[_experiment]

        for _same_index in tqdm(range(len(_experiment_tuples)),
                                desc='Main loop'):
            _same_tuple = _experiment_tuples[_same_index]
            _same_experiment, _same_series, _same_group = _same_tuple

            _same_left_cell_fiber_densities = \
                _experiments_fiber_densities[
                    (_same_experiment, _same_series, _same_group, 'left_cell')
                ]
            _same_right_cell_fiber_densities = \
                _experiments_fiber_densities[
                    (_same_experiment, _same_series, _same_group, 'right_cell')
                ]

            _same_properties = \
                load.group_properties(_same_experiment, _same_series, _same_group)
            _same_left_cell_fiber_densities = compute.remove_blacklist(
                _same_experiment, _same_series,
                _same_properties['cells_ids']['left_cell'],
                _same_left_cell_fiber_densities)
            _same_right_cell_fiber_densities = compute.remove_blacklist(
                _same_experiment, _same_series,
                _same_properties['cells_ids']['right_cell'],
                _same_right_cell_fiber_densities)

            _same_before_left_cell_fiber_densities = \
                _same_left_cell_fiber_densities[:AFTER_BLEB_INJECTION_FIRST_TIME_FRAME[_same_experiment]]
            _same_before_right_cell_fiber_densities = \
                _same_right_cell_fiber_densities[:AFTER_BLEB_INJECTION_FIRST_TIME_FRAME[_same_experiment]]

            _same_after_left_cell_fiber_densities = \
                _same_left_cell_fiber_densities[AFTER_BLEB_INJECTION_FIRST_TIME_FRAME[_same_experiment]:]
            _same_after_right_cell_fiber_densities = \
                _same_right_cell_fiber_densities[AFTER_BLEB_INJECTION_FIRST_TIME_FRAME[_same_experiment]:]

            _same_before_left_cell_fiber_densities_filtered, _same_before_right_cell_fiber_densities_filtered = \
                compute.longest_same_indices_shared_in_borders_sub_array(
                    _same_before_left_cell_fiber_densities, _same_before_right_cell_fiber_densities
                )

            _same_after_left_cell_fiber_densities_filtered, _same_after_right_cell_fiber_densities_filtered = \
                compute.longest_same_indices_shared_in_borders_sub_array(
                    _same_after_left_cell_fiber_densities, _same_after_right_cell_fiber_densities
                )

            # ignore small arrays
            _minimum_time_frame_for_correlation = compute.minimum_time_frames_for_correlation(
                _same_experiment)
            if len(_same_before_left_cell_fiber_densities_filtered) < _minimum_time_frame_for_correlation or \
                    len(_same_after_left_cell_fiber_densities_filtered) < _minimum_time_frame_for_correlation:
                continue

            _same_before_correlation = compute_lib.correlation(
                compute_lib.derivative(
                    _same_before_left_cell_fiber_densities_filtered,
                    _n=DERIVATIVE),
                compute_lib.derivative(
                    _same_before_right_cell_fiber_densities_filtered,
                    _n=DERIVATIVE))
            _same_after_correlation = compute_lib.correlation(
                compute_lib.derivative(
                    _same_after_left_cell_fiber_densities_filtered,
                    _n=DERIVATIVE),
                compute_lib.derivative(
                    _same_after_right_cell_fiber_densities_filtered,
                    _n=DERIVATIVE))

            for _different_index in range(len(_experiment_tuples)):
                if _same_index != _different_index:
                    _different_tuple = _experiment_tuples[_different_index]
                    _different_experiment, _different_series, _different_group = _different_tuple
                    for _same_cell_id, _different_cell_id in product(
                        ['left_cell', 'right_cell'],
                        ['left_cell', 'right_cell']):
                        _same_fiber_densities = _experiments_fiber_densities[(
                            _same_experiment, _same_series, _same_group,
                            _same_cell_id)]
                        _different_fiber_densities = _experiments_fiber_densities[
                            (_different_experiment, _different_series,
                             _different_group, _different_cell_id)]

                        _different_properties = load.group_properties(
                            _different_experiment, _different_series,
                            _different_group)
                        _same_fiber_densities = compute.remove_blacklist(
                            _same_experiment, _same_series,
                            _same_properties['cells_ids'][_same_cell_id],
                            _same_fiber_densities)
                        _different_fiber_densities = compute.remove_blacklist(
                            _different_experiment, _different_series,
                            _different_properties['cells_ids']
                            [_different_cell_id], _different_fiber_densities)

                        _same_before_fiber_densities = \
                            _same_fiber_densities[:AFTER_BLEB_INJECTION_FIRST_TIME_FRAME[_same_experiment]]
                        _same_after_fiber_densities = \
                            _same_fiber_densities[AFTER_BLEB_INJECTION_FIRST_TIME_FRAME[_same_experiment]:]

                        _different_before_fiber_densities = \
                            _different_fiber_densities[:AFTER_BLEB_INJECTION_FIRST_TIME_FRAME[_different_experiment]]
                        _different_after_fiber_densities = \
                            _different_fiber_densities[AFTER_BLEB_INJECTION_FIRST_TIME_FRAME[_different_experiment]:]

                        _same_before_fiber_densities_filtered, _different_before_fiber_densities_filtered = \
                            compute.longest_same_indices_shared_in_borders_sub_array(
                                _same_before_fiber_densities, _different_before_fiber_densities
                            )

                        _same_after_fiber_densities_filtered, _different_after_fiber_densities_filtered = \
                            compute.longest_same_indices_shared_in_borders_sub_array(
                                _same_after_fiber_densities, _different_after_fiber_densities
                            )

                        # ignore small arrays
                        if len(_same_before_fiber_densities_filtered) < _minimum_time_frame_for_correlation or \
                                len(_same_after_fiber_densities_filtered) < _minimum_time_frame_for_correlation:
                            continue

                        _different_before_correlation = compute_lib.correlation(
                            compute_lib.derivative(
                                _same_before_fiber_densities_filtered,
                                _n=DERIVATIVE),
                            compute_lib.derivative(
                                _different_before_fiber_densities_filtered,
                                _n=DERIVATIVE))
                        _different_after_correlation = compute_lib.correlation(
                            compute_lib.derivative(
                                _same_after_fiber_densities_filtered,
                                _n=DERIVATIVE),
                            compute_lib.derivative(
                                _different_after_fiber_densities_filtered,
                                _n=DERIVATIVE))

                        _correlations[0][0].append(_same_before_correlation)
                        _correlations[0][1].append(_same_after_correlation)
                        _correlations[1][0].append(
                            _different_before_correlation)
                        _correlations[1][1].append(
                            _different_after_correlation)

                        if _same_tuple not in _valid_real_tuples:
                            _valid_real_tuples.append(_same_tuple)

    print('Total tuples:', len(_valid_real_tuples))
    _distances_from_y_equal_x = [[], []]
    _same_correlations, _different_correlations = _correlations
    _same_before_correlations, _same_after_correlations = _same_correlations
    _different_before_correlations, _different_after_correlations = _different_correlations
    for _same_before, _same_after, _different_before, _different_after in \
            zip(_same_before_correlations, _same_after_correlations,
                _different_before_correlations, _different_after_correlations):

        for _group_type_index, _same, _different in \
                zip([0, 1], [_same_before, _same_after], [_different_before, _different_after]):

            _point_distance = compute_lib.distance_from_a_point_to_a_line(
                _line=[-1, -1, 1, 1], _point=[_same, _different])
            if _same > _different:
                _distances_from_y_equal_x[_group_type_index].append(
                    _point_distance)
            else:
                _distances_from_y_equal_x[_group_type_index].append(
                    -_point_distance)

    print('Total points:', len(_distances_from_y_equal_x[0]))
    print('Higher before same amount:',
          (np.array(_distances_from_y_equal_x[0]) > 0).sum() /
          len(_distances_from_y_equal_x[0]))
    print('Wilcoxon of before points:', wilcoxon(_distances_from_y_equal_x[0]))
    print('Higher after same amount:',
          (np.array(_distances_from_y_equal_x[1]) > 0).sum() /
          len(_distances_from_y_equal_x[1]))
    print('Wilcoxon of after points:', wilcoxon(_distances_from_y_equal_x[1]))
    _before_minus_after = np.array(_distances_from_y_equal_x[0]) - np.array(
        _distances_from_y_equal_x[1])
    print('Before > after amount:',
          (_before_minus_after > 0).sum() / len(_before_minus_after))
    print('Wilcoxon before & after:',
          wilcoxon(_distances_from_y_equal_x[0], _distances_from_y_equal_x[1]))

    # box plot
    _colors_array = config.colors(2)
    _names_array = ['Before', 'After']
    _fig = go.Figure(data=[
        go.Box(y=_y_array,
               name=_name,
               boxpoints=False,
               line={'width': 1},
               marker={'color': _color},
               showlegend=False) for _y_array, _name, _color in zip(
                   _distances_from_y_equal_x, _names_array, _colors_array)
    ],
                     layout={
                         'xaxis': {
                             'zeroline': False
                         },
                         'yaxis': {
                             'title': 'Same minus different correlation',
                             'zeroline': False,
                             'range': [-1, 1.1],
                             '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_box')

    # scatter plot
    _fig = go.Figure(data=go.Scatter(x=_distances_from_y_equal_x[0],
                                     y=_distances_from_y_equal_x[1],
                                     mode='markers',
                                     marker={
                                         'size': 5,
                                         'color': '#ea8500'
                                     },
                                     showlegend=False),
                     layout={
                         'xaxis': {
                             'title': 'Before bleb',
                             'zeroline': False,
                             'range': [-1.1, 1.2],
                             'tickmode': 'array',
                             'tickvals': [-1, -0.5, 0, 0.5, 1]
                         },
                         'yaxis': {
                             'title': 'After bleb',
                             '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=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))
Exemplo n.º 13
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)
        )
Exemplo n.º 14
0
def compute_tuples(_tuples):
    _arguments = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple

        # stop when windows are overlapping
        _properties = load.group_properties(_experiment, _series_id, _group)
        _latest_time_frame = len(_properties['time_points'])
        _middle_offsets_x = []
        for _time_frame in range(len(_properties['time_points'])):
            _pair_distance = \
                compute.pair_distance_in_cell_size_time_frame(_experiment, _series_id, _group, _time_frame)
            if _time_frame == 0:
                print(_tuple, 'Pair distance:', _pair_distance)
            _middle_offsets_x.append(
                (_time_frame, (_pair_distance - 1) / 2 -
                 QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER / 2))
            if _pair_distance - 1 - OFFSET_X * 2 < QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER * 2:
                _latest_time_frame = _time_frame - 1
                break

        for _cell_id in ['left_cell', 'right_cell']:
            _arguments.append({
                'experiment': _experiment,
                'series_id': _series_id,
                'group': _group,
                'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                'offset_x': OFFSET_X,
                'offset_y': OFFSET_Y,
                'offset_z': OFFSET_Z,
                'cell_id': _cell_id,
                'direction': 'inside',
                'time_points': _latest_time_frame,
                'middle_time_frame': -1
            })

        # middle one
        for (_time_frame, _offset_x) in _middle_offsets_x:
            _arguments.append({
                'experiment': _experiment,
                'series_id': _series_id,
                'group': _group,
                'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                'offset_x': _offset_x,
                'offset_y': OFFSET_Y,
                'offset_z': OFFSET_Z,
                'cell_id': 'left_cell',
                'direction': 'inside',
                'time_point': _time_frame,
                'middle_time_frame': _time_frame
            })

    _windows_dictionary, _windows_to_compute = \
        compute.windows(_arguments, _keys=['experiment', 'series_id', 'group', 'cell_id', 'middle_time_frame'])
    _fiber_densities = compute.fiber_densities(_windows_to_compute)

    _experiments_fiber_densities = {
        _key:
        [_fiber_densities[_tuple] for _tuple in _windows_dictionary[_key]]
        for _key in _windows_dictionary
    }

    _tuples_data = []
    for _tuple in tqdm(_tuples, desc='Main loop'):
        _experiment, _series_id, _group = _tuple

        _left_cell_fiber_densities = \
            _experiments_fiber_densities[(_experiment, _series_id, _group, 'left_cell', -1)]
        _right_cell_fiber_densities = \
            _experiments_fiber_densities[(_experiment, _series_id, _group, 'right_cell', -1)]
        _middle_fiber_densities = \
            [_experiments_fiber_densities[(_experiment, _series_id, _group, 'left_cell', _time_frame)][0]
             for _time_frame in range(0, len(_left_cell_fiber_densities))]

        _properties = load.group_properties(_experiment, _series_id, _group)
        _left_cell_fiber_densities = compute.remove_blacklist(
            _experiment, _series_id, _properties['cells_ids']['left_cell'],
            _left_cell_fiber_densities)
        _right_cell_fiber_densities = compute.remove_blacklist(
            _experiment, _series_id, _properties['cells_ids']['right_cell'],
            _right_cell_fiber_densities)

        _left_cell_fiber_densities_filtered, _right_cell_fiber_densities_filtered = \
            compute.longest_same_indices_shared_in_borders_sub_array(
                _left_cell_fiber_densities, _right_cell_fiber_densities)

        if len(_left_cell_fiber_densities_filtered) == 0:
            continue

        _start_time_frame = 0
        for _left in _left_cell_fiber_densities:
            if _left[0] == _left_cell_fiber_densities_filtered[0]:
                break
            _start_time_frame += 1

        _middle_fiber_densities_filtered = \
            [_fiber_density[0] for _fiber_density in _middle_fiber_densities]
        _middle_fiber_densities_filtered = \
            _middle_fiber_densities_filtered[_start_time_frame:
                                             _start_time_frame + len(_left_cell_fiber_densities_filtered)]

        _normalization = load.normalization_series_file_data(
            _experiment, _series_id)
        _left_cell_fiber_densities_normalized = compute_lib.z_score_array(
            _array=_left_cell_fiber_densities_filtered,
            _average=_normalization['average'],
            _std=_normalization['std'])
        _right_cell_fiber_densities_normalized = compute_lib.z_score_array(
            _array=_right_cell_fiber_densities_filtered,
            _average=_normalization['average'],
            _std=_normalization['std'])
        _middle_fiber_densities_normalized = compute_lib.z_score_array(
            _array=_middle_fiber_densities_filtered,
            _average=_normalization['average'],
            _std=_normalization['std'])

        _left_cell_fiber_densities_normalized = \
            compute_lib.derivative(_left_cell_fiber_densities_normalized, _n=DERIVATIVE)
        _right_cell_fiber_densities_normalized = \
            compute_lib.derivative(_right_cell_fiber_densities_normalized, _n=DERIVATIVE)
        _middle_fiber_densities_normalized = \
            compute_lib.derivative(_middle_fiber_densities_normalized, _n=DERIVATIVE)

        _correlation = \
            compute_lib.correlation(
                compute_lib.derivative(_left_cell_fiber_densities_normalized, _n=1),
                compute_lib.derivative(_right_cell_fiber_densities_normalized, _n=1)
            )

        _tuples_data.append([
            _tuple, _start_time_frame,
            (_left_cell_fiber_densities_normalized,
             _right_cell_fiber_densities_normalized,
             _middle_fiber_densities_normalized), _correlation
        ])

    # plots
    _names_array = ['Left cell', 'Right cell', 'Middle']
    _colors_array = config.colors(3)
    for _tuple_data in _tuples_data:
        _tuple, _start_time_frame, _y_arrays, _correlation = _tuple_data
        _fig = go.Figure(data=[
            go.Scatter(x=np.arange(
                start=_start_time_frame,
                stop=_start_time_frame + len(_y_arrays[0]) - DERIVATIVE,
                step=1) * TEMPORAL_RESOLUTION,
                       y=_y,
                       name=_name,
                       mode='lines',
                       line={'color': _color}) for _y, _name, _color in zip(
                           _y_arrays, _names_array, _colors_array)
        ],
                         layout={
                             'xaxis': {
                                 'title': 'Time (minutes)',
                                 'zeroline': False
                             },
                             'yaxis': {
                                 'title': 'Fiber density (z-score)',
                                 'zeroline': False,
                                 'range': [-3, 8],
                                 'tickmode': 'array',
                                 'tickvals': [-3, 0, 3, 6]
                             },
                             'legend': {
                                 'xanchor': 'left',
                                 'x': 0.1,
                                 'yanchor': 'top',
                                 'bordercolor': 'black',
                                 'borderwidth': 2,
                                 'bgcolor': 'white'
                             },
                         })

        _experiment, _series_id, _group = _tuple
        save.to_html(_fig=_fig,
                     _path=os.path.join(paths.PLOTS, save.get_module_name()),
                     _filename='plot_' + _experiment + '_' + str(_series_id) +
                     '_' + _group)

        print('Tuple:', _tuple, 'Correlation:', _correlation)
Exemplo n.º 15
0
def compute_fiber_densities(_band=True, _high_temporal_resolution=True):
    _experiments = all_experiments()
    _experiments = filtering.by_categories(
        _experiments=_experiments,
        _is_single_cell=False,
        _is_high_temporal_resolution=_high_temporal_resolution,
        _is_bleb=False,
        _is_dead_dead=False,
        _is_live_dead=False,
        _is_bead=False,
        _is_metastasis=False)

    _tuples = load.experiments_groups_as_tuples(_experiments)
    _tuples = filtering.by_pair_distance_range(_tuples, PAIR_DISTANCE_RANGE)
    _tuples = filtering.by_real_pairs(_tuples)
    _tuples = filtering.by_band(_tuples, _band=_band)
    print('Total tuples:', len(_tuples))

    _arguments = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        _latest_time_frame = compute.latest_time_frame_before_overlapping(
            _experiment, _series_id, _group, OFFSET_X)
        for _cell_id in ['left_cell', 'right_cell']:
            _arguments.append({
                'experiment': _experiment,
                'series_id': _series_id,
                'group': _group,
                'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                'offset_x': OFFSET_X,
                'offset_y': OFFSET_Y,
                'offset_z': OFFSET_Z,
                'cell_id': _cell_id,
                'direction': 'inside',
                'time_points': _latest_time_frame
            })

    _windows_dictionary, _windows_to_compute = compute.windows(
        _arguments, _keys=['experiment', 'series_id', 'group', 'cell_id'])
    _fiber_densities = compute.fiber_densities(_windows_to_compute,
                                               _subtract_border=True)

    _experiments_fiber_densities = {
        _key:
        [_fiber_densities[_tuple] for _tuple in _windows_dictionary[_key]]
        for _key in _windows_dictionary
    }

    _same_correlation_vs_time_lag = {}
    _same_time_lags_arrays = [[]
                              for _i in TIME_LAGS[_high_temporal_resolution]]
    _different_time_lags_arrays = [
        [] for _i in TIME_LAGS[_high_temporal_resolution]
    ]
    _same_time_lags_highest = [
        0 for _i in TIME_LAGS[_high_temporal_resolution]
    ]
    _different_time_lags_highest = [
        0 for _i in TIME_LAGS[_high_temporal_resolution]
    ]
    _valid_tuples = []
    for _same_index in tqdm(range(len(_tuples)), desc='Main loop'):
        _same_tuple = _tuples[_same_index]
        _same_experiment, _same_series, _same_group = _same_tuple

        _same_left_cell_fiber_densities = \
            _experiments_fiber_densities[
                (_same_experiment, _same_series, _same_group, 'left_cell')
            ]
        _same_right_cell_fiber_densities = \
            _experiments_fiber_densities[
                (_same_experiment, _same_series, _same_group, 'right_cell')
            ]

        _same_properties = \
            load.group_properties(_same_experiment, _same_series, _same_group)
        _same_left_cell_fiber_densities = compute.remove_blacklist(
            _same_experiment, _same_series,
            _same_properties['cells_ids']['left_cell'],
            _same_left_cell_fiber_densities)
        _same_right_cell_fiber_densities = compute.remove_blacklist(
            _same_experiment, _same_series,
            _same_properties['cells_ids']['right_cell'],
            _same_right_cell_fiber_densities)

        # time lag
        _same_highest_correlation = -1.1
        _same_highest_correlation_time_lag_index = 0
        _same_correlation_vs_time_lag[_same_tuple] = []
        for _time_lag_index, _time_lag in enumerate(
                TIME_LAGS[_high_temporal_resolution]):

            # choose either negative or positive lag
            for _symbol in [-1, 1]:

                # if no time lag consider it only once
                if _time_lag == 0 and _symbol == -1:
                    continue

                _time_lag_symbol = _time_lag * _symbol

                if _time_lag_symbol > 0:
                    _same_left_cell_fiber_densities_time_lag = _same_left_cell_fiber_densities[:
                                                                                               -_time_lag_symbol]
                    _same_right_cell_fiber_densities_time_lag = _same_right_cell_fiber_densities[
                        _time_lag_symbol:]
                elif _time_lag_symbol < 0:
                    _same_left_cell_fiber_densities_time_lag = _same_left_cell_fiber_densities[
                        -_time_lag_symbol:]
                    _same_right_cell_fiber_densities_time_lag = _same_right_cell_fiber_densities[:
                                                                                                 _time_lag_symbol]
                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_left_cell_fiber_densities_filtered, _same_right_cell_fiber_densities_filtered = \
                    compute.longest_same_indices_shared_in_borders_sub_array(
                        _same_left_cell_fiber_densities_time_lag, _same_right_cell_fiber_densities_time_lag
                    )

                # ignore small arrays
                if len(_same_left_cell_fiber_densities_filtered
                       ) < compute.minimum_time_frames_for_correlation(
                           _same_experiment):
                    _same_correlation_vs_time_lag[_same_tuple].append(None)
                    continue

                _same_correlation = compute_lib.correlation(
                    compute_lib.derivative(
                        _same_left_cell_fiber_densities_filtered,
                        _n=DERIVATIVE),
                    compute_lib.derivative(
                        _same_right_cell_fiber_densities_filtered,
                        _n=DERIVATIVE))

                _same_time_lags_arrays[_time_lag_index].append(
                    _same_correlation)
                _same_correlation_vs_time_lag[_same_tuple].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(_tuples)):
            if _same_index != _different_index:
                _different_tuple = _tuples[_different_index]
                _different_experiment, _different_series, _different_group = \
                    _different_tuple
                for _same_cell_id, _different_cell_id in product(
                    ['left_cell', 'right_cell'], ['left_cell', 'right_cell']):
                    _same_fiber_densities = _experiments_fiber_densities[(
                        _same_experiment, _same_series, _same_group,
                        _same_cell_id)]
                    _different_fiber_densities = _experiments_fiber_densities[(
                        _different_experiment, _different_series,
                        _different_group, _different_cell_id)]

                    _different_properties = load.group_properties(
                        _different_experiment, _different_series,
                        _different_group)
                    _same_fiber_densities = compute.remove_blacklist(
                        _same_experiment, _same_series,
                        _same_properties['cells_ids'][_same_cell_id],
                        _same_fiber_densities)
                    _different_fiber_densities = compute.remove_blacklist(
                        _different_experiment, _different_series,
                        _different_properties['cells_ids'][_different_cell_id],
                        _different_fiber_densities)

                    # time lag
                    _different_highest_correlation = -1.1
                    _different_highest_correlation_time_lag_index = 0
                    for _time_lag_index, _time_lag in enumerate(
                            TIME_LAGS[_high_temporal_resolution]):

                        # choose either negative or positive lag
                        for _symbol in [-1, 1]:

                            # if no time lag consider it only once
                            if _time_lag == 0 and _symbol == -1:
                                continue

                            _time_lag_symbol = _time_lag * _symbol

                            if _time_lag_symbol > 0:
                                _same_fiber_densities_time_lag = _same_fiber_densities[:
                                                                                       -_time_lag_symbol]
                                _different_fiber_densities_time_lag = _different_fiber_densities[
                                    _time_lag_symbol:]
                            elif _time_lag_symbol < 0:
                                _same_fiber_densities_time_lag = _same_fiber_densities[
                                    -_time_lag_symbol:]
                                _different_fiber_densities_time_lag = _different_fiber_densities[:
                                                                                                 _time_lag_symbol]
                            else:
                                _same_fiber_densities_time_lag = _same_fiber_densities
                                _different_fiber_densities_time_lag = _different_fiber_densities

                            _same_fiber_densities_filtered, _different_fiber_densities_filtered = \
                                compute.longest_same_indices_shared_in_borders_sub_array(
                                    _same_fiber_densities_time_lag, _different_fiber_densities_time_lag
                                )

                            # ignore small arrays
                            if len(
                                    _same_fiber_densities_filtered
                            ) < compute.minimum_time_frames_for_correlation(
                                    _different_experiment):
                                continue

                            _different_correlation = compute_lib.correlation(
                                compute_lib.derivative(
                                    _same_fiber_densities_filtered,
                                    _n=DERIVATIVE),
                                compute_lib.derivative(
                                    _different_fiber_densities_filtered,
                                    _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

                            if _same_tuple not in _valid_tuples:
                                _valid_tuples.append(_same_tuple)

                    _different_time_lags_highest[
                        _different_highest_correlation_time_lag_index] += 1

    print('Total tuples:', len(_valid_tuples))

    return _same_correlation_vs_time_lag, _same_time_lags_arrays, _different_time_lags_arrays, \
        _same_time_lags_highest, _different_time_lags_highest
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')
Exemplo n.º 17
0
def compute_data(_arguments):
    _offset_y_index, _offset_y, _offset_z_index, _offset_z = _arguments
    _same_correlations_array = []
    _different_correlations_array = []
    for _experiment in _tuples_by_experiment:
        _experiment_tuples = _tuples_by_experiment[_experiment]

        for _same_index in range(len(_experiment_tuples)):
            _same_tuple = _experiment_tuples[_same_index]
            _same_experiment, _same_series, _same_group = _same_tuple

            _same_left_cell_fiber_densities = _experiments_fiber_densities[(
                _same_experiment, _same_series, _same_group, _offset_y,
                _offset_z, 'left_cell')]
            _same_right_cell_fiber_densities = _experiments_fiber_densities[(
                _same_experiment, _same_series, _same_group, _offset_y,
                _offset_z, 'right_cell')]

            _same_properties = \
                load.group_properties(_same_experiment, _same_series, _same_group)
            _same_left_cell_fiber_densities = compute.remove_blacklist(
                _same_experiment, _same_series,
                _same_properties['cells_ids']['left_cell'],
                _same_left_cell_fiber_densities)
            _same_right_cell_fiber_densities = compute.remove_blacklist(
                _same_experiment, _same_series,
                _same_properties['cells_ids']['right_cell'],
                _same_right_cell_fiber_densities)

            _same_left_cell_fiber_densities_filtered, _same_right_cell_fiber_densities_filtered = \
                compute.longest_same_indices_shared_in_borders_sub_array(
                    _same_left_cell_fiber_densities, _same_right_cell_fiber_densities
                )

            # ignore small arrays
            if len(_same_left_cell_fiber_densities_filtered
                   ) < compute.minimum_time_frames_for_correlation(
                       _same_experiment):
                continue

            _same_correlation = compute_lib.correlation(
                compute_lib.derivative(
                    _same_left_cell_fiber_densities_filtered, _n=DERIVATIVE),
                compute_lib.derivative(
                    _same_right_cell_fiber_densities_filtered, _n=DERIVATIVE))
            for _different_index in range(len(_experiment_tuples)):
                if _same_index != _different_index:
                    _different_tuple = _experiment_tuples[_different_index]
                    _different_experiment, _different_series, _different_group = _different_tuple

                    for _same_cell_id, _different_cell_id in product(
                        ['left_cell', 'right_cell'],
                        ['left_cell', 'right_cell']):
                        _same_fiber_densities = _experiments_fiber_densities[(
                            _same_experiment, _same_series, _same_group,
                            _offset_y, _offset_z, _same_cell_id)]
                        _different_fiber_densities = _experiments_fiber_densities[
                            (_different_experiment, _different_series,
                             _different_group, _offset_y, _offset_z,
                             _different_cell_id)]

                        _different_properties = load.group_properties(
                            _different_experiment, _different_series,
                            _different_group)
                        _same_fiber_densities = compute.remove_blacklist(
                            _same_experiment, _same_series,
                            _same_properties['cells_ids'][_same_cell_id],
                            _same_fiber_densities)
                        _different_fiber_densities = compute.remove_blacklist(
                            _different_experiment, _different_series,
                            _different_properties['cells_ids']
                            [_different_cell_id], _different_fiber_densities)

                        _same_fiber_densities_filtered, _different_fiber_densities_filtered = \
                            compute.longest_same_indices_shared_in_borders_sub_array(
                                _same_fiber_densities, _different_fiber_densities
                            )

                        # ignore small arrays
                        if len(_same_fiber_densities_filtered
                               ) < compute.minimum_time_frames_for_correlation(
                                   _different_experiment):
                            continue

                        _different_correlation = compute_lib.correlation(
                            compute_lib.derivative(
                                _same_fiber_densities_filtered, _n=DERIVATIVE),
                            compute_lib.derivative(
                                _different_fiber_densities_filtered,
                                _n=DERIVATIVE))
                        _same_correlations_array.append(_same_correlation)
                        _different_correlations_array.append(
                            _different_correlation)

    # compute fraction
    _annotation = None
    _same_minus_different = np.array(_same_correlations_array) - np.array(
        _different_correlations_array)
    _same_count = len(_same_minus_different[_same_minus_different > 0])
    if len(_same_minus_different) > 0:
        _same_fraction = round(_same_count / len(_same_minus_different), 10)
        _wilcoxon = wilcoxon(_same_minus_different)
        _p_value = _wilcoxon[1]
        if _p_value > 0.05:
            _annotation = {
                'text': 'x',
                'showarrow': False,
                'x': _offset_z,
                'y': _offset_y,
                'font': {
                    'size': 6,
                    'color': 'red'
                }
            }
    else:
        _same_fraction = None

    return _offset_y_index, _offset_z_index, _same_fraction, _annotation
Exemplo n.º 18
0
def main():
    _experiments = all_experiments()
    _experiments = filtering.by_categories(_experiments=_experiments,
                                           _is_single_cell=True,
                                           _is_high_temporal_resolution=False,
                                           _is_bleb=False,
                                           _is_dead_dead=False,
                                           _is_live_dead=False,
                                           _is_bead=False,
                                           _is_metastasis=False)

    _tuples = load.experiments_groups_as_tuples(_experiments)
    _tuples = filtering.by_time_frames_amount(
        _tuples, compute.density_time_frame(_experiments[0]))
    _tuples = filtering.by_main_cell(_tuples)

    _arguments = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        _time_frame = compute.density_time_frame(_experiment)
        for _direction in ['left', 'right']:
            _arguments.append({
                'experiment': _experiment,
                'series_id': _series_id,
                'group': _group,
                'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                'offset_x': OFFSET_X,
                'offset_y': OFFSET_Y,
                'offset_z': OFFSET_Z,
                'cell_id': 'cell',
                'direction': _direction,
                'time_points': _time_frame
            })

    _windows_dictionary, _windows_to_compute = \
        compute.windows(_arguments, _keys=['experiment', 'series_id', 'group', 'direction'])
    _fiber_densities = compute.fiber_densities(_windows_to_compute,
                                               _subtract_border=True)

    _tuples = organize.by_single_cell_id(_tuples)
    print('Total experiments:', len(_tuples))

    _kpss_y_arrays = [[] for _i in DERIVATIVES]
    _adf_y_arrays = [[] for _i in DERIVATIVES]
    for _tuple in tqdm(_tuples, desc='Experiments loop'):
        _experiment, _series_id, _cell_id = _tuple
        _cell_fiber_densities = compute_single_cell_mean(
            _experiment=_experiment,
            _series_id=_series_id,
            _cell_tuples=_tuples[_tuple],
            _windows_dictionary=_windows_dictionary,
            _fiber_densities=_fiber_densities)
        for _derivative_index, _derivative in enumerate(DERIVATIVES):
            _cell_fiber_densities_derivative = compute_lib.derivative(
                _cell_fiber_densities, _n=_derivative)
            _, _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('Total cells:', len(_kpss_y_arrays[0]))

    # 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)
Exemplo n.º 19
0
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(_high_temporal_resolution=True, _offset_y=0.5):
    _experiments = all_experiments()
    _experiments = filtering.by_categories(
        _experiments=_experiments,
        _is_single_cell=False,
        _is_high_temporal_resolution=_high_temporal_resolution,
        _is_bleb=False,
        _is_dead_dead=False,
        _is_live_dead=False,
        _is_bead=False,
        _is_metastasis=False)

    _tuples = load.experiments_groups_as_tuples(_experiments)
    _tuples = filtering.by_real_pairs(_tuples)
    # _tuples = filtering.by_band(_tuples)
    _tuples = filtering.by_pair_distance_range(_tuples, PAIR_DISTANCE_RANGE)
    print('Total tuples:', len(_tuples))

    _arguments = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        _latest_time_frame = compute.latest_time_frame_before_overlapping(
            _experiment, _series_id, _group, OFFSET_X)
        for _cell_id, _direction in product(['left_cell', 'right_cell'],
                                            ['inside', 'outside']):
            _arguments.append({
                'experiment': _experiment,
                'series_id': _series_id,
                'group': _group,
                'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                'offset_x': OFFSET_X,
                'offset_y': _offset_y,
                'offset_z': OFFSET_Z,
                'cell_id': _cell_id,
                'direction': _direction,
                'time_points': _latest_time_frame
            })

    _windows_dictionary, _windows_to_compute = \
        compute.windows(_arguments, _keys=['experiment', 'series_id', 'group', 'cell_id', 'direction'])
    _fiber_densities = compute.fiber_densities(_windows_to_compute,
                                               _subtract_border=True)

    _experiments_fiber_densities = {
        _key:
        [_fiber_densities[_tuple] for _tuple in _windows_dictionary[_key]]
        for _key in _windows_dictionary
    }

    _inner_correlations = []
    _outer_correlations = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        _left_inner_tuple = (_experiment, _series_id, _group, 'left_cell',
                             'inside')
        _left_outer_tuple = (_experiment, _series_id, _group, 'left_cell',
                             'outside')
        _right_inner_tuple = (_experiment, _series_id, _group, 'right_cell',
                              'inside')
        _right_outer_tuple = (_experiment, _series_id, _group, 'right_cell',
                              'outside')

        if _left_inner_tuple not in _windows_dictionary or _left_outer_tuple not in _windows_dictionary or \
                _right_inner_tuple not in _windows_dictionary or _right_outer_tuple not in _windows_dictionary:
            continue

        _properties = load.group_properties(_experiment, _series_id, _group)
        _left_inner_fiber_densities = _experiments_fiber_densities[
            _left_inner_tuple]
        _left_outer_fiber_densities = _experiments_fiber_densities[
            _left_outer_tuple]
        _right_inner_fiber_densities = _experiments_fiber_densities[
            _right_inner_tuple]
        _right_outer_fiber_densities = _experiments_fiber_densities[
            _right_outer_tuple]

        _left_inner_fiber_densities = compute.remove_blacklist(
            _experiment, _series_id, _properties['cells_ids']['left_cell'],
            _left_inner_fiber_densities)
        _left_outer_fiber_densities = compute.remove_blacklist(
            _experiment, _series_id, _properties['cells_ids']['left_cell'],
            _left_outer_fiber_densities)
        _right_inner_fiber_densities = compute.remove_blacklist(
            _experiment, _series_id, _properties['cells_ids']['right_cell'],
            _right_inner_fiber_densities)
        _right_outer_fiber_densities = compute.remove_blacklist(
            _experiment, _series_id, _properties['cells_ids']['right_cell'],
            _right_outer_fiber_densities)

        _left_inner_fiber_densities, _right_inner_fiber_densities = \
            compute.longest_same_indices_shared_in_borders_sub_array(
                _left_inner_fiber_densities, _right_inner_fiber_densities)
        _left_outer_fiber_densities, _right_outer_fiber_densities = \
            compute.longest_same_indices_shared_in_borders_sub_array(
                _left_outer_fiber_densities, _right_outer_fiber_densities)

        # ignore small arrays
        _minimum_time_frame_for_correlation = compute.minimum_time_frames_for_correlation(
            _experiment)
        if len(_left_inner_fiber_densities) < _minimum_time_frame_for_correlation or \
                len(_left_outer_fiber_densities) < _minimum_time_frame_for_correlation:
            continue

        _inner_correlations.append(
            compute_lib.correlation(
                compute_lib.derivative(_left_inner_fiber_densities,
                                       _n=DERIVATIVE),
                compute_lib.derivative(_right_inner_fiber_densities,
                                       _n=DERIVATIVE)))
        _outer_correlations.append(
            compute_lib.correlation(
                compute_lib.derivative(_left_outer_fiber_densities,
                                       _n=DERIVATIVE),
                compute_lib.derivative(_right_outer_fiber_densities,
                                       _n=DERIVATIVE)))

    print('Total pairs:', len(_inner_correlations))
    print(
        'Pearson correlation:',
        compute_lib.correlation(_inner_correlations,
                                _outer_correlations,
                                _with_p_value=True))
    print('Wilcoxon of inner around the zero:', wilcoxon(_inner_correlations))
    print('Wilcoxon of outer around the zero:', wilcoxon(_outer_correlations))
    _inner_minus_outer = np.array(_inner_correlations) - np.array(
        _outer_correlations)
    print('Wilcoxon of inner minus outer:', wilcoxon(_inner_minus_outer))
    print('Higher inner amount:',
          (_inner_minus_outer > 0).sum() / len(_inner_minus_outer))

    # plot
    _fig = go.Figure(data=go.Scatter(x=_inner_correlations,
                                     y=_outer_correlations,
                                     mode='markers',
                                     marker={
                                         'size': 5,
                                         'color': '#ea8500'
                                     },
                                     showlegend=False),
                     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]
                         },
                         '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_high_' + str(_high_temporal_resolution) +
                 '_offset_y_' + str(_offset_y))
Exemplo n.º 21
0
def main():
    _experiments = all_experiments()
    _experiments = filtering.by_categories(_experiments=_experiments,
                                           _is_single_cell=False,
                                           _is_high_temporal_resolution=False,
                                           _is_bleb=False,
                                           _is_dead_dead=False,
                                           _is_live_dead=False,
                                           _is_bead=False,
                                           _is_metastasis=False)

    _tuples = load.experiments_groups_as_tuples(_experiments)
    _tuples = filtering.by_time_frames_amount(
        _tuples, compute.density_time_frame(_experiments[0]))
    _tuples = filtering.by_real_pairs(_tuples)
    _tuples = filtering.by_band(_tuples)
    _tuples = filtering.by_pair_distance_range(_tuples, PAIR_DISTANCE_RANGE)
    print('Total tuples:', len(_tuples))

    _arguments = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        _latest_time_frame = compute.latest_time_frame_before_overlapping(
            _experiment, _series_id, _group, OFFSET_X)
        for _cell_id in ['left_cell', 'right_cell']:
            _arguments.append({
                'experiment': _experiment,
                'series_id': _series_id,
                'group': _group,
                'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                'offset_x': OFFSET_X,
                'offset_y': OFFSET_Y,
                'offset_z': OFFSET_Z,
                'cell_id': _cell_id,
                'direction': 'inside',
                'time_points': _latest_time_frame
            })

    _windows_dictionary, _windows_to_compute = \
        compute.windows(_arguments, _keys=['experiment', 'series_id', 'group', 'cell_id'])
    _fiber_densities = compute.fiber_densities(_windows_to_compute,
                                               _subtract_border=True)

    _experiments_fiber_densities = {
        _key:
        [_fiber_densities[_tuple] for _tuple in _windows_dictionary[_key]]
        for _key in _windows_dictionary
    }

    _kpss_y_arrays = [[] for _i in DERIVATIVES]
    _adf_y_arrays = [[] for _i in DERIVATIVES]
    for _tuple in tqdm(_tuples, desc='Experiments loop'):
        _experiment, _series_id, _group = _tuple
        _properties = load.group_properties(_experiment, _series_id, _group)

        _left_cell_fiber_densities = _experiments_fiber_densities[(
            _experiment, _series_id, _group, 'left_cell')]
        _left_cell_fiber_densities = compute.remove_blacklist(
            _experiment, _series_id, _properties['cells_ids']['left_cell'],
            _left_cell_fiber_densities)
        _right_cell_fiber_densities = _experiments_fiber_densities[(
            _experiment, _series_id, _group, 'right_cell')]
        _right_cell_fiber_densities = compute.remove_blacklist(
            _experiment, _series_id, _properties['cells_ids']['right_cell'],
            _right_cell_fiber_densities)

        if not OUT_OF_BOUNDARIES:
            _left_cell_fiber_densities = \
                compute.longest_fiber_densities_ascending_sequence(_left_cell_fiber_densities)
            _right_cell_fiber_densities = \
                compute.longest_fiber_densities_ascending_sequence(_right_cell_fiber_densities)
        else:
            _left_cell_fiber_densities = [
                _fiber_density[0]
                for _fiber_density in _left_cell_fiber_densities
            ]
            _right_cell_fiber_densities = [
                _fiber_density[0]
                for _fiber_density in _right_cell_fiber_densities
            ]

        # ignore small arrays
        _minimum_time_frames_for_correlation = compute.minimum_time_frames_for_correlation(
            _experiment)
        if len(_left_cell_fiber_densities) < _minimum_time_frames_for_correlation or \
                len(_right_cell_fiber_densities) < _minimum_time_frames_for_correlation:
            continue

        for _derivative_index, _derivative in enumerate(DERIVATIVES):
            for _cell_fiber_densities in [
                    _left_cell_fiber_densities, _right_cell_fiber_densities
            ]:
                _cell_fiber_densities_derivative = compute_lib.derivative(
                    _cell_fiber_densities, _n=_derivative)
                _, _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('Total pairs:', len(_kpss_y_arrays[0]) / 2)

    # 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)
Exemplo n.º 22
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=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(_early_time_frames=True):
    _experiments = all_experiments()
    _experiments = filtering.by_categories(_experiments=_experiments,
                                           _is_single_cell=False,
                                           _is_high_temporal_resolution=False,
                                           _is_bleb=False,
                                           _is_dead_dead=False,
                                           _is_live_dead=False,
                                           _is_bead=False,
                                           _is_metastasis=False)

    _tuples = load.experiments_groups_as_tuples(_experiments)
    _tuples = filtering.by_pair_distance_range(_tuples, PAIR_DISTANCE_RANGE)
    _tuples = filtering.by_real_pairs(_tuples)
    _tuples = filtering.by_band(_tuples)
    print('Total tuples:', len(_tuples))

    _arguments = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        for _cell_id in ['left_cell', 'right_cell']:
            _arguments.append({
                'experiment': _experiment,
                'series_id': _series_id,
                'group': _group,
                'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                'offset_x': OFFSET_X,
                'offset_y': OFFSET_Y,
                'offset_z': OFFSET_Z,
                'cell_id': _cell_id,
                'direction': 'inside'
            })

    _windows_dictionary, _windows_to_compute = compute.windows(
        _arguments, _keys=['experiment', 'series_id', 'group', 'cell_id'])
    _fiber_densities = compute.fiber_densities(_windows_to_compute)

    _heatmap_fiber = []
    _heatmap_fiber_change = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        _series_normalization = load.normalization_series_file_data(
            _experiment, _series_id)
        for _cell_id in ['left_cell', 'right_cell']:
            _fiber_densities_by_time = [
                _fiber_densities[_tuple]
                for _tuple in _windows_dictionary[(_experiment, _series_id,
                                                   _group, _cell_id)]
            ]
            _cell_fiber_densities = \
                _fiber_densities_by_time[TIME_FRAMES[OFFSET_Y]['early'][0] if _early_time_frames else TIME_FRAMES[OFFSET_Y]['late'][0]:
                                         TIME_FRAMES[OFFSET_Y]['early'][1] if _early_time_frames else TIME_FRAMES[OFFSET_Y]['late'][1]]
            _properties = load.group_properties(_experiment, _series_id,
                                                _group)
            _cell_fiber_densities = compute.remove_blacklist(
                _experiment, _series_id, _properties['cells_ids'][_cell_id],
                _cell_fiber_densities)
            _cell_fiber_densities = compute.longest_fiber_densities_ascending_sequence(
                _cell_fiber_densities)

            # fix if found nan
            if True in np.isnan(_cell_fiber_densities):
                _cell_fiber_densities = _cell_fiber_densities[:np.where(
                    np.isnan(_cell_fiber_densities))[0][0]]

            # not enough data
            if len(_cell_fiber_densities) < DERIVATIVE + 1:
                continue

            _z_score_fiber_density = libs.compute_lib.z_score_array(
                _array=_cell_fiber_densities,
                _average=_series_normalization['average'],
                _std=_series_normalization['std'])
            if _experiment in ['SN41', 'SN44']:
                for _start_index in [0, 1, 2]:
                    _heatmap_fiber += _z_score_fiber_density[_start_index::3][
                        DERIVATIVE:]
                    _heatmap_fiber_change += compute_lib.derivative(
                        _z_score_fiber_density[_start_index::3], _n=DERIVATIVE)
            else:
                _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_early_' + str(_early_time_frames))
Exemplo n.º 24
0
def compute_fiber_densities(_offset_y=0.5, _high_temporal_resolution=False):
    _experiments = all_experiments()
    _experiments = filtering.by_categories(
        _experiments=_experiments,
        _is_single_cell=False,
        _is_high_temporal_resolution=_high_temporal_resolution,
        _is_bleb=False,
        _is_dead_dead=False,
        _is_live_dead=False,
        _is_bead=False,
        _is_metastasis=False)

    _tuples = load.experiments_groups_as_tuples(_experiments)
    _tuples = filtering.by_pair_distance_range(_tuples, PAIR_DISTANCE_RANGE)
    _tuples = filtering.by_band(_tuples)
    _tuples = filtering.by_real_fake_pairs(_tuples, _real_fake_pairs=False)
    _experiments_matched = organize.by_matched_real_and_fake(_tuples)
    print('Total matched pairs:', len(_experiments_matched))

    _arguments = []
    for _matched_tuple in _experiments_matched:
        for _tuple in _matched_tuple:
            _experiment, _series_id, _group = _tuple
            _latest_time_frame = compute.latest_time_frame_before_overlapping(
                _experiment, _series_id, _group, OFFSET_X)
            for _cell_id in ['left_cell', 'right_cell']:
                _arguments.append({
                    'experiment': _experiment,
                    'series_id': _series_id,
                    'group': _group,
                    'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                    'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                    'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                    'offset_x': OFFSET_X,
                    'offset_y': _offset_y,
                    'offset_z': OFFSET_Z,
                    'cell_id': _cell_id,
                    'direction': 'inside',
                    'time_points': _latest_time_frame
                })

    _windows_dictionary, _windows_to_compute = compute.windows(
        _arguments, _keys=['experiment', 'series_id', 'group', 'cell_id'])
    _fiber_densities = compute.fiber_densities(_windows_to_compute,
                                               _subtract_border=True)

    _experiments_fiber_densities = {
        _key:
        [_fiber_densities[_tuple] for _tuple in _windows_dictionary[_key]]
        for _key in _windows_dictionary
    }

    _tuples_by_experiment = organize.by_experiment(_tuples)

    # same (real, fake), different (real, fake)
    _correlations = [[[], []], [[], []]]
    _valid_real_tuples = []
    for _experiment in _tuples_by_experiment:
        print('Experiment:', _experiment)
        _experiment_tuples = _tuples_by_experiment[_experiment]
        _experiments_matched = organize.by_matched_real_and_fake(
            _experiment_tuples)
        print('Matched pairs:', len(_experiments_matched))

        for _same_index in tqdm(range(len(_experiments_matched)),
                                desc='Main loop'):
            for _group_type_index in [0, 1]:
                _same_tuple = _experiments_matched[_same_index][
                    _group_type_index]
                _same_experiment, _same_series, _same_group = _same_tuple

                _same_left_cell_fiber_densities = \
                    _experiments_fiber_densities[
                        (_same_experiment, _same_series, _same_group, 'left_cell')
                    ]
                _same_right_cell_fiber_densities = \
                    _experiments_fiber_densities[
                        (_same_experiment, _same_series, _same_group, 'right_cell')
                    ]

                _same_properties = \
                    load.group_properties(_same_experiment, _same_series, _same_group)
                _same_left_cell_fiber_densities = compute.remove_blacklist(
                    _same_experiment, _same_series,
                    _same_properties['cells_ids']['left_cell'],
                    _same_left_cell_fiber_densities)
                _same_right_cell_fiber_densities = compute.remove_blacklist(
                    _same_experiment, _same_series,
                    _same_properties['cells_ids']['right_cell'],
                    _same_right_cell_fiber_densities)

                _same_left_cell_fiber_densities_filtered, _same_right_cell_fiber_densities_filtered = \
                    compute.longest_same_indices_shared_in_borders_sub_array(
                        _same_left_cell_fiber_densities, _same_right_cell_fiber_densities
                    )

                # ignore small arrays
                if len(_same_left_cell_fiber_densities_filtered
                       ) < compute.minimum_time_frames_for_correlation(
                           _same_experiment):
                    for _different_index in range(len(_experiments_matched)):
                        if _same_index != _different_index:
                            # for all combinations
                            for _i in range(4):
                                _correlations[0][_group_type_index].append(
                                    None)
                                _correlations[1][_group_type_index].append(
                                    None)
                    continue

                _same_correlation = compute_lib.correlation(
                    compute_lib.derivative(
                        _same_left_cell_fiber_densities_filtered,
                        _n=DERIVATIVE),
                    compute_lib.derivative(
                        _same_right_cell_fiber_densities_filtered,
                        _n=DERIVATIVE))
                for _different_index in range(len(_experiments_matched)):
                    if _same_index != _different_index:
                        _different_tuple = _experiments_matched[
                            _different_index][_group_type_index]
                        _different_experiment, _different_series, _different_group = _different_tuple
                        for _same_cell_id, _different_cell_id in product(
                            ['left_cell', 'right_cell'],
                            ['left_cell', 'right_cell']):
                            _same_fiber_densities = _experiments_fiber_densities[
                                (_same_experiment, _same_series, _same_group,
                                 _same_cell_id)]
                            _different_fiber_densities = _experiments_fiber_densities[
                                (_different_experiment, _different_series,
                                 _different_group, _different_cell_id)]

                            _different_properties = load.group_properties(
                                _different_experiment, _different_series,
                                _different_group)
                            _same_fiber_densities = compute.remove_blacklist(
                                _same_experiment, _same_series,
                                _same_properties['cells_ids'][_same_cell_id],
                                _same_fiber_densities)
                            _different_fiber_densities = compute.remove_blacklist(
                                _different_experiment, _different_series,
                                _different_properties['cells_ids']
                                [_different_cell_id],
                                _different_fiber_densities)

                            _same_fiber_densities_filtered, _different_fiber_densities_filtered = \
                                compute.longest_same_indices_shared_in_borders_sub_array(
                                    _same_fiber_densities, _different_fiber_densities
                                )

                            # ignore small arrays
                            if len(
                                    _same_fiber_densities_filtered
                            ) < compute.minimum_time_frames_for_correlation(
                                    _different_experiment):
                                _correlations[0][_group_type_index].append(
                                    None)
                                _correlations[1][_group_type_index].append(
                                    None)
                                continue

                            _different_correlation = compute_lib.correlation(
                                compute_lib.derivative(
                                    _same_fiber_densities_filtered,
                                    _n=DERIVATIVE),
                                compute_lib.derivative(
                                    _different_fiber_densities_filtered,
                                    _n=DERIVATIVE))

                            _correlations[0][_group_type_index].append(
                                _same_correlation)
                            _correlations[1][_group_type_index].append(
                                _different_correlation)

                            if _group_type_index == 0 and _same_tuple not in _valid_real_tuples:
                                _valid_real_tuples.append(_same_tuple)

    print('Total real tuples:', len(_valid_real_tuples))
    _distances_from_y_equal_x = [[], []]
    _same_correlations, _different_correlations = _correlations
    _same_real_correlations, _same_fake_correlations = _same_correlations
    _different_real_correlations, _different_fake_correlations = _different_correlations
    for _same_real, _same_fake, _different_real, _different_fake in \
            zip(_same_real_correlations, _same_fake_correlations,
                _different_real_correlations, _different_fake_correlations):

        # one of the correlations is none - not valid
        if None in [_same_real, _same_fake, _different_real, _different_fake]:
            continue

        for _group_type_index, _same, _different in \
                zip([0, 1], [_same_real, _same_fake], [_different_real, _different_fake]):

            _point_distance = compute_lib.distance_from_a_point_to_a_line(
                _line=[-1, -1, 1, 1], _point=[_same, _different])
            if _same > _different:
                _distances_from_y_equal_x[_group_type_index].append(
                    _point_distance)
            else:
                _distances_from_y_equal_x[_group_type_index].append(
                    -_point_distance)

    return _distances_from_y_equal_x
Exemplo n.º 25
0
def main():
    _arguments = []
    for _tuple in TRIPLET:
        _experiment, _series_id, _group = _tuple
        _pair_distance = compute.pair_distance_in_cell_size_time_frame(_experiment, _series_id, _group, _time_frame=0)
        print(_tuple, 'pairs distance:', round(_pair_distance, 2))
        _latest_time_frame = compute.latest_time_frame_before_overlapping(_experiment, _series_id, _group, OFFSET_X)
        for _cell_id in ['left_cell', 'right_cell']:
            _arguments.append({
                'experiment': _experiment,
                'series_id': _series_id,
                'group': _group,
                'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                'offset_x': OFFSET_X,
                'offset_y': OFFSET_Y,
                'offset_z': OFFSET_Z,
                'cell_id': _cell_id,
                'direction': 'inside',
                'time_points': _latest_time_frame
            })

    _windows_dictionary, _windows_to_compute = compute.windows(_arguments,
                                                               _keys=['experiment', 'series_id', 'group', 'cell_id'])
    _fiber_densities = compute.fiber_densities(_windows_to_compute, _subtract_border=True)

    _experiments_fiber_densities = {
        _key: [_fiber_densities[_tuple] for _tuple in _windows_dictionary[_key]]
        for _key in _windows_dictionary
    }

    _same_correlations_arrays = [[], [], []]
    _different_correlations_arrays = [[], [], []]
    _names_array = []
    for _same_index in tqdm(range(3), desc='Main loop'):
        _same_tuple = TRIPLET[_same_index]
        _same_experiment, _same_series, _same_group = _same_tuple

        _same_left_cell_fiber_densities = \
            _experiments_fiber_densities[
                (_same_experiment, _same_series, _same_group, 'left_cell')
            ]
        _same_right_cell_fiber_densities = \
            _experiments_fiber_densities[
                (_same_experiment, _same_series, _same_group, 'right_cell')
            ]

        _same_properties = \
            load.group_properties(_same_experiment, _same_series, _same_group)
        _same_left_cell_fiber_densities = compute.remove_blacklist(
            _same_experiment,
            _same_series,
            _same_properties['cells_ids']['left_cell'],
            _same_left_cell_fiber_densities
        )
        _same_right_cell_fiber_densities = compute.remove_blacklist(
            _same_experiment,
            _same_series,
            _same_properties['cells_ids']['right_cell'],
            _same_right_cell_fiber_densities
        )

        _same_left_cell_fiber_densities_filtered, _same_right_cell_fiber_densities_filtered = \
            compute.longest_same_indices_shared_in_borders_sub_array(
                _same_left_cell_fiber_densities, _same_right_cell_fiber_densities
            )

        _same_correlation = compute_lib.correlation(
            compute_lib.derivative(_same_left_cell_fiber_densities_filtered, _n=DERIVATIVE),
            compute_lib.derivative(_same_right_cell_fiber_densities_filtered, _n=DERIVATIVE)
        )
        for _different_index in range(3):
            if _same_index != _different_index:
                _different_tuple = TRIPLET[_different_index]
                _different_experiment, _different_series, _different_group = \
                    _different_tuple
                for _same_cell_id, _different_cell_id in product(['left_cell', 'right_cell'],
                                                                 ['left_cell', 'right_cell']):
                    _same_fiber_densities = _experiments_fiber_densities[(
                        _same_experiment,
                        _same_series,
                        _same_group,
                        _same_cell_id
                    )]
                    _different_fiber_densities = _experiments_fiber_densities[(
                        _different_experiment,
                        _different_series,
                        _different_group,
                        _different_cell_id
                    )]

                    _different_properties = load.group_properties(
                        _different_experiment, _different_series, _different_group
                    )
                    _same_fiber_densities = compute.remove_blacklist(
                        _same_experiment,
                        _same_series,
                        _same_properties['cells_ids'][_same_cell_id],
                        _same_fiber_densities
                    )
                    _different_fiber_densities = compute.remove_blacklist(
                        _different_experiment,
                        _different_series,
                        _different_properties['cells_ids'][_different_cell_id],
                        _different_fiber_densities
                    )

                    _same_fiber_densities_filtered, _different_fiber_densities_filtered = \
                        compute.longest_same_indices_shared_in_borders_sub_array(
                            _same_fiber_densities, _different_fiber_densities
                        )

                    _different_correlation = compute_lib.correlation(
                        compute_lib.derivative(_same_fiber_densities_filtered, _n=DERIVATIVE),
                        compute_lib.derivative(_different_fiber_densities_filtered, _n=DERIVATIVE)
                    )

                    _same_correlations_arrays[_same_index].append(_same_correlation)
                    _different_correlations_arrays[_same_index].append(_different_correlation)

        _names_array.append('Cells ' + _same_group.split('_')[1] + ' & ' + _same_group.split('_')[2])
        print('Group:', TRIPLET[_same_index])
        print('Points:', len(_same_correlations_arrays[_same_index]))
        _same_minus_different = \
            np.array(_same_correlations_arrays[_same_index]) - np.array(_different_correlations_arrays[_same_index])
        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))

    print('Total points:', len(np.array(_same_correlations_arrays).flatten()))
    _same_minus_different = \
        np.array(_same_correlations_arrays).flatten() - np.array(_different_correlations_arrays).flatten()
    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
    _colors_array = config.colors(3)
    _fig = go.Figure(
        data=[
            go.Scatter(
                x=_same_correlations_array,
                y=_different_correlations_array,
                name=_name,
                mode='markers',
                marker={
                    'size': 15,
                    'color': _color
                },
                opacity=0.7
            ) for _same_correlations_array, _different_correlations_array, _name, _color in
            zip(_same_correlations_arrays, _different_correlations_arrays, _names_array, _colors_array)
        ],
        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]
            },
            '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'
    )
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():
    _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():
    _experiments = all_experiments()
    _experiments = filtering.by_categories(
        _experiments=_experiments,
        _is_single_cell=False,
        _is_high_temporal_resolution=False,
        _is_bleb=False,
        _is_dead_dead=False,
        _is_live_dead=False,
        _is_bead=False,
        _is_metastasis=False
    )

    _tuples = load.experiments_groups_as_tuples(_experiments)
    _tuples = filtering.by_real_pairs(_tuples)
    _tuples = filtering.by_band(_tuples)
    _tuples = filtering.by_time_frames_amount(_tuples, MINIMUM_TIME_FRAMES)
    _tuples = filtering.by_pair_distance_range(_tuples, [MINIMUM_PAIR_DISTANCE, sys.maxsize])
    _triplets = filtering.by_triplets(_tuples)
    print('Total triplets:', len(_triplets))

    _arguments = []
    for _triplet in _triplets:
        for _tuple in _triplet:
            _experiment, _series_id, _group = _tuple
            _latest_time_frame = compute.latest_time_frame_before_overlapping(_experiment, _series_id, _group, OFFSET_X)
            for _cell_id in ['left_cell', 'right_cell']:
                _arguments.append({
                    'experiment': _experiment,
                    'series_id': _series_id,
                    'group': _group,
                    'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                    'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                    'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                    'offset_x': OFFSET_X,
                    'offset_y': OFFSET_Y,
                    'offset_z': OFFSET_Z,
                    'cell_id': _cell_id,
                    'direction': 'inside',
                    'time_points': _latest_time_frame
                })

    _windows_dictionary, _windows_to_compute = compute.windows(_arguments,
                                                               _keys=['experiment', 'series_id', 'group', 'cell_id'])
    _fiber_densities = compute.fiber_densities(_windows_to_compute, _subtract_border=True)

    _experiments_fiber_densities = {
        _key: [_fiber_densities[_tuple] for _tuple in _windows_dictionary[_key]]
        for _key in _windows_dictionary
    }

    _same_correlations_arrays = [[] for _i in _triplets]
    _different_correlations_arrays = [[] for _i in _triplets]
    _names_array = []
    for _triplet_index, _triplet in enumerate(_triplets):
        for _same_index in tqdm(range(len(_triplet)), desc='Main loop'):
            _same_tuple = _triplet[_same_index]
            _same_experiment, _same_series, _same_group = _same_tuple

            _same_left_cell_fiber_densities = \
                _experiments_fiber_densities[
                    (_same_experiment, _same_series, _same_group, 'left_cell')
                ]
            _same_right_cell_fiber_densities = \
                _experiments_fiber_densities[
                    (_same_experiment, _same_series, _same_group, 'right_cell')
                ]

            _same_properties = \
                load.group_properties(_same_experiment, _same_series, _same_group)
            _same_left_cell_fiber_densities = compute.remove_blacklist(
                _same_experiment,
                _same_series,
                _same_properties['cells_ids']['left_cell'],
                _same_left_cell_fiber_densities
            )
            _same_right_cell_fiber_densities = compute.remove_blacklist(
                _same_experiment,
                _same_series,
                _same_properties['cells_ids']['right_cell'],
                _same_right_cell_fiber_densities
            )

            _same_left_cell_fiber_densities_filtered, _same_right_cell_fiber_densities_filtered = \
                compute.longest_same_indices_shared_in_borders_sub_array(
                    _same_left_cell_fiber_densities, _same_right_cell_fiber_densities
                )

            _same_correlation = compute_lib.correlation(
                compute_lib.derivative(_same_left_cell_fiber_densities_filtered, _n=DERIVATIVE),
                compute_lib.derivative(_same_right_cell_fiber_densities_filtered, _n=DERIVATIVE)
            )
            for _different_index in range(len(_triplet)):
                if _same_index != _different_index:
                    _different_tuple = _triplet[_different_index]
                    _different_experiment, _different_series, _different_group = \
                        _different_tuple
                    for _same_cell_id, _different_cell_id in product(['left_cell', 'right_cell'],
                                                                     ['left_cell', 'right_cell']):
                        _same_fiber_densities = _experiments_fiber_densities[(
                            _same_experiment,
                            _same_series,
                            _same_group,
                            _same_cell_id
                        )]
                        _different_fiber_densities = _experiments_fiber_densities[(
                            _different_experiment,
                            _different_series,
                            _different_group,
                            _different_cell_id
                        )]

                        _different_properties = load.group_properties(
                            _different_experiment, _different_series, _different_group
                        )
                        _same_fiber_densities = compute.remove_blacklist(
                            _same_experiment,
                            _same_series,
                            _same_properties['cells_ids'][_same_cell_id],
                            _same_fiber_densities
                        )
                        _different_fiber_densities = compute.remove_blacklist(
                            _different_experiment,
                            _different_series,
                            _different_properties['cells_ids'][_different_cell_id],
                            _different_fiber_densities
                        )

                        _same_fiber_densities_filtered, _different_fiber_densities_filtered = \
                            compute.longest_same_indices_shared_in_borders_sub_array(
                                _same_fiber_densities, _different_fiber_densities
                            )

                        _different_correlation = compute_lib.correlation(
                            compute_lib.derivative(_same_fiber_densities_filtered, _n=DERIVATIVE),
                            compute_lib.derivative(_different_fiber_densities_filtered, _n=DERIVATIVE)
                        )

                        _same_correlations_arrays[_triplet_index].append(_same_correlation)
                        _different_correlations_arrays[_triplet_index].append(_different_correlation)

        _names_array.append('Triplet #' + str(_triplet_index + 1))

    print('Total points:', len(np.array(_same_correlations_arrays).flatten()))
    _same_minus_different = \
        np.array(_same_correlations_arrays).flatten() - np.array(_different_correlations_arrays).flatten()
    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
    _colors_array = ['green', 'blue', config.colors(1)]
    _fig = go.Figure(
        data=[
            go.Scatter(
                x=_same_correlations_array,
                y=_different_correlations_array,
                name=_name,
                mode='markers',
                marker={
                    'size': 15,
                    'color': _color
                },
                opacity=0.7
            ) for _same_correlations_array, _different_correlations_array, _name, _color in
            zip(_same_correlations_arrays, _different_correlations_arrays, _names_array, _colors_array)
        ],
        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]
            },
            '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'
    )
def main(_high_temporal_resolution=True):
    _experiments = all_experiments()
    _experiments = filtering.by_categories(
        _experiments=_experiments,
        _is_single_cell=False,
        _is_high_temporal_resolution=_high_temporal_resolution,
        _is_bleb=False,
        _is_dead_dead=False,
        _is_live_dead=False,
        _is_bead=False,
        _is_metastasis=False)

    _tuples = load.experiments_groups_as_tuples(_experiments)
    _tuples = filtering.by_time_frames_amount(
        _tuples, _time_frames=MOVING_WINDOW_LENGTH[_high_temporal_resolution])
    _tuples = filtering.by_pair_distance_range(
        _tuples, _distance_range=PAIR_DISTANCE_RANGE)
    _tuples = filtering.by_real_pairs(_tuples)
    _tuples = filtering.by_band(_tuples)
    print('Total tuples:', len(_tuples))

    _arguments = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        _latest_time_frame = compute.latest_time_frame_before_overlapping(
            _experiment, _series_id, _group, OFFSET_X)
        for _cell_id in ['left_cell', 'right_cell']:
            _arguments.append({
                'experiment': _experiment,
                'series_id': _series_id,
                'group': _group,
                'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                'offset_x': OFFSET_X,
                'offset_y': OFFSET_Y,
                'offset_z': OFFSET_Z,
                'cell_id': _cell_id,
                'direction': 'inside',
                'time_points': _latest_time_frame
            })

    _windows_dictionary, _windows_to_compute = compute.windows(
        _arguments, _keys=['experiment', 'series_id', 'group', 'cell_id'])
    _fiber_densities = compute.fiber_densities(_windows_to_compute,
                                               _subtract_border=True)

    _experiments_fiber_densities = {
        _key:
        [_fiber_densities[_tuple] for _tuple in _windows_dictionary[_key]]
        for _key in _windows_dictionary
    }

    for _tuple in _tuples:
        _correlations = []
        _experiment, _series_id, _group = _tuple

        _left_cell_fiber_densities = \
            _experiments_fiber_densities[(_experiment, _series_id, _group, 'left_cell')]
        _right_cell_fiber_densities = \
            _experiments_fiber_densities[(_experiment, _series_id, _group, 'right_cell')]

        _properties = load.group_properties(_experiment, _series_id, _group)
        _left_cell_fiber_densities = compute.remove_blacklist(
            _experiment, _series_id, _properties['cells_ids']['left_cell'],
            _left_cell_fiber_densities)
        _right_cell_fiber_densities = compute.remove_blacklist(
            _experiment, _series_id, _properties['cells_ids']['right_cell'],
            _right_cell_fiber_densities)

        for _start_time_frame in \
                range(0, END_TIME_FRAME[_high_temporal_resolution], TIME_FRAME_STEP[_high_temporal_resolution]):

            _left_cell_fiber_densities_window = _left_cell_fiber_densities[
                _start_time_frame:_start_time_frame +
                MOVING_WINDOW_LENGTH[_high_temporal_resolution]]
            _right_cell_fiber_densities_window = _right_cell_fiber_densities[
                _start_time_frame:_start_time_frame +
                MOVING_WINDOW_LENGTH[_high_temporal_resolution]]

            _left_cell_fiber_densities_filtered, _right_cell_fiber_densities_filtered = \
                compute.longest_same_indices_shared_in_borders_sub_array(
                    _left_cell_fiber_densities_window, _right_cell_fiber_densities_window)

            # ignore small arrays
            if len(_left_cell_fiber_densities_filtered
                   ) < MOVING_WINDOW_LENGTH[_high_temporal_resolution]:
                _correlations.append(None)
                continue

            _correlations.append(
                compute_lib.correlation(
                    compute_lib.derivative(_left_cell_fiber_densities_filtered,
                                           _n=DERIVATIVE),
                    compute_lib.derivative(
                        _right_cell_fiber_densities_filtered, _n=DERIVATIVE)))

        # plot
        _temporal_resolution = compute.temporal_resolution_in_minutes(
            _experiment)
        _fig = go.Figure(data=go.Scatter(
            x=np.arange(start=0, stop=len(_correlations), step=1) *
            _temporal_resolution * TIME_FRAME_STEP[_high_temporal_resolution],
            y=_correlations,
            mode='lines+markers',
            line={'dash': 'solid'}),
                         layout={
                             'xaxis': {
                                 'title': 'Window start time (minutes)',
                                 'zeroline': False
                             },
                             'yaxis': {
                                 'title': 'Inner correlation',
                                 'zeroline': False
                             }
                         })

        save.to_html(_fig=_fig,
                     _path=os.path.join(paths.PLOTS, save.get_module_name()),
                     _filename='plot_' + str(_experiment) + '_' +
                     str(_series_id) + '_' + str(_group))
def main(_directions=None):
    if _directions is None:
        _directions = ['inside', 'outside']

    _experiments = all_experiments()
    _experiments = filtering.by_categories(
        _experiments=_experiments,
        _is_single_cell=False,
        _is_high_temporal_resolution=False,
        _is_bleb=False,
        _is_dead_dead=False,
        _is_live_dead=False,
        _is_bead=False,
        _is_metastasis=False
    )

    _tuples = load.experiments_groups_as_tuples(_experiments)
    _tuples = filtering.by_real_pairs(_tuples)
    _tuples = filtering.by_band(_tuples)
    _tuples = filtering.by_pair_distance_range(_tuples, PAIR_DISTANCE_RANGE)
    print('Total tuples:', len(_tuples))

    _arguments = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        _latest_time_frame = compute.latest_time_frame_before_overlapping(_experiment, _series_id, _group, OFFSET_X)
        for _cell_id, _direction in product(['left_cell', 'right_cell'], _directions):
            _arguments.append({
                'experiment': _experiment,
                'series_id': _series_id,
                'group': _group,
                'length_x': QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                'length_y': QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                'length_z': QUANTIFICATION_WINDOW_WIDTH_IN_CELL_DIAMETER,
                'offset_x': OFFSET_X,
                'offset_y': OFFSET_Y,
                'offset_z': OFFSET_Z,
                'cell_id': _cell_id,
                'direction': _direction,
                'time_points': _latest_time_frame
            })

    _windows_dictionary, _windows_to_compute = \
        compute.windows(_arguments, _keys=['experiment', 'series_id', 'group', 'cell_id', 'direction'])
    _fiber_densities = compute.fiber_densities(_windows_to_compute, _subtract_border=True)

    _experiments_fiber_densities = {
        _key: [_fiber_densities[_tuple] for _tuple in _windows_dictionary[_key]]
        for _key in _windows_dictionary
    }

    for _direction in _directions:
        _y_arrays = [[] for _i in DERIVATIVES]
        for _tuple in tqdm(_tuples, desc='Experiments loop'):
            _experiment, _series_id, _group = _tuple

            if (_experiment, _series_id, _group, 'left_cell', _direction) not in _windows_dictionary or \
                    (_experiment, _series_id, _group, 'right_cell', _direction) not in _windows_dictionary:
                continue

            _properties = load.group_properties(_experiment, _series_id, _group)

            _left_cell_fiber_densities = \
                _experiments_fiber_densities[(_experiment, _series_id, _group, 'left_cell', _direction)]
            _right_cell_fiber_densities = \
                _experiments_fiber_densities[(_experiment, _series_id, _group, 'right_cell', _direction)]

            _left_cell_fiber_densities = compute.remove_blacklist(
                _experiment, _series_id, _properties['cells_ids']['left_cell'], _left_cell_fiber_densities)
            _right_cell_fiber_densities = compute.remove_blacklist(
                _experiment, _series_id, _properties['cells_ids']['right_cell'], _right_cell_fiber_densities)

            if not OUT_OF_BOUNDARIES:
                _left_cell_fiber_densities, _right_cell_fiber_densities = \
                    compute.longest_same_indices_shared_in_borders_sub_array(
                        _left_cell_fiber_densities, _right_cell_fiber_densities
                    )
            else:
                _left_cell_fiber_densities = [_fiber_density[0] for _fiber_density in _left_cell_fiber_densities]
                _right_cell_fiber_densities = [_fiber_density[0] for _fiber_density in _right_cell_fiber_densities]

            # ignore small arrays
            _minimum_time_frame_for_correlation = compute.minimum_time_frames_for_correlation(_experiment)
            if len(_left_cell_fiber_densities) < _minimum_time_frame_for_correlation or \
                    len(_right_cell_fiber_densities) < _minimum_time_frame_for_correlation:
                continue

            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
        )