Beispiel #1
0
def compute_single_cell_mean(_experiment, _series_id, _cell_tuples,
                             _windows_dictionary, _fiber_densities):
    _cell_fiber_densities = []
    for _time_frame in range(compute.density_time_frame(_experiment)):
        _time_frame_fiber_densities = []
        for _cell_tuple in _cell_tuples:
            _, _, _group = _cell_tuple
            for _direction in ['left', 'right']:
                _window_tuple = _windows_dictionary[(_experiment, _series_id,
                                                     _group,
                                                     _direction)][_time_frame]
                _fiber_density = _fiber_densities[_window_tuple]

                if not OUT_OF_BOUNDARIES and _fiber_density[1]:
                    continue

                _time_frame_fiber_densities.append(_fiber_density)

        _cell_fiber_densities.append(np.mean(_time_frame_fiber_densities))

    return _cell_fiber_densities
def compute_matched_fiber(_tuples):
    _tuples = filtering.by_time_frames_amount(
        _tuples, compute.density_time_frame(_tuples[0]))
    _tuples = filtering.by_pair_distance_range(_tuples, PAIR_DISTANCE_RANGE)
    _experiments_matched = organize.by_matched_real_and_fake(_tuples)
    print('Total matched pairs:', len(_experiments_matched))

    _max_offsets_x = []
    _arguments = []
    for _matched_tuple in _experiments_matched:
        for _tuple in _matched_tuple:
            _experiment, _series_id, _group = _tuple
            _time_frame = compute.density_time_frame(_experiment)
            _pair_distance = compute.pair_distance_in_cell_size_time_frame(
                _experiment, _series_id, _group, _time_frame - 1)
            _offsets_x = \
                np.arange(start=0, stop=_pair_distance / 2 - 0.5 - QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                          step=OFFSET_X_STEP)
            if len(_offsets_x) > len(_max_offsets_x):
                _max_offsets_x = _offsets_x
            for _offset_x in _offsets_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_point': _time_frame - 1
                    })

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

    _experiments_fiber_densities_real = [[]
                                         for _i in range(len(_max_offsets_x))]
    _experiments_fiber_densities_fake = [[]
                                         for _i in range(len(_max_offsets_x))]
    for _tuple in tqdm(_experiments_matched, desc='Experiments loop'):
        _tuple_real, _tuple_fake = _tuple

        _experiment_real, _series_id_real, _group_real = _tuple_real
        _experiment_fake, _series_id_fake, _group_fake = _tuple_fake

        for _offset_x_index, _offset_x in enumerate(_max_offsets_x):
            for _cell_id in ['left_cell', 'right_cell']:
                if (_experiment_real, _series_id_real, _group_real, _offset_x, _cell_id) and \
                        (_experiment_fake, _series_id_fake, _group_fake, _offset_x, _cell_id) in _windows_dictionary:
                    _normalization = load.normalization_series_file_data(
                        _experiment_real, _series_id_real)

                    _window_tuple_real = \
                        _windows_dictionary[(_experiment_real, _series_id_real, _group_real, _offset_x, _cell_id)][0]
                    _fiber_density_real = _fiber_densities[_window_tuple_real]

                    _window_tuple_fake = \
                        _windows_dictionary[(_experiment_fake, _series_id_fake, _group_fake, _offset_x, _cell_id)][0]
                    _fiber_density_fake = _fiber_densities[_window_tuple_fake]

                    if not OUT_OF_BOUNDARIES and (_fiber_density_real[1]
                                                  or _fiber_density_fake[1]):
                        continue

                    _normalized_fiber_density_real = compute_lib.z_score(
                        _x=_fiber_density_real[0],
                        _average=_normalization['average'],
                        _std=_normalization['std'])
                    _normalized_fiber_density_fake = compute_lib.z_score(
                        _x=_fiber_density_fake[0],
                        _average=_normalization['average'],
                        _std=_normalization['std'])

                    if not np.isnan(
                            _normalized_fiber_density_real) and not np.isnan(
                                _normalized_fiber_density_fake):
                        _experiments_fiber_densities_real[
                            _offset_x_index].append(
                                _normalized_fiber_density_real)
                        _experiments_fiber_densities_fake[
                            _offset_x_index].append(
                                _normalized_fiber_density_fake)

    return _experiments_fiber_densities_real, _experiments_fiber_densities_fake, _max_offsets_x
def compute_experiments_data():
    _experiments = all_experiments()
    _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 = experiments_load.experiments_groups_as_tuples(_experiments)
    _tuples = experiments_filtering.by_time_frames_amount(_tuples, EXPERIMENTS_TIME_FRAMES)
    _tuples = experiments_filtering.by_real_pairs(_tuples)
    _tuples = experiments_filtering.by_pair_distance_range(_tuples, PAIR_DISTANCE_RANGE)
    _tuples = experiments_filtering.by_band(_tuples)
    print('Total tuples:', len(_tuples))

    _arguments = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        _time_frame = experiments_compute.density_time_frame(_experiment)
        for _cell_id in ['left_cell', 'right_cell']:
            _arguments.append({
                'experiment': _experiment,
                'series_id': _series_id,
                'group': _group,
                'length_x': experiments_config.QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                'length_y': experiments_config.QUANTIFICATION_WINDOW_HEIGHT_IN_CELL_DIAMETER,
                'length_z': experiments_config.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': _time_frame
            })

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

    _experiments_fiber_densities = [[] for _i in range(EXPERIMENTS_TIME_FRAMES)]
    for _tuple in tqdm(_tuples, desc='Experiments loop'):
        _experiment, _series_id, _group = _tuple
        _normalization = experiments_load.normalization_series_file_data(_experiment, _series_id)

        for _time_frame in range(EXPERIMENTS_TIME_FRAMES):
            for _cell_id in ['left_cell', 'right_cell']:
                _window_tuple = _windows_dictionary[(_experiment, _series_id, _group, _cell_id)][_time_frame]
                _fiber_density = _fiber_densities[_window_tuple]

                if not OUT_OF_BOUNDARIES and _fiber_density[1]:
                    continue

                _normalized_fiber_density = compute_lib.z_score(
                    _x=_fiber_density[0],
                    _average=_normalization['average'],
                    _std=_normalization['std']
                )

                if not np.isnan(_normalized_fiber_density):
                    _experiments_fiber_densities[_time_frame].append(_normalized_fiber_density)

    print('Total experiments pairs:', len(_experiments_fiber_densities[0]))

    return _experiments_fiber_densities
Beispiel #4
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)
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)

    _max_offsets_x = []
    _arguments = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        _time_frame = compute.minimum_time_frames_for_correlation(_experiment)
        _pair_distance = \
            compute.pair_distance_in_cell_size_time_frame(_experiment, _series_id, _group, _time_frame=_time_frame - 1)
        _offsets_x = \
            np.arange(start=0, stop=_pair_distance / 2 - 0.5 - QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                      step=OFFSET_X_STEP)
        if len(_offsets_x) > len(_max_offsets_x):
            _max_offsets_x = _offsets_x
        for _offset_x in _offsets_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_point': _time_frame - 1
                })

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

    _x_array = []
    _y_array = []
    _n_array = []
    _p_value_array = []
    for _offset_x in _max_offsets_x:
        _pair_distances = []
        _z_scores = []
        for _tuple in _tuples:
            _experiment, _series_id, _group = _tuple
            for _cell_id in ['left_cell', 'right_cell']:
                if (_experiment, _series_id, _group, _offset_x,
                        _cell_id) in _windows_dictionary:
                    _normalization = load.normalization_series_file_data(
                        _experiment, _series_id)
                    _window_tuple = _windows_dictionary[(_experiment,
                                                         _series_id, _group,
                                                         _offset_x,
                                                         _cell_id)][0]
                    _fiber_density = _fiber_densities[_window_tuple]

                    if not OUT_OF_BOUNDARIES and _fiber_density[1]:
                        continue

                    _normalized_fiber_density = compute_lib.z_score(
                        _x=_fiber_density[0],
                        _average=_normalization['average'],
                        _std=_normalization['std'])

                    if not np.isnan(_normalized_fiber_density):
                        _pair_distances.append(
                            compute.pair_distance_in_cell_size_time_frame(
                                _experiment, _series_id, _group,
                                _time_frame=0))
                        _z_scores.append(_normalized_fiber_density)

        if len(_pair_distances) > 2:
            _x_array.append(round(_offset_x, 1))
            _correlation = compute_lib.correlation(_pair_distances,
                                                   _z_scores,
                                                   _with_p_value=True)
            _y_array.append(round(_correlation[0], 2))
            _n_array.append(len(_pair_distances))
            _p_value_array.append(round(_correlation[1], 2))

    print('Pearson:')
    print(compute_lib.correlation(_x_array, _y_array, _with_p_value=True))

    # plot
    _significant_x_array = [
        _x for _x, _p_value in zip(_x_array, _p_value_array) if _p_value < 0.05
    ]
    _fig = go.Figure(data=[
        go.Scatter(x=_x_array,
                   y=_y_array,
                   mode='markers',
                   marker={
                       'size': 15,
                       'color': 'black'
                   },
                   showlegend=False),
        go.Scatter(x=_significant_x_array,
                   y=[-0.79] * len(_significant_x_array),
                   mode='text',
                   text='*',
                   textfont={'color': 'red'},
                   showlegend=False)
    ],
                     layout={
                         'xaxis': {
                             'title': 'Window distance (cell diameter)',
                             'zeroline': False
                         },
                         'yaxis': {
                             'title':
                             'Correlation:<br>pair distance vs. fiber density',
                             'zeroline': False,
                             'range': [-0.82, 0.3],
                             'tickmode': 'array',
                             'tickvals': [-0.75, -0.25, 0.25]
                         },
                         'shapes': [{
                             'type': 'line',
                             'x0': -0.2,
                             'y0': -0.8,
                             'x1': 3.2,
                             'y1': -0.8,
                             'line': {
                                 'color': 'black',
                                 'width': 2
                             }
                         }, {
                             'type': 'line',
                             'x0': -0.2,
                             'y0': -0.8,
                             'x1': -0.2,
                             'y1': 0.3,
                             'line': {
                                 'color': 'black',
                                 'width': 2
                             }
                         }]
                     })

    save.to_html(_fig=_fig,
                 _path=os.path.join(paths.PLOTS, save.get_module_name()),
                 _filename='plot')

    # table
    print('Window distance (cell diameter)',
          'Correlation: pair distance vs. fiber density',
          'N',
          'P-value',
          sep='\t')
    for _x, _y, _n, _p_value in zip(_x_array, _y_array, _n_array,
                                    _p_value_array):
        print(_x, _y, _n, _p_value, sep='\t')
Beispiel #6
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)
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')
Beispiel #8
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)
    print('Total tuples:', len(_tuples))

    _arguments = []
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        _time_frame = compute.density_time_frame(_experiment)
        _pair_distance = \
            compute.pair_distance_in_cell_size_time_frame(_experiment, _series_id, _group, _time_frame=_time_frame - 1)
        for _offset_x in OFFSETS_X:
            if _pair_distance / 2 - 0.5 - QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER >= _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_point': _time_frame - 1
                    })

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

    for _offset_x in OFFSETS_X:
        _x_array = []
        _y_array = []
        for _tuple in _tuples:
            _experiment, _series_id, _group = _tuple
            for _cell_id in ['left_cell', 'right_cell']:
                if (_experiment, _series_id, _group, _cell_id,
                        _offset_x) in _windows_dictionary:
                    _pair_distance = \
                        compute.pair_distance_in_cell_size_time_frame(_experiment, _series_id, _group, _time_frame=0)
                    _normalization = load.normalization_series_file_data(
                        _experiment, _series_id)
                    _window_tuple = _windows_dictionary[(_experiment,
                                                         _series_id, _group,
                                                         _cell_id,
                                                         _offset_x)][0]
                    _fiber_density = _fiber_densities[_window_tuple]

                    if not OUT_OF_BOUNDARIES and _fiber_density[1]:
                        continue

                    _normalized_fiber_density = compute_lib.z_score(
                        _x=_fiber_density[0],
                        _average=_normalization['average'],
                        _std=_normalization['std'])

                    if not np.isnan(_normalized_fiber_density):
                        _x_array.append(_pair_distance)
                        _y_array.append(_normalized_fiber_density)

        print('Offset x (cell diameter):', _offset_x)
        print('Total pairs:', len(_x_array))
        print(compute_lib.correlation(_x_array, _y_array, _with_p_value=True))

        # plot
        _fig = go.Figure(data=go.Scatter(x=_x_array,
                                         y=_y_array,
                                         mode='markers',
                                         marker={
                                             'size': 15,
                                             'color': 'black'
                                         }),
                         layout={
                             'xaxis': {
                                 'title': 'Pair distance (cell diameter)',
                                 'zeroline': False
                             },
                             'yaxis': {
                                 'title': 'Fiber density (z-score)',
                                 'zeroline': False,
                                 'range': [-2.2, 13],
                                 'tickmode': 'array',
                                 'tickvals': [0, 4, 8, 12]
                             },
                             'shapes': [{
                                 'type': 'line',
                                 'x0': 4.5,
                                 'y0': -2,
                                 'x1': 9.5,
                                 'y1': -2,
                                 'line': {
                                     'color': 'black',
                                     'width': 2
                                 }
                             }, {
                                 'type': 'line',
                                 'x0': 4.5,
                                 'y0': -2,
                                 'x1': 4.5,
                                 'y1': 13,
                                 'line': {
                                     'color': 'black',
                                     'width': 2
                                 }
                             }]
                         })

        save.to_html(_fig=_fig,
                     _path=os.path.join(paths.PLOTS, save.get_module_name()),
                     _filename='plot_offset_x_' + str(_offset_x))
def compute_cell_pairs():
    _x_array = []
    _y_array = []
    _names_array = []
    for _distances_range in PAIR_DISTANCE_RANGES:
        print('Pair distance range:', str(_distances_range))
        _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_pair_distance_range(_tuples, _distances_range)
        _tuples = filtering.by_band(_tuples)
        print('Total tuples:', len(_tuples))

        _max_offsets_x = []
        _arguments = []
        for _tuple in _tuples:
            _experiment, _series_id, _group = _tuple
            _time_frame = compute.minimum_time_frames_for_correlation(
                _experiment)
            _pair_distance = \
                compute.pair_distance_in_cell_size_time_frame(_experiment, _series_id, _group, _time_frame - 1)
            _offsets_x = np.arange(
                start=0,
                stop=_pair_distance / 2 - 0.5 -
                QUANTIFICATION_WINDOW_LENGTH_IN_CELL_DIAMETER,
                step=OFFSET_X_STEP)
            if len(_offsets_x) > len(_max_offsets_x):
                _max_offsets_x = _offsets_x
            for _offset_x in _offsets_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_point': _time_frame - 1
                    })

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

        _pair_distance_fiber_densities = [[]
                                          for _i in range(len(_max_offsets_x))]
        for _tuple in _tuples:
            _experiment, _series_id, _group = _tuple
            for _offset_x_index, _offset_x in enumerate(_max_offsets_x):
                for _cell_id in ['left_cell', 'right_cell']:
                    if (_experiment, _series_id, _group, _offset_x,
                            _cell_id) in _windows_dictionary:
                        _normalization = load.normalization_series_file_data(
                            _experiment, _series_id)
                        _window_tuple = _windows_dictionary[(_experiment,
                                                             _series_id,
                                                             _group, _offset_x,
                                                             _cell_id)][0]
                        _fiber_density = _fiber_densities[_window_tuple]

                        if not OUT_OF_BOUNDARIES and _fiber_density[1]:
                            continue

                        _normalized_fiber_density = compute_lib.z_score(
                            _x=_fiber_density[0],
                            _average=_normalization['average'],
                            _std=_normalization['std'])

                        if not np.isnan(_normalized_fiber_density):
                            _pair_distance_fiber_densities[
                                _offset_x_index].append(
                                    _normalized_fiber_density)

        _x_array.append(_max_offsets_x)
        _y_array.append(_pair_distance_fiber_densities)
        _names_array.append('Pair distance ' + str(_distances_range[0]) + '-' +
                            str(_distances_range[1]))
    return _names_array, _x_array, _y_array
Beispiel #10
0
def main(_band=None,
         _high_temporal_resolution=True,
         _tuples_to_mark=None,
         _tuples_to_plot=None,
         _plots=None):
    if _plots is None:
        _plots = ['whiteness', 'granger']

    _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=MINIMUM_TIME_FRAMES)
    _tuples = filtering.by_pair_distance_range(
        _tuples, _distance_range=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
    }

    _n_pairs = 0
    _n_pairs_with_band = 0
    _whiteness_p_values = []
    _n_passed_whiteness_with_band = 0
    _granger_causality_p_values = []
    _n_passed_granger_causality_with_band = 0
    _correlations = []
    _time_lag_correlations = []
    _end_fiber_densities = []
    for _tuple in _tuples:
        _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)

        _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) < MINIMUM_TIME_FRAMES:
            continue

        _n_pairs += 1
        if _properties['band']:
            _n_pairs_with_band += 1

        _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

        # stationary test
        with warnings.catch_warnings():
            warnings.simplefilter('ignore', category=InterpolationWarning)

            # find derivative for stationary
            for _derivative in range(10):
                _left_cell_fiber_densities_derivative = \
                    compute_lib.derivative(_left_cell_fiber_densities_filtered, _n=_derivative)
                _right_cell_fiber_densities_derivative = \
                    compute_lib.derivative(_right_cell_fiber_densities_filtered, _n=_derivative)

                if ADF_TEST:
                    _, _left_cell_adf_p_value, _, _, _, _ = adfuller(
                        _left_cell_fiber_densities_derivative)
                    _, _right_cell_adf_p_value, _, _, _, _ = adfuller(
                        _right_cell_fiber_densities_derivative)
                    if _left_cell_adf_p_value > 0.05 or _right_cell_adf_p_value > 0.05:
                        continue

                if KPSS_TEST:
                    _, _left_cell_kpss_p_value, _, _ = kpss(
                        _left_cell_fiber_densities_derivative, nlags='legacy')
                    _, _right_cell_kpss_p_value, _, _ = kpss(
                        _right_cell_fiber_densities_derivative, nlags='legacy')
                    if _left_cell_kpss_p_value < 0.05 or _right_cell_kpss_p_value < 0.05:
                        continue

                # stationary
                break

        # causality
        try:
            _x = pd.DataFrame(data=[[_left_value, _right_value]
                                    for _left_value, _right_value in zip(
                                        _left_cell_fiber_densities_derivative,
                                        _right_cell_fiber_densities_derivative)
                                    ],
                              columns=['left', 'right'])

            # var model to retrieve lag
            _var_model = VAR(_x)
            _lag_order_results = _var_model.select_order()
            _estimators_lags = [
                _lag_order_results.aic, _lag_order_results.bic,
                _lag_order_results.fpe, _lag_order_results.hqic
            ]
            _min_estimator_lag = min(_estimators_lags)

            # found a lag
            if 0 < _min_estimator_lag <= MAXIMUM_LAG:
                _var_model_results = _var_model.fit(maxlags=_min_estimator_lag,
                                                    ic=None)

                _whiteness = _var_model_results.test_whiteness(
                    nlags=_min_estimator_lag + 1)
                _whiteness_p_values.append(_whiteness.pvalue)

                if _tuples_to_mark is not None and _tuple in _tuples_to_mark and _whiteness.pvalue > 0.05:
                    print(_tuple, 'marked whiteness p-value:',
                          _whiteness.pvalue)

                # no autocorrelation in the residuals
                if _whiteness.pvalue > 0.05:
                    if _properties['band']:
                        _n_passed_whiteness_with_band += 1

                    # time lag = 0
                    _correlation = compute_lib.correlation(
                        _left_cell_fiber_densities_derivative,
                        _right_cell_fiber_densities_derivative)

                    # if _correlation < 0.5:
                    #     continue

                    # granger causality
                    for _caused, _causing in zip(['left', 'right'],
                                                 ['right', 'left']):
                        _granger = _var_model_results.test_causality(
                            caused=_caused, causing=_causing)
                        _granger_causality_p_values.append(_granger.pvalue)

                        # time lag = 0
                        _correlations.append(_correlation)

                        # time lag = min estimator
                        if _causing == 'left':
                            _left_fiber_densities_time_lag = \
                                _left_cell_fiber_densities_derivative[:-_min_estimator_lag]
                            _right_fiber_densities_time_lag = \
                                _right_cell_fiber_densities_derivative[_min_estimator_lag:]
                        else:
                            _left_fiber_densities_time_lag = \
                                _left_cell_fiber_densities_derivative[_min_estimator_lag:]
                            _right_fiber_densities_time_lag = \
                                _right_cell_fiber_densities_derivative[:-_min_estimator_lag]
                        _time_lag_correlation = compute_lib.correlation(
                            _left_fiber_densities_time_lag,
                            _right_fiber_densities_time_lag)
                        _time_lag_correlations.append(_time_lag_correlation)

                        # end fiber density
                        _time_frame = compute.density_time_frame(_experiment)
                        if len(_left_cell_fiber_densities_filtered
                               ) > _time_frame:
                            _end_fiber_density = \
                                (_left_cell_fiber_densities_filtered[_time_frame] +
                                 _right_cell_fiber_densities_filtered[_time_frame]) / 2
                        else:
                            _end_fiber_density = \
                                (_left_cell_fiber_densities_filtered[-1] +
                                 _right_cell_fiber_densities_filtered[-1]) / 2
                        _normalization = load.normalization_series_file_data(
                            _experiment, _series_id)
                        _normalized_fiber_density = compute_lib.z_score(
                            _end_fiber_density, _normalization['average'],
                            _normalization['std'])
                        _end_fiber_densities.append(_normalized_fiber_density)

                        # marking
                        if _tuples_to_mark is not None and _tuple in _tuples_to_mark and _granger.pvalue < 0.05:
                            print(_tuple, 'causing:', _causing,
                                  'marked granger p-value:', _granger.pvalue)

                        if _granger.pvalue < 0.05:
                            if _properties['band']:
                                _n_passed_granger_causality_with_band += 1

                            _normality = _var_model_results.test_normality()
                            _inst_granger = _var_model_results.test_inst_causality(
                                causing=_causing)

                            print(
                                _tuple,
                                _causing.capitalize() + ' causes ' + _caused +
                                '!',
                                'time-points: ' +
                                str(len(
                                    _left_cell_fiber_densities_derivative)),
                                'stationary derivative: ' + str(_derivative),
                                'band:' + str(_properties['band']),
                                'p-value: ' + str(round(_granger.pvalue, 4)),
                                'lag: ' + str(_min_estimator_lag),
                                'normality p-value: ' +
                                str(round(_normality.pvalue, 4)),
                                'inst p-value: ' +
                                str(round(_inst_granger.pvalue, 4)),
                                sep='\t')

                            # lag = 0
                            print('Time lag = 0 correlation:', _correlation)

                            # rest of lags
                            for _lag in range(1, _min_estimator_lag + 1):
                                if _causing == 'left':
                                    _left_fiber_densities_time_lag = _left_cell_fiber_densities_derivative[:
                                                                                                           -_lag]
                                    _right_fiber_densities_time_lag = _right_cell_fiber_densities_derivative[
                                        _lag:]
                                else:
                                    _left_fiber_densities_time_lag = _left_cell_fiber_densities_derivative[
                                        _lag:]
                                    _right_fiber_densities_time_lag = _right_cell_fiber_densities_derivative[:
                                                                                                             -_lag]

                                _correlation = compute_lib.correlation(
                                    _left_fiber_densities_time_lag,
                                    _right_fiber_densities_time_lag)
                                print(
                                    'Time lag = ' + str(_lag) +
                                    ' correlation:', _correlation)

                            # plots
                            if _tuples_to_plot is not None and _tuple in _tuples_to_plot:
                                _y_arrays = [
                                    _left_cell_fiber_densities_derivative,
                                    _right_cell_fiber_densities_derivative
                                ]
                                _names_array = ['Left cell', 'Right cell']
                                _colors_array = config.colors(2)
                                _temporal_resolution = compute.temporal_resolution_in_minutes(
                                    _experiment)
                                _fig = go.Figure(data=[
                                    go.Scatter(x=np.arange(
                                        start=_start_time_frame,
                                        stop=_start_time_frame +
                                        len(_left_cell_fiber_densities_derivative
                                            ),
                                        step=1) * _temporal_resolution,
                                               y=_y,
                                               name=_name,
                                               mode='lines',
                                               line={
                                                   'color': _color,
                                                   'width': 1
                                               })
                                    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)'
                                                         + '\'' * _derivative,
                                                         'zeroline':
                                                         False
                                                     },
                                                     '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)

                                # residuals
                                _y_arrays = \
                                    [_var_model_results.resid.values[:, 0], _var_model_results.resid.values[:, 1]]
                                _fig = go.Figure(data=[
                                    go.Scatter(x=np.arange(
                                        start=_start_time_frame,
                                        stop=_start_time_frame + len(_y),
                                        step=1) * _temporal_resolution,
                                               y=_y,
                                               name=_name,
                                               mode='lines',
                                               line={
                                                   'color': _color,
                                                   'width': 1
                                               })
                                    for _y, _name, _color in zip(
                                        _y_arrays, _names_array, _colors_array)
                                ],
                                                 layout={
                                                     'xaxis': {
                                                         'title':
                                                         'Time (minutes)',
                                                         'zeroline': False
                                                     },
                                                     'yaxis': {
                                                         'title': 'Residual',
                                                         'zeroline': False
                                                     },
                                                     '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_residuals_' + _experiment +
                                    '_' + str(_series_id) + '_' + _group)

        # not enough time points
        except ValueError:
            continue

    print('Total pairs:', _n_pairs)
    print('Total pairs with band:', _n_pairs_with_band)
    print('Total pairs passed whiteness:',
          (np.array(_whiteness_p_values) > 0.05).sum())
    print('Total pairs passed whiteness with band:',
          _n_passed_whiteness_with_band)
    print('Total cells passed granger causality:',
          (np.array(_granger_causality_p_values) < 0.05).sum())
    print('Total cells passed granger causality with band:',
          _n_passed_granger_causality_with_band)

    # p-value correction
    print('Corrections of GC p-value < 0.05:')
    _granger_causality_p_values_corrected = multipletests(
        pvals=_granger_causality_p_values, method='fdr_bh')
    for _p_value, _p_value_corrected in zip(
            _granger_causality_p_values,
            _granger_causality_p_values_corrected[1]):
        if _p_value < 0.05:
            print('Original GC p-value:', _p_value, 'corrected:',
                  _p_value_corrected)

    # plots
    for _test_name, _y_title, _y_array in \
            zip(
                ['whiteness', 'granger'],
                ['Whiteness p-value', 'Granger causality p-value'],
                [_whiteness_p_values, _granger_causality_p_values]
            ):
        if _test_name in _plots:
            _fig = go.Figure(data=go.Box(y=_y_array,
                                         boxpoints='all',
                                         jitter=1,
                                         pointpos=0,
                                         line={'width': 1},
                                         fillcolor='white',
                                         marker={
                                             'size': 10,
                                             'color': '#ea8500'
                                         },
                                         opacity=0.7,
                                         showlegend=False),
                             layout={
                                 'xaxis': {
                                     'zeroline': False
                                 },
                                 'yaxis': {
                                     'title': _y_title,
                                     'zeroline': False,
                                     'range': [-0.1, 1.1],
                                     'tickmode': 'array',
                                     'tickvals': [0.05, 1]
                                 },
                                 'shapes': [{
                                     'type': 'line',
                                     'x0': -0.75,
                                     'y0': 0.05,
                                     'x1': 0.75,
                                     'y1': 0.05,
                                     '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)

    # granger versus correlation
    print(
        'GC vs. correlation pearson correlation:',
        compute_lib.correlation(_granger_causality_p_values,
                                _correlations,
                                _with_p_value=True))
    _fig = go.Figure(data=go.Scatter(x=_granger_causality_p_values,
                                     y=_correlations,
                                     mode='markers',
                                     marker={
                                         'size': 10,
                                         'color': '#ea8500'
                                     },
                                     showlegend=False),
                     layout={
                         'xaxis': {
                             'title': 'Granger causality p-value',
                             '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_gc_vs_correlation')

    # granger versus time lag correlation
    print(
        'GC vs. time lag correlation pearson correlation:',
        compute_lib.correlation(_granger_causality_p_values,
                                _time_lag_correlations,
                                _with_p_value=True))
    _fig = go.Figure(data=go.Scatter(x=_granger_causality_p_values,
                                     y=_time_lag_correlations,
                                     mode='markers',
                                     marker={
                                         'size': 10,
                                         'color': '#ea8500'
                                     },
                                     showlegend=False),
                     layout={
                         'xaxis': {
                             'title': 'Granger causality p-value',
                             'zeroline': False,
                         },
                         'yaxis': {
                             'title': 'GC lag inner correlation',
                             'zeroline': False,
                         }
                     })

    save.to_html(_fig=_fig,
                 _path=os.path.join(paths.PLOTS, save.get_module_name()),
                 _filename='plot_gc_vs_time_lag_correlation')

    # granger versus end fiber density
    print(
        'GC vs. end fiber density pearson correlation:',
        compute_lib.correlation(_granger_causality_p_values,
                                _end_fiber_densities,
                                _with_p_value=True))
    _fig = go.Figure(data=go.Scatter(x=_granger_causality_p_values,
                                     y=_end_fiber_densities,
                                     mode='markers',
                                     marker={
                                         'size': 10,
                                         'color': '#ea8500'
                                     },
                                     showlegend=False),
                     layout={
                         'xaxis': {
                             'title': 'Granger causality p-value',
                             'zeroline': False,
                         },
                         'yaxis': {
                             'title': 'End fiber density (z-score)',
                             'zeroline': False,
                         }
                     })

    save.to_html(_fig=_fig,
                 _path=os.path.join(paths.PLOTS, save.get_module_name()),
                 _filename='plot_gc_vs_end_density')