def main(_offset_y=0.5):
    _correlations_array, _saturation_array = compute_fiber_densities(_offset_y)

    # plot
    _fig = go.Figure(data=go.Scatter(x=_saturation_array,
                                     y=_correlations_array,
                                     mode='markers',
                                     marker={
                                         'size': 10,
                                         'color': '#ea8500'
                                     },
                                     showlegend=False),
                     layout={
                         'xaxis': {
                             'title': 'End mean saturation fraction',
                             'zeroline': False,
                             'range': [-0.01, 0.1],
                             'tickmode': 'array',
                             'tickvals': [0, 0.05, 0.1]
                         },
                         'yaxis': {
                             'title': 'Inner windows correlation',
                             'zeroline': False,
                             'range': [-1.1, 1.2],
                             '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_offset_y_' + str(_offset_y))
def main(_real_cells=True,
         _static=False,
         _band=True,
         _high_temporal_resolution=False):
    _z_array = np.zeros(shape=(len(BY), len(BY)))
    for (_padding_index,
         _padding_by), (_space_index,
                        _space_by) in product(enumerate(BY), enumerate(BY)):
        print('Padding by: ', _padding_by, ', space by: ', _space_by)
        _same_correlations_array, _different_correlations_array = \
            same_inner_correlation_vs_different_inner_correlation.compute_fiber_densities(
                _real_cells=_real_cells, _static=_static, _band=_band, _high_temporal_resolution=_high_temporal_resolution,
                _padding_y_by=_padding_by, _padding_z_by=_padding_by, _space_y_by=_space_by, _space_z_by=_space_by
            )
        _same_minus_different = np.array(_same_correlations_array) - np.array(
            _different_correlations_array)
        _same_greater_than_different = (_same_minus_different >
                                        0).sum() / len(_same_minus_different)
        _z_array[_space_index, _padding_index] = _same_greater_than_different

    # plot
    _colors_array = ['black', 'white', config.colors(1)]
    _fig = go.Figure(data=go.Heatmap(
        x=BY,
        y=BY,
        z=_z_array,
        colorscale=sns.color_palette(_colors_array).as_hex(),
        colorbar={
            'tickmode': 'array',
            'tickvals': [0.5, 0.75, 1],
            'ticktext': ['0.5', 'Same > different', '1'],
            'tickangle': -90
        },
        showscale=True,
        zmin=0.5,
        zmax=1),
                     layout={
                         'xaxis': {
                             'title': 'Border size (cell diameter)',
                             'zeroline': False,
                             'tickmode': 'array',
                             'tickvals': [0, 0.5, 1, 1.5, 2]
                         },
                         'yaxis': {
                             'title': 'Space from window (cell diameter)',
                             'zeroline': False,
                             'tickmode': 'array',
                             'tickvals': [0, 0.5, 1, 1.5, 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))
Beispiel #3
0
def main(_band=True, _high_temporal_resolution=False):
    print('Computing fiber densities vs. offsets in axes:')
    _fiber_densities_z_array = inner_density_vs_offsets_in_axes.compute_z_array(
        _band=_band, _high_temporal_resolution=_high_temporal_resolution)

    print('Computing fiber density changes vs. offsets in axes:')
    _fiber_density_changes_z_array = inner_density_change_vs_offsets_in_axes.compute_z_array(
        _band=_band, _high_temporal_resolution=_high_temporal_resolution)

    _z_array = _fiber_density_changes_z_array / _fiber_densities_z_array

    # plot
    _offsets_y = np.arange(start=OFFSET_Y_START,
                           stop=OFFSET_Y_END + OFFSET_Y_STEP,
                           step=OFFSET_Y_STEP)
    _offsets_z = np.arange(start=OFFSET_Z_START,
                           stop=OFFSET_Z_END + OFFSET_Z_STEP,
                           step=OFFSET_Z_STEP)
    _colors_array = ['white', config.colors(1)]
    _fig = go.Figure(data=go.Heatmap(
        x=_offsets_z,
        y=_offsets_y,
        z=_z_array,
        colorscale=sns.color_palette(_colors_array).as_hex(),
        colorbar={
            'tickmode': 'array',
            'tickvals': [0, 0.25, 0.5],
            'ticktext': ['0.0', 'Z-score change / z-score', '0.5'],
            'tickangle': -90
        },
        showscale=True,
        zmin=-0,
        zmax=0.5),
                     layout={
                         'xaxis': {
                             'title': 'Offset in XY axis (cell diameter)',
                             'zeroline': False,
                             'tickmode': 'array',
                             'tickvals': [-4, -2, 0, 2, 4]
                         },
                         'yaxis': {
                             'title': 'Offset in Z axis (cell diameter)',
                             'zeroline': False,
                             'tickmode': 'array',
                             'tickvals': [-1, 0, 1, 2]
                         }
                     })

    save.to_html(_fig=_fig,
                 _path=os.path.join(paths.PLOTS, save.get_module_name()),
                 _filename='plot_high_time_' + str(_high_temporal_resolution) +
                 '_band_' + str(_band))
def main(_high_temporal_resolution=False):
    _y_arrays = [[], []]
    for _band_index, _band in enumerate([True, False]):
        print('Band:', _band)
        _same_correlations_array, _different_correlations_array = \
            same_inner_correlation_vs_different_inner_correlation.compute_fiber_densities(_band=_band,
                                                                                          _high_temporal_resolution=_high_temporal_resolution)
        for _same, _different in zip(_same_correlations_array, _different_correlations_array):
            _point_distance = compute_lib.distance_from_a_point_to_a_line(
                _line=[-1, -1, 1, 1],
                _point=[_same, _different]
            )
            if _same > _different:
                _y_arrays[_band_index].append(_point_distance)
            else:
                _y_arrays[_band_index].append(-_point_distance)

    # plot
    _colors_array = config.colors(2)
    _names_array = ['Band', 'No Band']
    _fig = go.Figure(
        data=[
            go.Box(
                y=_y_array,
                name=_name,
                boxpoints=False,
                line={
                    'width': 1,
                    'color': _color
                },
                showlegend=False
            ) for _y_array, _name, _color in zip(_y_arrays, _names_array, _colors_array)
        ],
        layout={
            'xaxis': {
                'zeroline': False
            },
            'yaxis': {
                'title': 'Same minus different correlation',
                'zeroline': False
            }
        }
    )

    save.to_html(
        _fig=_fig,
        _path=os.path.join(paths.PLOTS, save.get_module_name()),
        _filename='plot_high_temporal_res_' + str(_high_temporal_resolution)
    )
def main():
    print('Computing fiber vs. offsets in axes:')
    _fiber_z_array = inner_density_vs_offsets_in_axes.compute_z_array(
        _offset_y_start=OFFSET_Y_START,
        _offset_y_end=OFFSET_Y_END,
        _offset_z_start=OFFSET_Z_START,
        _offset_z_end=OFFSET_Z_END).flatten()

    print('Computing "same vs. different" vs. offset in axes:')
    _same_vs_different_z_array = \
        same_inner_correlation_vs_different_inner_correlation_offsets_in_axes.compute_z_array(
            _offset_y_start=OFFSET_Y_START, _offset_y_end=OFFSET_Y_END,
            _offset_z_start=OFFSET_Z_START, _offset_z_end=OFFSET_Z_END).flatten()

    print(
        'Correlation:',
        compute_lib.correlation(_fiber_z_array,
                                _same_vs_different_z_array,
                                _with_p_value=True))

    # plot
    _fig = go.Figure(data=go.Scatter(x=_fiber_z_array,
                                     y=_same_vs_different_z_array,
                                     mode='markers',
                                     marker={
                                         'size': 5,
                                         'color': '#ea8500'
                                     },
                                     showlegend=False),
                     layout={
                         'xaxis': {
                             'title': 'Fiber density (z-score)',
                             'zeroline': False
                         },
                         'yaxis': {
                             'title': '"same" > "different" (%)',
                             'zeroline': False
                         }
                     })

    save.to_html(_fig=_fig,
                 _path=os.path.join(paths.PLOTS, save.get_module_name()),
                 _filename='plot')
def main(_band=True, _high_temporal_resolution=False):
    _distances_from_y_equal_x, _z_positions_array = compute_fiber_densities(
        _band, _high_temporal_resolution)

    _min_z_position, _max_z_position = min(_z_positions_array), max(
        _z_positions_array)
    _z_positions_array_normalized = [
        (_z_position_value - _min_z_position) /
        (_max_z_position - _min_z_position)
        for _z_position_value in _z_positions_array
    ]

    # plot
    _fig = go.Figure(data=go.Scatter(x=_z_positions_array_normalized,
                                     y=_distances_from_y_equal_x,
                                     mode='markers',
                                     marker={
                                         'size': 5,
                                         'color': '#ea8500'
                                     },
                                     showlegend=False),
                     layout={
                         'xaxis': {
                             'title': 'Normalized mean Z distance',
                             'zeroline': False,
                             'range': [-0.1, 1.2],
                             'tickmode': 'array',
                             'tickvals': [0, 0.5, 1]
                         },
                         'yaxis': {
                             'title': 'Distance from y = x',
                             'zeroline': False,
                             'range': [-1.1, 1.2],
                             '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_band_' + str(_band) + '_high_temporal_res_' +
                 str(_high_temporal_resolution))
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))
def main():
    print('Regular experiments')
    _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_bleb_from_start(_experiments, _from_start=False)
    _regular_experiments, _regular_offsets_x = compute_fiber(_tuples)

    print('Bleb experiments')
    _experiments = all_experiments()
    _experiments = filtering.by_categories(_experiments=_experiments,
                                           _is_single_cell=False,
                                           _is_high_temporal_resolution=False,
                                           _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_bleb_from_start(_experiments, _from_start=True)
    _bleb_experiments_real, _bleb_experiments_fake, _bleb_offsets_x = compute_matched_fiber(
        _tuples)

    print('\nWindow distance (cell diameter)',
          'Regular # of cells',
          'Regular Wilcoxon p-value',
          'Bleb # of cells',
          'Bleb "real" Wilcoxon p-value',
          'Bleb "fake" Wilcoxon p-value',
          sep='\t')
    for _offset_x, _regular_experiment, _bleb_experiment_real, _bleb_experiment_fake in \
            zip(_regular_offsets_x, _regular_experiments, _bleb_experiments_real, _bleb_experiments_fake):
        print(round(_offset_x, 2),
              len(_regular_experiment),
              wilcoxon(_regular_experiment)[1],
              len(_bleb_experiment_real),
              wilcoxon(_bleb_experiment_real)[1],
              wilcoxon(_bleb_experiment_fake)[1],
              sep='\t')

    # bleb real vs. fake
    print('\nBleb real vs. fake wilcoxon')
    print('Window distance (cell diameter)', 'Wilcoxon p-value', sep='\t')
    for _offset_x, _bleb_experiment_real, _bleb_experiment_fake in \
            zip(_bleb_offsets_x, _bleb_experiments_real, _bleb_experiments_fake):
        print(round(_offset_x, 2),
              wilcoxon(_bleb_experiment_real, _bleb_experiment_fake)[1],
              sep='\t')

    # plot regular vs. bleb
    _fig = go.Figure(data=[
        go.Scatter(x=_regular_offsets_x,
                   y=[np.mean(_array) for _array in _regular_experiments],
                   name='No bleb',
                   error_y={
                       'type': 'data',
                       'array':
                       [np.std(_array) for _array in _regular_experiments],
                       'thickness': 1,
                       'color': '#005b96'
                   },
                   mode='markers',
                   marker={
                       'size': 15,
                       'color': '#005b96'
                   },
                   opacity=0.7),
        go.Scatter(x=_bleb_offsets_x,
                   y=[np.mean(_array) for _array in _bleb_experiments_real],
                   name='Bleb',
                   error_y={
                       'type':
                       'data',
                       'array':
                       [np.std(_array) for _array in _bleb_experiments_real],
                       'thickness':
                       1,
                       'color':
                       '#ea8500'
                   },
                   mode='markers',
                   marker={
                       'size': 15,
                       'color': '#ea8500'
                   },
                   opacity=0.7)
    ],
                     layout={
                         'xaxis': {
                             'title': 'Window distance (cell diameter)',
                             'zeroline': False
                         },
                         'yaxis': {
                             'title': 'Fiber density (z-score)',
                             'range': [-1.7, 13],
                             'zeroline': False,
                             'tickmode': 'array',
                             'tickvals': [0, 4, 8, 12]
                         },
                         'legend': {
                             'xanchor': 'right',
                             'yanchor': 'top',
                             'bordercolor': 'black',
                             'borderwidth': 2
                         },
                         'shapes': [{
                             'type': 'line',
                             'x0': -0.2,
                             'y0': -1.5,
                             'x1': 3.4,
                             'y1': -1.5,
                             'line': {
                                 'color': 'black',
                                 'width': 2
                             }
                         }, {
                             'type': 'line',
                             'x0': -0.2,
                             'y0': -1.5,
                             'x1': -0.2,
                             'y1': 13,
                             'line': {
                                 'color': 'black',
                                 'width': 2
                             }
                         }]
                     })

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

    # plot bleb real vs. bleb fake
    _fig = go.Figure(data=[
        go.Scatter(x=_regular_offsets_x,
                   y=[np.mean(_array) for _array in _bleb_experiments_real],
                   name='Bleb real',
                   error_y={
                       'type':
                       'data',
                       'array':
                       [np.std(_array) for _array in _bleb_experiments_real],
                       'thickness':
                       1,
                       'color':
                       '#005b96'
                   },
                   mode='markers',
                   marker={
                       'size': 15,
                       'color': '#005b96'
                   },
                   opacity=0.7),
        go.Scatter(x=_bleb_offsets_x,
                   y=[np.mean(_array) for _array in _bleb_experiments_fake],
                   name='Bleb fake',
                   error_y={
                       'type':
                       'data',
                       'array':
                       [np.std(_array) for _array in _bleb_experiments_fake],
                       'thickness':
                       1,
                       'color':
                       '#ea8500'
                   },
                   mode='markers',
                   marker={
                       'size': 15,
                       'color': '#ea8500'
                   },
                   opacity=0.7)
    ],
                     layout={
                         'xaxis': {
                             'title': 'Window distance (cell diameter)',
                             'zeroline': False
                         },
                         'yaxis': {
                             'title': 'Fiber density (z-score)',
                             'range': [-0.5, 1.5],
                             'zeroline': False,
                             'tickmode': 'array',
                             'tickvals': [-0.5, 0, 0.5, 1]
                         },
                         'legend': {
                             'xanchor': 'right',
                             'yanchor': 'top',
                             'bordercolor': 'black',
                             'borderwidth': 2
                         },
                         'shapes': [{
                             'type': 'line',
                             'x0': -0.2,
                             'y0': -0.45,
                             'x1': 3.4,
                             'y1': -0.45,
                             'line': {
                                 'color': 'black',
                                 'width': 2
                             }
                         }, {
                             'type': 'line',
                             'x0': -0.2,
                             'y0': -0.45,
                             'x1': -0.2,
                             'y1': 1.5,
                             'line': {
                                 'color': 'black',
                                 'width': 2
                             }
                         }]
                     })

    save.to_html(_fig=_fig,
                 _path=os.path.join(paths.PLOTS, save.get_module_name()),
                 _filename='plot_bleb_real_vs_fake')
Beispiel #9
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')
Beispiel #10
0
def main(_band=True,
         _high_temporal_resolution=True,
         _plots=None,
         _plot_types=None):
    if _plots is None:
        _plots = ['same', 'different']
    if _plot_types is None:
        _plot_types = ['scatter', 'box', 'bar']

    _same_correlation_vs_time_lag, _same_time_lags_arrays, _different_time_lags_arrays, _same_time_lags_highest, \
        _different_time_lags_highest = compute_fiber_densities(_band, _high_temporal_resolution)

    if _plots is not None:

        # individual plots
        if 'scatter' in _plot_types:
            for _same_tuple in _same_correlation_vs_time_lag:
                _experiment, _series_id, _group = _same_tuple
                _temporal_resolution = compute.temporal_resolution_in_minutes(
                    _experiment)
                _fig = go.Figure(
                    data=go.Scatter(
                        x=np.array(TIME_LAGS[_high_temporal_resolution]) *
                        _temporal_resolution,
                        y=_same_correlation_vs_time_lag[_same_tuple],
                        mode='markers',
                        marker={
                            'size': 25,
                            'color': '#ea8500'
                        }),
                    layout={
                        'xaxis': {
                            'title':
                            'Time lag (minutes)',
                            'zeroline':
                            False,
                            'tickmode':
                            'array',
                            'tickvals':
                            np.array(TIME_LAGS[_high_temporal_resolution]) *
                            _temporal_resolution
                        },
                        'yaxis': {
                            'title': '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_' + _experiment + '_' +
                             str(_series_id) + '_' + _group)

        # box plots
        if 'box' in _plot_types:
            for _name, _arrays in zip(
                ['same', 'different'],
                [_same_time_lags_arrays, _different_time_lags_arrays]):
                if _name in _plots:
                    _fig = go.Figure(
                        data=[
                            go.Box(y=_y,
                                   name=_time_lag *
                                   5 if _high_temporal_resolution else 15,
                                   boxpoints=False,
                                   line={'width': 1},
                                   marker={
                                       'size': 10,
                                       'color': '#ea8500'
                                   },
                                   showlegend=False)
                            for _y, _time_lag in zip(
                                _arrays, TIME_LAGS[_high_temporal_resolution])
                        ],
                        layout={
                            'xaxis': {
                                'title':
                                'Time lag (minutes)',
                                'zeroline':
                                False,
                                'tickmode':
                                'array',
                                'tickvals':
                                np.array(TIME_LAGS[_high_temporal_resolution])
                                * 5 if _high_temporal_resolution else 15
                            },
                            'yaxis': {
                                'title':
                                'Inner correlation' if _name == 'same' else
                                'Different network 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_box_high_temporal_res_' +
                                 str(_high_temporal_resolution) + '_' + _name)

        # bar plot
        if 'bar' in _plot_types:
            for _name, _sums in zip(
                ['same', 'different'],
                [_same_time_lags_highest, _different_time_lags_highest]):
                if _name in _plots:
                    _fig = go.Figure(
                        data=go.Bar(
                            x=np.array(TIME_LAGS[_high_temporal_resolution]) *
                            5 if _high_temporal_resolution else 15,
                            y=np.array(_sums) / sum(_sums),
                            marker={'color': '#ea8500'}),
                        layout={
                            'xaxis': {
                                'title':
                                'Time lag (minutes)',
                                'zeroline':
                                False,
                                'tickmode':
                                'array',
                                'tickvals':
                                np.array(TIME_LAGS[_high_temporal_resolution])
                                * 5 if _high_temporal_resolution else 15
                            },
                            'yaxis': {
                                'title': 'Highest correlations 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_bar_high_temporal_res_' +
                                 str(_high_temporal_resolution) + '_' + _name)
Beispiel #11
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)
    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
            })

    _z_array = np.zeros(shape=(len(BY), len(BY)))
    for (_padding_index, _padding_by), (_space_index, _space_by) in product(enumerate(BY), enumerate(BY)):
        print('Padding by: ', _padding_by, ', space by: ', _space_by)
        _correlation = compute_data(_tuples, _arguments, _padding_y_by=_padding_by, _padding_z_by=_padding_by,
                                    _space_y_by=_space_by, _space_z_by=_space_by)
        _z_array[_space_index, _padding_index] = _correlation

    # plot
    _colors_array = ['white', config.colors(1)]
    _fig = go.Figure(
        data=go.Heatmap(
            x=BY,
            y=BY,
            z=_z_array,
            colorscale=sns.color_palette(_colors_array).as_hex(),
            colorbar={
                'tickmode': 'array',
                'tickvals': [0, 0.5, 1],
                'ticktext': ['0', 'Correlation', '1'],
                'tickangle': -90
            },
            showscale=True,
            zmin=0,
            zmax=1
        ),
        layout={
            'xaxis': {
                'title': 'Border size (cell diameter)',
                'zeroline': False,
                'tickmode': 'array',
                'tickvals': [0, 0.5, 1, 1.5, 2]
            },
            'yaxis': {
                'title': 'Space from quantification window (cell diameter)',
                'zeroline': False,
                'tickmode': 'array',
                'tickvals': [0, 0.5, 1, 1.5, 2]
            }
        }
    )

    save.to_html(
        _fig=_fig,
        _path=os.path.join(paths.PLOTS, save.get_module_name()),
        _filename='plot_high_time_' + str(_high_temporal_resolution) + '_band_' + str(_band)
    )
def main(_low_connectivity=False):
    _names_array, _x_array, _y_array = compute_cell_pairs(_low_connectivity)

    # plot
    _colors_array = config.colors(3)
    _fig = go.Figure(data=[
        go.Scatter(x=_x,
                   y=[np.mean(_array) for _array in _y],
                   name=_name,
                   error_y={
                       'type': 'data',
                       'array': [np.std(_array) for _array in _y],
                       'thickness': 1,
                       'color': _color
                   },
                   mode='markers',
                   marker={
                       'size': 15,
                       'color': _color
                   },
                   opacity=0.7) for _x, _y, _name, _color in zip(
                       _x_array, _y_array, _names_array, _colors_array)
    ],
                     layout={
                         'xaxis': {
                             'title': 'Window distance (cell diameter)',
                             'zeroline': False
                         },
                         'yaxis': {
                             'title': 'Fiber density (z-score)',
                             'range': [-1.7, 13],
                             'zeroline': False,
                             'tickmode': 'array',
                             'tickvals': [0, 4, 8, 12]
                         },
                         'legend': {
                             'xanchor': 'right',
                             'yanchor': 'top',
                             'bordercolor': 'black',
                             'borderwidth': 2
                         },
                         'shapes': [{
                             'type': 'line',
                             'x0': -0.2,
                             'y0': -1.5,
                             'x1': 3.4,
                             'y1': -1.5,
                             'line': {
                                 'color': 'black',
                                 'width': 2
                             }
                         }, {
                             'type': 'line',
                             'x0': -0.2,
                             'y0': -1.5,
                             'x1': -0.2,
                             'y1': 13,
                             'line': {
                                 'color': 'black',
                                 'width': 2
                             }
                         }]
                     })

    save.to_html(_fig=_fig,
                 _path=os.path.join(paths.PLOTS, save.get_module_name()),
                 _filename='plot_low_con_' + str(_low_connectivity))
Beispiel #13
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'
    )
Beispiel #14
0
def main(_alpha=1,
         _beta=1,
         _low_connectivity=False,
         _plots=None,
         _plot_types=None):
    if _plots is None:
        _plots = ['same', 'different']
    if _plot_types is None:
        _plot_types = ['scatter', 'box', 'bar']

    _same_correlation_vs_time_lag, _same_time_lags_arrays, _different_time_lags_arrays, _same_time_lags_highest, \
        _different_time_lags_highest = compute_fiber_densities(_alpha, _beta, _low_connectivity)

    if _plots is not None:

        # individual plots
        if 'scatter' in _plot_types:
            for _same_simulation in _same_correlation_vs_time_lag:
                _fig = go.Figure(data=go.Scatter(
                    x=TIME_LAGS,
                    y=_same_correlation_vs_time_lag[_same_simulation],
                    mode='markers',
                    marker={
                        'size': 25,
                        'color': '#2e82bf'
                    }),
                                 layout={
                                     'xaxis': {
                                         'title': 'Time lag',
                                         'zeroline': False,
                                         'tickmode': 'array',
                                         'tickvals': TIME_LAGS
                                     },
                                     'yaxis': {
                                         'title': '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_' + _same_simulation)

        # box plots
        if 'box' in _plot_types:
            for _name, _arrays in zip(
                ['same', 'different'],
                [_same_time_lags_arrays, _different_time_lags_arrays]):
                if _name in _plots:
                    _fig = go.Figure(
                        data=[
                            go.Box(y=_y,
                                   name=_time_lag,
                                   boxpoints=False,
                                   line={'width': 1},
                                   marker={
                                       'size': 10,
                                       'color': '#2e82bf'
                                   },
                                   showlegend=False)
                            for _y, _time_lag in zip(_arrays, TIME_LAGS)
                        ],
                        layout={
                            'xaxis': {
                                'title': 'Time lag',
                                'zeroline': False,
                                'tickmode': 'array',
                                'tickvals': TIME_LAGS
                            },
                            'yaxis': {
                                'title':
                                'Inner correlation' if _name == 'same' else
                                'Different network 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_box_alpha_' + str(_alpha) +
                                 '_beta_' + str(_beta) + '_low_con_' +
                                 str(_low_connectivity) + '_' + _name)

        # bar plot
        if 'bar' in _plot_types:
            for _name, _sums in zip(
                ['same', 'different'],
                [_same_time_lags_highest, _different_time_lags_highest]):
                if _name in _plots:
                    _fig = go.Figure(
                        data=go.Bar(x=TIME_LAGS,
                                    y=np.array(_sums) / sum(_sums),
                                    marker={'color': '#2e82bf'}),
                        layout={
                            'xaxis': {
                                'title': 'Time lag',
                                'zeroline': False,
                                'tickmode': 'array',
                                'tickvals': TIME_LAGS
                            },
                            'yaxis': {
                                'title': 'Highest correlation 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_bar_alpha_' + str(_alpha) +
                                 '_beta_' + str(_beta) + '_low_con_' +
                                 str(_low_connectivity) + '_' + _name)
Beispiel #15
0
def main(_low_connectivity=False):
    print('Simulations')
    _simulations_pairs_fiber_densities = \
        inner_density_vs_window_distance_cell_pairs.compute_simulations_data(_low_connectivity)
    _simulations_single_cells_fiber_densities = \
        inner_density_vs_window_distance_single_cells.compute_simulations_data(_low_connectivity)

    _min_simulations_fiber_densities_length = \
        min(len(_simulations_pairs_fiber_densities), len(_simulations_single_cells_fiber_densities))

    _simulations_fiber_densities_differences = \
        np.mean(_simulations_pairs_fiber_densities[:_min_simulations_fiber_densities_length], axis=1) - \
        np.mean(_simulations_single_cells_fiber_densities[:_min_simulations_fiber_densities_length], axis=1)

    print('Experiments')
    _experiments_pairs_fiber_densities, _ = inner_density_vs_window_distance_cell_pairs.compute_experiments_data(
    )
    _experiments_single_cells_fiber_densities = inner_density_vs_window_distance_single_cells.compute_experiments_data(
    )

    _experiments_pairs_fiber_densities_averages = \
        np.array([np.mean(_array) for _array in _experiments_pairs_fiber_densities])
    _experiments_single_cells_fiber_densities_averages = \
        np.array([np.mean(_array) for _array in _experiments_single_cells_fiber_densities])

    _min_experiments_fiber_densities_length = \
        min(len(_experiments_pairs_fiber_densities_averages), len(_experiments_single_cells_fiber_densities_averages))
    _experiments_fiber_densities_differences = \
        _experiments_pairs_fiber_densities_averages[:_min_experiments_fiber_densities_length] - \
        _experiments_single_cells_fiber_densities_averages[:_min_experiments_fiber_densities_length]

    # plot
    _fig = go.Figure(data=[
        go.Scatter(x=OFFSETS_X,
                   y=_simulations_fiber_densities_differences,
                   name='Simulations',
                   mode='markers',
                   marker={
                       'size': 15,
                       'color': '#005b96'
                   },
                   opacity=0.7),
        go.Scatter(x=OFFSETS_X,
                   y=_experiments_fiber_densities_differences,
                   name='Experiments',
                   mode='markers',
                   marker={
                       'size': 15,
                       'color': '#ea8500'
                   },
                   opacity=0.7)
    ],
                     layout={
                         'xaxis': {
                             'title': 'Window distance (cell diameter)',
                             'zeroline': False
                         },
                         'yaxis': {
                             'title': 'Fiber density difference (z-score)',
                             'range': [-0.2, 6],
                             'zeroline': False,
                             'tickvals': [0, 2, 4]
                         },
                         'legend': {
                             'xanchor': 'right',
                             'yanchor': 'top',
                             'bordercolor': 'black',
                             'borderwidth': 2,
                             'bgcolor': 'white'
                         },
                         'shapes': [{
                             'type': 'line',
                             'x0': -0.2,
                             'y0': 0,
                             'x1': 2.6,
                             'y1': 0,
                             'line': {
                                 'color': 'black',
                                 'width': 2
                             }
                         }, {
                             'type': 'line',
                             'x0': -0.2,
                             'y0': 0,
                             'x1': -0.2,
                             'y1': 6,
                             'line': {
                                 'color': 'black',
                                 'width': 2
                             }
                         }]
                     })

    save.to_html(_fig=_fig,
                 _path=os.path.join(paths_lib.PLOTS, save.get_module_name()),
                 _filename='plot_low_con_' + str(_low_connectivity))
Beispiel #16
0
def main(_real_cells=True,
         _static=False,
         _band=True,
         _high_temporal_resolution=False,
         _pair_distance_range=None,
         _offset_y=0.5):
    if _pair_distance_range is None:
        _pair_distance_range = PAIR_DISTANCE_RANGE

    _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)
    print('Total tuples:', len(_tuples))

    _arguments = []
    _longest_time_frame = 0
    for _tuple in _tuples:
        _experiment, _series_id, _group = _tuple
        _latest_time_frame = compute.latest_time_frame_before_overlapping(
            _experiment, _series_id, _group, OFFSET_X)

        # save for later
        if _latest_time_frame > _longest_time_frame:
            _longest_time_frame = _latest_time_frame

        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)

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

    _valid_tuples = []
    _valid_cells = []
    _densities = [[] for _ in range(_longest_time_frame)]
    for _tuple in tqdm(_tuples, desc='Experiments loop'):
        _experiment, _series_id, _group = _tuple
        _normalization = load.normalization_series_file_data(
            _experiment, _series_id)
        _properties = load.group_properties(_experiment, _series_id, _group)

        for _cell_id in ['left_cell', 'right_cell']:
            _cell_fiber_densities = \
                _experiments_fiber_densities[(_experiment, _series_id, _group, _cell_id)]

            _cell_fiber_densities = compute.remove_blacklist(
                _experiment, _series_id, _properties['cells_ids'][_cell_id],
                _cell_fiber_densities)

            _previous_cell_fiber_density_normalized = None
            for _time_frame, _cell_fiber_density in enumerate(
                    _cell_fiber_densities):

                # not out of border
                if _cell_fiber_density[1]:
                    _previous_cell_fiber_density_normalized = None
                    continue

                # normalize
                _cell_fiber_density_normalized = compute_lib.z_score(
                    _x=_cell_fiber_density[0],
                    _average=_normalization['average'],
                    _std=_normalization['std'])

                # no previous
                if _previous_cell_fiber_density_normalized is None:
                    _previous_cell_fiber_density_normalized = _cell_fiber_density_normalized
                    continue

                # change
                _cell_fiber_density_normalized_change = _cell_fiber_density_normalized - _previous_cell_fiber_density_normalized
                _previous_cell_fiber_density_normalized = _cell_fiber_density_normalized

                # save
                _densities[_time_frame].append(
                    _cell_fiber_density_normalized_change)

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

                _cell_tuple = (_experiment, _series_id, _group, _cell_id)
                if _cell_tuple not in _valid_cells:
                    _valid_cells.append(_cell_tuple)

    print('Total pairs:', len(_valid_tuples))
    print('Total cells:', len(_valid_cells))

    # plot
    _temporal_resolution = compute.temporal_resolution_in_minutes(
        _experiments[0])
    _fig = go.Figure(
        data=go.Scatter(x=np.array(range(_longest_time_frame)) *
                        _temporal_resolution,
                        y=[np.mean(_array) for _array in _densities],
                        name='Fiber density change (z-score)',
                        error_y={
                            'type': 'data',
                            'array': [np.std(_array) for _array in _densities],
                            'thickness': 1
                        },
                        mode='lines+markers',
                        marker={
                            'size': 5,
                            'color': '#ea8500'
                        },
                        line={'dash': 'solid'},
                        showlegend=False),
        layout={
            'xaxis': {
                'title': 'Time (minutes)',
                # 'zeroline': False
            },
            'yaxis': {
                'title': 'Fiber density change (z-score)',
                # 'zeroline': False
            },
            # 'shapes': [
            #     {
            #         'type': 'line',
            #         'x0': -_temporal_resolution,
            #         'y0': -0.5,
            #         'x1': -_temporal_resolution,
            #         'y1': 2,
            #         'line': {
            #             'color': 'black',
            #             'width': 2
            #         }
            #     },
            #     {
            #         'type': 'line',
            #         'x0': -_temporal_resolution,
            #         'y0': -0.5,
            #         'x1': 350,
            #         'y1': -0.5,
            #         '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) + '_range_' +
        '_'.join([str(_distance) for _distance in _pair_distance_range]) +
        '_y_' + str(_offset_y))
Beispiel #17
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():
    _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 #19
0
def main(_low_connectivity=False):
    print('Simulations')
    _simulations_fiber_densities = compute_simulations_data(_low_connectivity)

    print('Experiments')
    _experiments_fiber_densities, _experiments_offsets_x = compute_experiments_data(
    )

    # plot
    _fig = go.Figure(data=[
        go.Scatter(
            x=SIMULATIONS_OFFSETS_X,
            y=[np.mean(_array) for _array in _simulations_fiber_densities],
            name='Simulations',
            error_y={
                'type':
                'data',
                'array':
                [np.std(_array) for _array in _simulations_fiber_densities],
                'thickness':
                1,
                'color':
                '#005b96'
            },
            mode='markers',
            marker={
                'size': 15,
                'color': '#005b96'
            },
            opacity=0.7),
        go.Scatter(
            x=_experiments_offsets_x,
            y=[np.mean(_array) for _array in _experiments_fiber_densities],
            name='Experiments',
            error_y={
                'type':
                'data',
                'array':
                [np.std(_array) for _array in _experiments_fiber_densities],
                'thickness':
                1,
                'color':
                '#ea8500'
            },
            mode='markers',
            marker={
                'size': 15,
                'color': '#ea8500'
            },
            opacity=0.7)
    ],
                     layout={
                         'xaxis': {
                             'title': 'Window distance (cell diameter)',
                             'zeroline': False
                         },
                         'yaxis': {
                             'title': 'Fiber density (z-score)',
                             'range': [-1.7, 13],
                             'zeroline': False,
                             'tickmode': 'array',
                             'tickvals': [0, 4, 8, 12]
                         },
                         'legend': {
                             'xanchor': 'right',
                             'yanchor': 'top',
                             'bordercolor': 'black',
                             'borderwidth': 2
                         },
                         'shapes': [{
                             'type': 'line',
                             'x0': -0.2,
                             'y0': -1.5,
                             'x1': 3.4,
                             'y1': -1.5,
                             'line': {
                                 'color': 'black',
                                 'width': 2
                             }
                         }, {
                             'type': 'line',
                             'x0': -0.2,
                             'y0': -1.5,
                             'x1': -0.2,
                             'y1': 13,
                             'line': {
                                 'color': 'black',
                                 'width': 2
                             }
                         }]
                     })

    save.to_html(_fig=_fig,
                 _path=os.path.join(paths_lib.PLOTS, save.get_module_name()),
                 _filename='plot_pair_distance_' + str(PAIR_DISTANCE) +
                 '_low_con_' + str(_low_connectivity))
Beispiel #20
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)
    )
def main(_band=True,
         _high_temporal_resolution=False,
         _offset_x=OFFSET_X,
         _offset_y_start=OFFSET_Y_START,
         _offset_y_end=OFFSET_Y_END,
         _offset_y_step=OFFSET_Y_STEP,
         _offset_z_start=OFFSET_Z_START,
         _offset_z_end=OFFSET_Z_END,
         _offset_z_step=OFFSET_Z_STEP):
    global _tuples, _experiments_fiber_densities, _z_array

    compute_z_array(_band=_band,
                    _high_temporal_resolution=_high_temporal_resolution,
                    _offset_x=OFFSET_X,
                    _offset_y_start=OFFSET_Y_START,
                    _offset_y_end=OFFSET_Y_END,
                    _offset_y_step=OFFSET_Y_STEP,
                    _offset_z_start=OFFSET_Z_START,
                    _offset_z_end=OFFSET_Z_END,
                    _offset_z_step=OFFSET_Z_STEP)

    # plot
    _offsets_y = np.arange(start=_offset_y_start,
                           stop=_offset_y_end + _offset_y_step,
                           step=_offset_y_step)
    _offsets_z = np.arange(start=_offset_z_start,
                           stop=_offset_z_end + _offset_z_step,
                           step=_offset_z_step)
    _colors_array = ['white', config.colors(1)]
    _fig = go.Figure(data=go.Heatmap(
        x=_offsets_z,
        y=_offsets_y,
        z=_z_array,
        colorscale=sns.color_palette(_colors_array).as_hex(),
        colorbar={
            'tickmode': 'array',
            'tickvals': [0, 0.35, 0.7],
            'ticktext': ['0.0', 'Z-score change', '0.7'],
            'tickangle': -90
        },
        showscale=True,
        zmin=0,
        zmax=0.7),
                     layout={
                         'xaxis': {
                             'title': 'Offset in XY axis (cell diameter)',
                             'zeroline': False,
                             'tickmode': 'array',
                             'tickvals': [-4, -2, 0, 2, 4]
                         },
                         'yaxis': {
                             'title': 'Offset in Z axis (cell diameter)',
                             'zeroline': False,
                             'tickmode': 'array',
                             'tickvals': [-1, 0, 1, 2]
                         }
                     })

    save.to_html(_fig=_fig,
                 _path=os.path.join(paths.PLOTS, save.get_module_name()),
                 _filename='plot_high_time_' + str(_high_temporal_resolution) +
                 '_band_' + str(_band))
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')
Beispiel #23
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(_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():
    _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(_type='alpha',
         _low_connectivity=False,
         _plots=None,
         _plot_types=None):
    if _plots is None:
        _plots = ['same', 'different']
    if _plot_types is None:
        _plot_types = ['stacked_bar', 'box', 'bar']

    _same_arrays = []
    _different_arrays = []
    _same_highest = []
    _different_highest = []

    if _type == 'alpha':
        _alphas = ALPHAS
        _betas = [BETA] * len(ALPHAS)
        _names = _alphas
    elif _type == 'beta':
        _alphas = [ALPHA] * len(BETAS)
        _betas = BETAS
        _names = _betas
    else:
        raise Exception(
            'No such type. Only \'alpha\' or \'beta\' are acceptable.')

    for _alpha, _beta in zip(_alphas, _betas):
        print('Alpha:', _alpha, 'beta:', _beta)
        _, _same_time_lags_arrays, _different_time_lags_arrays, _same_time_lags_highest, \
            _different_time_lags_highest = same_inner_correlation_vs_different_inner_correlation_cross_correlation.compute_fiber_densities(
                _alpha=_alpha, _beta=_beta, _low_connectivity=_low_connectivity)

        _same_arrays.append(_same_time_lags_arrays[TIME_LAG_INDEX])
        _different_arrays.append(_different_time_lags_arrays[TIME_LAG_INDEX])
        _same_highest.append(_same_time_lags_highest)
        _different_highest.append(_different_time_lags_highest)

    if _plots is not None:

        # stacked bar plot
        if 'stacked_bar' in _plot_types:
            for _name, _sums in zip(['same', 'different'],
                                    [_same_highest, _different_highest]):
                if _name in _plots:

                    _y_arrays = [[], [], []]
                    for _type_sums in _sums:
                        _left_wins, _none_wins, _right_wins = 0, 0, 0
                        for _time_lag, _type_sum in zip(
                                same_inner_correlation_vs_different_inner_correlation_cross_correlation
                                .TIME_LAGS, _type_sums):
                            if _time_lag > 0:
                                _left_wins += _type_sum
                            elif _time_lag < 0:
                                _right_wins += _type_sum
                            else:
                                _none_wins += _type_sum
                        _total = sum(_type_sums)
                        _y_arrays[0].append(_left_wins / _total)
                        _y_arrays[1].append(_none_wins / _total)
                        _y_arrays[2].append(_right_wins / _total)

                    _colors_array = config.colors(3)
                    _fig = go.Figure(data=[
                        go.Bar(x=_names,
                               y=_y_array,
                               name=_name,
                               marker={'color': _color}) for _name, _y_array,
                        _color in zip(['Leader', 'None', 'Follower'],
                                      _y_arrays, _colors_array)
                    ],
                                     layout={
                                         'xaxis': {
                                             'title': _type.capitalize(),
                                             'zeroline': False,
                                             'tickmode': 'array',
                                             'tickvals': _names,
                                             'type': 'category'
                                         },
                                         'yaxis': {
                                             'title':
                                             'Highest correlation fraction',
                                             'range': [0, 1.1],
                                             'zeroline': False,
                                             'tickmode': 'array',
                                             'tickvals': [0, 0.5, 1]
                                         },
                                         'barmode': 'stack',
                                         '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_stacked_bar_' + _type +
                                 '_low_con_' + str(_low_connectivity) + '_' +
                                 _name)

        # box plot
        if 'box' in _plot_types:
            for _name, _arrays in zip(['same', 'different'],
                                      [_same_arrays, _different_arrays]):
                if _name in _plots:
                    _fig = go.Figure(
                        data=[
                            go.Box(y=_y,
                                   name=_name,
                                   boxpoints=False,
                                   line={'width': 1},
                                   marker={
                                       'size': 10,
                                       'color': '#2e82bf'
                                   },
                                   showlegend=False)
                            for _y, _name in zip(_arrays, _names)
                        ],
                        layout={
                            'xaxis': {
                                'title': _type.capitalize(),
                                'zeroline': False,
                                'tickmode': 'array',
                                'tickvals': _names,
                                'type': 'category'
                            },
                            'yaxis': {
                                'title':
                                'Inner correlation' if _name == 'same' else
                                'Different network 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_box_' + _type + '_low_con_' +
                                 str(_low_connectivity) + '_' + _name)

        # bar plot
        if 'bar' in _plot_types:
            for _name, _sums in zip(['same', 'different'],
                                    [_same_highest, _different_highest]):
                if _name in _plots:
                    _fig = go.Figure(data=go.Bar(
                        x=_names,
                        y=[
                            _type_sums[TIME_LAG_INDEX] / sum(_type_sums)
                            for _type_sums in _sums
                        ],
                        marker={'color': '#2e82bf'}),
                                     layout={
                                         'xaxis': {
                                             'title': _type.capitalize(),
                                             'zeroline': False,
                                             'tickmode': 'array',
                                             'tickvals': _names,
                                             'type': 'category'
                                         },
                                         'yaxis': {
                                             'title': 'Lag ' + str(TIME_LAG) +
                                             ' highest correlation 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_bar_' + _type + '_low_con_' +
                                 str(_low_connectivity) + '_' + _name)
def main():
    print('Single cells')
    _single_cells_fiber_densities = inner_density_vs_window_distance_single_cells.compute_experiments_data()

    print('Cell pairs')
    _names_array, _x_array, _y_array = inner_density_vs_window_distance.compute_cell_pairs()

    # plot
    _colors_array = config.colors(len(_names_array) + 1)
    _fig = go.Figure(
        data=[
            go.Scatter(
                x=_x,
                y=[np.mean(_array) for _array in _y],
                name=_name,
                error_y={
                    'type': 'data',
                    'array': [np.std(_array) for _array in _y],
                    'thickness': 1,
                    'color': _color
                },
                mode='markers',
                marker={
                    'size': 15,
                    'color': _color
                },
                opacity=0.7
            ) for _x, _y, _name, _color in zip(_x_array, _y_array, _names_array, _colors_array[:-1])
        ] + [
            go.Scatter(
                x=inner_density_vs_window_distance_single_cells.OFFSETS_X,
                y=[np.mean(_array) for _array in _single_cells_fiber_densities],
                name='Single cells',
                 error_y={
                     'type': 'data',
                     'array': [np.std(_array) for _array in _single_cells_fiber_densities],
                     'thickness': 1,
                     'color': _colors_array[-1]
                 },
                 mode='markers',
                 marker={
                     'size': 15,
                     'color': _colors_array[-1]
                 },
                 opacity=0.7
                )
        ],
        layout={
            'xaxis': {
                'title': 'Window distance (cell diameter)',
                'zeroline': False
            },
            'yaxis': {
                'title': 'Fiber density (z-score)',
                'range': [-1.7, 13],
                'zeroline': False,
                'tickmode': 'array',
                'tickvals': [0, 4, 8, 12]
            },
            'legend': {
                'xanchor': 'right',
                'yanchor': 'top',
                'bordercolor': 'black',
                'borderwidth': 2
            },
            'shapes': [
                {
                    'type': 'line',
                    'x0': -0.2,
                    'y0': -1.5,
                    'x1': 3.4,
                    'y1': -1.5,
                    'line': {
                        'color': 'black',
                        'width': 2
                    }
                },
                {
                    'type': 'line',
                    'x0': -0.2,
                    'y0': -1.5,
                    'x1': -0.2,
                    'y1': 13,
                    'line': {
                        'color': 'black',
                        'width': 2
                    }
                }
            ]
        }
    )

    save.to_html(
        _fig=_fig,
        _path=os.path.join(paths.PLOTS, save.get_module_name()),
        _filename='plot'
    )
Beispiel #29
0
def main():
    _fiber_densities = compute_data()

    # plot
    _fig = go.Figure(data=[
        go.Scatter(x=list(range(TIME_POINTS))[::TIME_STEP],
                   y=[np.mean(_array)
                      for _array in _fiber_densities][::TIME_STEP],
                   error_y={
                       'type':
                       'data',
                       'array':
                       [np.std(_array)
                        for _array in _fiber_densities][::TIME_STEP],
                       'thickness':
                       1,
                       'color':
                       '#005b96'
                   },
                   mode='markers',
                   marker={
                       'size': 15,
                       'color': '#005b96'
                   },
                   opacity=0.7,
                   showlegend=False)
    ],
                     layout={
                         'xaxis': {
                             'title': 'Cell contraction (%)',
                             'zeroline': False,
                             'tickmode': 'array',
                             'tickvals': [0, 25, 50]
                         },
                         'yaxis': {
                             'title': 'Fiber density (z-score)',
                             'range': [-1.7, 6],
                             'zeroline': False,
                             'tickmode': 'array',
                             'tickvals': [0, 2, 4, 6]
                         },
                         'shapes': [{
                             'type': 'line',
                             'x0': -2,
                             'y0': -1.5,
                             'x1': 53,
                             'y1': -1.5,
                             'line': {
                                 'color': 'black',
                                 'width': 2
                             }
                         }, {
                             'type': 'line',
                             'x0': -2,
                             'y0': -1.5,
                             'x1': -2,
                             'y1': 6,
                             'line': {
                                 'color': 'black',
                                 'width': 2
                             }
                         }]
                     })

    save.to_html(_fig=_fig,
                 _path=os.path.join(paths.PLOTS, save.get_module_name()),
                 _filename='plot')
def main():
    print('Simulations')
    _simulations_fiber_densities = compute_simulations_data()

    print('Experiments')
    _experiments_fiber_densities = compute_experiments_data()

    # plot
    _fig = go.Figure(
        data=[
            go.Scatter(
                x=list(range(SIMULATIONS_TIME_POINTS))[::SIMULATIONS_STEP],
                y=[np.mean(_array) for _array in _simulations_fiber_densities][::SIMULATIONS_STEP],
                name='Simulations',
                error_y={
                    'type': 'data',
                    'array': [np.std(_array) for _array in _simulations_fiber_densities][::SIMULATIONS_STEP],
                    'thickness': 1,
                    'color': '#005b96'
                },
                mode='markers',
                marker={
                    'size': 15,
                    'color': '#005b96'
                },
                opacity=0.7
            ),
            go.Scatter(
                x=np.array(range(EXPERIMENTS_TIME_FRAMES)) * 15,
                xaxis='x2',
                y=[np.mean(_array) for _array in _experiments_fiber_densities],
                name='Experiments',
                error_y={
                    'type': 'data',
                    'array': [np.std(_array) for _array in _experiments_fiber_densities],
                    'thickness': 1,
                    'color': '#ea8500'
                },
                mode='markers',
                marker={
                    'size': 15,
                    'color': '#ea8500'
                },
                opacity=0.7
            )
        ],
        layout={
            'xaxis': {
                'title': 'Cell contraction (%)',
                'titlefont': {
                    'color': '#005b96'
                },
                'tickfont': {
                    'color': '#005b96'
                },
                'zeroline': False
            },
            'xaxis2': {
                'title': 'Time (minutes)',
                'titlefont': {
                    'color': '#ea8500'
                },
                'tickfont': {
                    'color': '#ea8500'
                },
                # 'anchor': 'free',
                'overlaying': 'x',
                'side': 'bottom',
                'showgrid': False,
                'zeroline': False
            },
            'yaxis': {
                'title': 'Fiber density (z-score)',
                # 'domain': [0.3, 1],
                'range': [-1.7, 14],
                'zeroline': False,
                'tickmode': 'array',
                'tickvals': [0, 4, 8, 12]
            },
            'legend': {
                'xanchor': 'left',
                'x': 0.1,
                'yanchor': 'top',
                'bordercolor': 'black',
                'borderwidth': 2,
                'bgcolor': 'white'
            },
            'shapes': [
                {
                    'type': 'line',
                    'x0': -2,
                    'y0': -1.5,
                    'x1': 53,
                    'y1': -1.5,
                    'line': {
                        'color': 'black',
                        'width': 2
                    }
                },
                {
                    'type': 'line',
                    'x0': -2,
                    'y0': -1.5,
                    'x1': -2,
                    'y1': 14,
                    'line': {
                        'color': 'black',
                        'width': 2
                    }
                }
            ]
        }
    )

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