Completed
Push — master ( 03c687...f8239c )
by Chad
15:27
created

diff_classifier.heatmaps.plot_individual_msds()   A

Complexity

Conditions 3

Size

Total Lines 63
Code Lines 31

Duplication

Lines 63
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 31
dl 63
loc 63
rs 9.1359
c 0
b 0
f 0
cc 3
nop 10

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
import matplotlib as mpl
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
import numpy as np
3
import pandas as pd
4
import matplotlib.pyplot as plt
0 ignored issues
show
introduced by
Imports from package matplotlib are not grouped
Loading history...
5
from scipy.spatial import Voronoi
0 ignored issues
show
Bug introduced by
The name Voronoi does not seem to exist in module scipy.spatial.
Loading history...
6
import scipy.stats as stats
7
import os
0 ignored issues
show
Unused Code introduced by
The import os seems to be unused.
Loading history...
introduced by
standard import "import os" should be placed before "import matplotlib as mpl"
Loading history...
8
import os.path as op
0 ignored issues
show
Unused Code introduced by
Unused os.path imported as op
Loading history...
introduced by
standard import "import os.path as op" should be placed before "import matplotlib as mpl"
Loading history...
9
from shapely.geometry import Point
0 ignored issues
show
introduced by
Unable to import 'shapely.geometry'
Loading history...
10
from shapely.geometry.polygon import Polygon
0 ignored issues
show
introduced by
Unable to import 'shapely.geometry.polygon'
Loading history...
11
import numpy.ma as ma
0 ignored issues
show
introduced by
Imports from package numpy are not grouped
Loading history...
12
import matplotlib.cm as cm
0 ignored issues
show
introduced by
Imports from package matplotlib are not grouped
Loading history...
13
import diff_classifier.aws as aws
14
15
16 View Code Duplication
def voronoi_finite_polygons_2d(vor, radius=None):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
Comprehensibility introduced by
This function exceeds the maximum number of variables (23/15).
Loading history...
17
    """
18
    Reconstruct infinite voronoi regions in a 2D diagram to finite
19
    regions.
20
21
    Parameters
22
    ----------
23
    vor : Voronoi
24
        Input diagram
25
    radius : float, optional
26
        Distance to 'points at infinity'.
27
28
    Returns
29
    -------
30
    regions : list of tuples
31
        Indices of vertices in each revised Voronoi regions.
32
    vertices : list of tuples
33
        Coordinates for revised Voronoi vertices. Same as coordinates
34
        of input vertices, with 'points at infinity' appended to the
35
        end.
36
37
    """
38
39
    if vor.points.shape[1] != 2:
40
        raise ValueError("Requires 2D input")
41
42
    new_regions = []
43
    new_vertices = vor.vertices.tolist()
44
45
    center = vor.points.mean(axis=0)
46
    if radius is None:
47
        radius = vor.points.ptp().max()
48
49
    # Construct a map containing all ridges for a given point
50
    all_ridges = {}
51
    for (p1, p2), (v1, v2) in zip(vor.ridge_points, vor.ridge_vertices):
0 ignored issues
show
Coding Style Naming introduced by
The name p1 does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name p2 does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name v1 does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name v2 does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
52
        all_ridges.setdefault(p1, []).append((p2, v1, v2))
53
        all_ridges.setdefault(p2, []).append((p1, v1, v2))
54
55
    counter = 0
56
    for p1, region in enumerate(vor.point_region):
0 ignored issues
show
Coding Style Naming introduced by
The name p1 does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
57
        try:
58
            vertices = vor.regions[region]
59
60
            if all(v >= 0 for v in vertices):
61
                # finite region
62
                new_regions.append(vertices)
63
                continue
64
65
            # reconstruct a non-finite region
66
            ridges = all_ridges[p1]
67
            new_region = [v for v in vertices if v >= 0]
68
69
            for p2, v1, v2 in ridges:
0 ignored issues
show
Coding Style Naming introduced by
The name p2 does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name v1 does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name v2 does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
70
                if v2 < 0:
71
                    v1, v2 = v2, v1
0 ignored issues
show
Coding Style Naming introduced by
The name v1 does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name v2 does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
72
                if v1 >= 0:
73
                    # finite ridge: already in the region
74
                    continue
75
76
                # Compute the missing endpoint of an infinite ridge
77
78
                t = vor.points[p2] - vor.points[p1]  # tangent
0 ignored issues
show
Coding Style Naming introduced by
The name t does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
79
                t /= np.linalg.norm(t)
0 ignored issues
show
Coding Style Naming introduced by
The name t does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
80
                n = np.array([-t[1], t[0]])  # normal
0 ignored issues
show
Coding Style Naming introduced by
The name n does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
81
82
                midpoint = vor.points[[p1, p2]].mean(axis=0)
83
                direction = np.sign(np.dot(midpoint - center, n)) * n
84
                far_point = vor.vertices[v2] + direction * radius
85
86
                new_region.append(len(new_vertices))
87
                new_vertices.append(far_point.tolist())
88
89
            # sort region counterclockwise
90
            vs = np.asarray([new_vertices[v] for v in new_region])
0 ignored issues
show
Coding Style Naming introduced by
The name vs does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
91
            c = vs.mean(axis=0)
0 ignored issues
show
Coding Style Naming introduced by
The name c does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
92
            angles = np.arctan2(vs[:, 1] - c[1], vs[:, 0] - c[0])
93
            new_region = np.array(new_region)[np.argsort(angles)]
94
95
            # finish
96
            new_regions.append(new_region.tolist())
97
        except KeyError:
98
            counter = counter + 1
99
            # print('Oops {}'.format(counter))
100
101
    return new_regions, np.asarray(new_vertices)
102
103
104 View Code Duplication
def plot_heatmap(prefix, feature='asymmetry1', vmin=0, vmax=1, resolution=512, rows=4, cols=4,
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
best-practice introduced by
Too many arguments (12/5)
Loading history...
Comprehensibility introduced by
This function exceeds the maximum number of variables (39/15).
Loading history...
105
                 upload=True, dpi=None, figsize=(12, 10), remote_folder = "01_18_Experiment",
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
106
                 bucket='ccurtis.data'):
107
    """
108
    Plot heatmap of trajectories in video with colors corresponding to features.
109
110
    Parameters
111
    ----------
112
    prefix: string
113
        Prefix of file name to be plotted e.g. features_P1.csv prefix is P1.
114
    feature: string
115
        Feature to be plotted.  See features_analysis.py
116
    vmin: float64
117
        Lower intensity bound for heatmap.
118
    vmax: float64
119
        Upper intensity bound for heatmap.
120
    resolution: int
121
        Resolution of base image.  Only needed to calculate bounds of image.
122
    rows: int
123
        Rows of base images used to build tiled image.
124
    cols: int
125
        Columns of base images used to build tiled images.
126
    upload: boolean
127
        True if you want to upload to s3.
128
    dpi: int
129
        Desired dpi of output image.
130
    figsize: list
131
        Desired dimensions of output image.
132
133
    Returns
134
    -------
135
136
    """
137
    # Inputs
138
    # ----------
139
    merged_ft = pd.read_csv('features_{}.csv'.format(prefix))
140
    string = feature
141
    leveler = merged_ft[string]
0 ignored issues
show
Unused Code introduced by
The variable leveler seems to be unused.
Loading history...
142
    t_min = vmin
143
    t_max = vmax
144
    ires = resolution
145
146
    # Building points and color schemes
147
    # ----------
148
    zs = ma.masked_invalid(merged_ft[string])
0 ignored issues
show
Coding Style Naming introduced by
The name zs does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
149
    zs = ma.masked_where(zs <= t_min, zs)
0 ignored issues
show
Coding Style Naming introduced by
The name zs does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
150
    zs = ma.masked_where(zs >= t_max, zs)
0 ignored issues
show
Coding Style Naming introduced by
The name zs does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
151
    to_mask = ma.getmask(zs)
152
    zs = ma.compressed(zs)
0 ignored issues
show
Coding Style Naming introduced by
The name zs does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
153
154
    xs = ma.compressed(ma.masked_where(to_mask, merged_ft['X'].astype(int)))
0 ignored issues
show
Coding Style Naming introduced by
The name xs does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
155
    ys = ma.compressed(ma.masked_where(to_mask, merged_ft['Y'].astype(int)))
0 ignored issues
show
Coding Style Naming introduced by
The name ys does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
156
    points = np.zeros((xs.shape[0], 2))
157
    points[:, 0] = xs
158
    points[:, 1] = ys
159
    vor = Voronoi(points)
160
161
    # Plot
162
    # ----------
163
    fig = plt.figure(figsize=figsize, dpi=dpi)
164
    regions, vertices = voronoi_finite_polygons_2d(vor)
165
    my_map = cm.get_cmap('viridis')
166
    norm = mpl.colors.Normalize(t_min, t_max, clip=True)
167
    mapper = cm.ScalarMappable(norm=norm, cmap=cm.viridis)
0 ignored issues
show
Bug introduced by
The Module matplotlib.cm does not seem to have a member named viridis.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
168
169
    test = 0
170
    p2 = 0
0 ignored issues
show
Coding Style Naming introduced by
The name p2 does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
171
    counter = 0
0 ignored issues
show
Unused Code introduced by
The variable counter seems to be unused.
Loading history...
172
    for i in range(0, points.shape[0]-1):
0 ignored issues
show
Unused Code introduced by
The variable i seems to be unused.
Loading history...
173
        try:
174
            polygon = vertices[regions[p2]]
175
            point1 = Point(points[test, :])
176
            poly1 = Polygon(polygon)
177
            check = poly1.contains(point1)
178
            if check:
179
                plt.fill(*zip(*polygon), color=my_map(norm(zs[test])), alpha=0.7)
180
                p2 = p2 + 1
0 ignored issues
show
Coding Style Naming introduced by
The name p2 does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
181
                test = test + 1
182
            else:
183
                p2 = p2
0 ignored issues
show
Coding Style Naming introduced by
The name p2 does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
184
                test = test + 1
185
        except IndexError:
186
            print('Index mismatch possible.')
187
188
    mapper.set_array(10)
189
    plt.colorbar(mapper)
190
    plt.xlim(0, ires*cols)
191
    plt.ylim(0, ires*rows)
192
    plt.axis('off')
193
194
    print('Plotted {} heatmap successfully.'.format(prefix))
195
    outfile = 'hm_{}_{}.png'.format(feature, prefix)
196
    fig.savefig(outfile, bbox_inches='tight')
197
    if upload == True:
0 ignored issues
show
introduced by
Comparison to True should be just 'expr' or 'expr is True'
Loading history...
198
        aws.upload_s3(outfile, remote_folder+'/'+outfile, bucket_name=bucket)
199
200
201 View Code Duplication
def plot_scatterplot(prefix, feature='asymmetry1', vmin=0, vmax=1, resolution=512, rows=4, cols=4,
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
best-practice introduced by
Too many arguments (10/5)
Loading history...
Comprehensibility introduced by
This function exceeds the maximum number of variables (24/15).
Loading history...
202
                     upload=True, remote_folder = "01_18_Experiment",
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
203
                     bucket='ccurtis.data'):
204
    """
205
    Plot scatterplot of trajectories in video with colors corresponding to features.
206
207
    Parameters
208
    ----------
209
    prefix: string
210
        Prefix of file name to be plotted e.g. features_P1.csv prefix is P1.
211
    feature: string
212
        Feature to be plotted.  See features_analysis.py
213
    vmin: float64
214
        Lower intensity bound for heatmap.
215
    vmax: float64
216
        Upper intensity bound for heatmap.
217
    resolution: int
218
        Resolution of base image.  Only needed to calculate bounds of image.
219
    rows: int
220
        Rows of base images used to build tiled image.
221
    cols: int
222
        Columns of base images used to build tiled images.
223
    upload: boolean
224
        True if you want to upload to s3.
225
226
    """
227
    # Inputs
228
    # ----------
229
    merged_ft = pd.read_csv('features_{}.csv'.format(prefix))
230
    string = feature
231
    leveler = merged_ft[string]
0 ignored issues
show
Unused Code introduced by
The variable leveler seems to be unused.
Loading history...
232
    t_min = vmin
233
    t_max = vmax
234
    ires = resolution
235
236
    norm = mpl.colors.Normalize(t_min, t_max, clip=True)
237
    mapper = cm.ScalarMappable(norm=norm, cmap=cm.viridis)
0 ignored issues
show
Bug introduced by
The Module matplotlib.cm does not seem to have a member named viridis.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
238
239
    zs = ma.masked_invalid(merged_ft[string])
0 ignored issues
show
Coding Style Naming introduced by
The name zs does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
240
    zs = ma.masked_where(zs <= t_min, zs)
0 ignored issues
show
Coding Style Naming introduced by
The name zs does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
241
    zs = ma.masked_where(zs >= t_max, zs)
0 ignored issues
show
Coding Style Naming introduced by
The name zs does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
242
    to_mask = ma.getmask(zs)
243
    zs = ma.compressed(zs)
0 ignored issues
show
Coding Style Naming introduced by
The name zs does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
244
    xs = ma.compressed(ma.masked_where(to_mask, merged_ft['X'].astype(int)))
0 ignored issues
show
Coding Style Naming introduced by
The name xs does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
245
    ys = ma.compressed(ma.masked_where(to_mask, merged_ft['Y'].astype(int)))
0 ignored issues
show
Coding Style Naming introduced by
The name ys does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
246
247
    fig = plt.figure(figsize=(12, 10))
248
    plt.scatter(xs, ys, c=zs, s=10)
249
    mapper.set_array(10)
250
    plt.colorbar(mapper)
251
    plt.xlim(0, ires*cols)
252
    plt.ylim(0, ires*rows)
253
    plt.axis('off')
254
255
    print('Plotted {} scatterplot successfully.'.format(prefix))
256
    outfile = 'scatter_{}.png'.format(prefix)
257
    fig.savefig(outfile, bbox_inches='tight')
258
    if upload == True:
0 ignored issues
show
introduced by
Comparison to True should be just 'expr' or 'expr is True'
Loading history...
259
        aws.upload_s3(outfile, remote_folder+'/'+outfile, bucket_name=bucket)
260
261
262 View Code Duplication
def plot_trajectories(prefix, resolution=512, rows=4, cols=4, upload=True, 
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
Coding Style introduced by
Trailing whitespace
Loading history...
best-practice introduced by
Too many arguments (7/5)
Loading history...
263
                      remote_folder = "01_18_Experiment", bucket='ccurtis.data'):
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
264
    """
265
    Plot trajectories in video.
266
267
    Parameters
268
    ----------
269
    prefix: string
270
        Prefix of file name to be plotted e.g. features_P1.csv prefix is P1.
271
    resolution: int
272
        Resolution of base image.  Only needed to calculate bounds of image.
273
    rows: int
274
        Rows of base images used to build tiled image.
275
    cols: int
276
        Columns of base images used to build tiled images.
277
    upload: boolean
278
        True if you want to upload to s3.
279
280
    """
281
    merged = pd.read_csv('msd_{}.csv'.format(prefix))
282
    particles = int(max(merged['Track_ID']))
283
    ires = resolution
284
285
    fig = plt.figure(figsize=(12, 12))
286
    for part in range(0, particles):
287
        x = merged[merged['Track_ID'] == part]['X']
0 ignored issues
show
Coding Style Naming introduced by
The name x does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
288
        y = merged[merged['Track_ID'] == part]['Y']
0 ignored issues
show
Coding Style Naming introduced by
The name y does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
289
        plt.plot(x, y, color='k', alpha=0.7)
290
291
    plt.xlim(0, ires*cols)
292
    plt.ylim(0, ires*rows)
293
    plt.axis('off')
294
295
    print('Plotted {} trajectories successfully.'.format(prefix))
296
    outfile = 'traj_{}.png'.format(prefix)
297
    fig.savefig(outfile, bbox_inches='tight')
298
    if upload == True:
0 ignored issues
show
introduced by
Comparison to True should be just 'expr' or 'expr is True'
Loading history...
299
        aws.upload_s3(outfile, remote_folder+'/'+outfile, bucket_name=bucket)
300
301
302 View Code Duplication
def plot_histogram(prefix, xlabel='Log Diffusion Coefficient Dist', ylabel='Trajectory Count',
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
best-practice introduced by
Too many arguments (14/5)
Loading history...
Comprehensibility introduced by
This function exceeds the maximum number of variables (36/15).
Loading history...
303
                   fps=100.02, umppx=0.16, frames=651, y_range=100, frame_interval=20, frame_range=100,
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (103/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Unused Code introduced by
The argument frames seems to be unused.
Loading history...
304
                   analysis='log', theta='D', upload=True, remote_folder = "01_18_Experiment",
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
305
                   bucket='ccurtis.data'):
306
    """
307
    Plot heatmap of trajectories in video with colors corresponding to features.
308
309
    Parameters
310
    ----------
311
    prefix: string
312
        Prefix of file name to be plotted e.g. features_P1.csv prefix is P1.
313
    xlabel: string
314
        X axis label.
315
    ylabel: string
316
        Y axis label.
317
    fps: float64
318
        Frames per second of video.
319
    umppx: float64
320
        Resolution of video in microns per pixel.
321
    frames: int
322
        Number of frames in video.
323
    y_range: float64 or int
324
        Desire y range of graph.
325
    frame_interval: int
326
        Desired spacing between MSDs/Deffs to be plotted.
327
    analysis: string
328
        Desired output format.  If log, will plot log(MSDs/Deffs)
329
    theta: string
330
        Desired output.  D for diffusion coefficients.  Anything else, MSDs.
331
    upload: boolean
332
        True if you want to upload to s3.
333
334
    """
335
    merged = pd.read_csv('msd_{}.csv'.format(prefix))
336
    data = merged
0 ignored issues
show
Unused Code introduced by
The variable data seems to be unused.
Loading history...
337
    frame_range = range(frame_interval, frame_range+frame_interval, frame_interval)
338
339
    # load data
340
341
    # generate keys for legend
342
    bar = {}
0 ignored issues
show
introduced by
Black listed name "bar"
Loading history...
343
    keys = []
344
    entries = []
345
    for i in range(0, len(list(frame_range))):
346
        keys.append(i)
347
        entries.append(str(10*frame_interval*(i+1)) + 'ms')
348
349
    set_x_limit = False
350
    set_y_limit = True
351
    colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
352
    fig = plt.figure(figsize=(16, 6))
353
354
    counter = 0
355
    for i in frame_range:
356
        toi = i/fps
357
        if theta == "MSD":
358
            factor = 1
359
        else:
360
            factor = 4*toi
361
362
        if analysis == 'log':
363
            dist = np.log(umppx*umppx*merged.loc[merged.Frame == i, 'MSDs'].dropna()/factor)
364
            test_bins = np.linspace(-5, 5, 76)
365
        else:
366
            dist = umppx*umppx*merged.loc[merged.Frame == i, 'MSDs'].dropna()/factor
367
            test_bins = np.linspace(0, 20, 76)
368
369
        histogram, test_bins = np.histogram(dist, bins=test_bins)
370
371
        # Plot_general_histogram_code
372
        avg = np.mean(dist)
373
374
        plt.rc('axes', linewidth=2)
375
        plot = histogram
376
        bins = test_bins
377
        width = 0.7 * (bins[1] - bins[0])
378
        center = (bins[:-1] + bins[1:])/2
379
        bar[keys[counter]] = plt.bar(center, plot, align='center', width=width, color=colors[counter], label=entries[counter])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (126/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
380
        plt.axvline(avg, color=colors[counter])
381
        plt.xlabel(xlabel, fontsize=30)
382
        plt.ylabel(ylabel, fontsize=30)
383
        plt.tick_params(axis='both', which='major', labelsize=20)
384
385
        counter = counter + 1
386
        if set_y_limit:
387
            plt.gca().set_ylim([0, y_range])
388
389
        if set_x_limit:
390
            plt.gca().set_xlim([0, x_range])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable x_range does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
Undefined variable 'x_range'
Loading history...
391
392
        plt.legend(fontsize=20, frameon=False)
393
    outfile = 'hist_{}.png'.format(prefix)
394
    fig.savefig(outfile, bbox_inches='tight')
395
    if upload==True:
0 ignored issues
show
Coding Style introduced by
Exactly one space required around comparison
Loading history...
introduced by
Comparison to True should be just 'expr' or 'expr is True'
Loading history...
396
        aws.upload_s3(outfile, remote_folder+'/'+outfile, bucket_name=bucket)
397
398
399 View Code Duplication
def plot_particles_in_frame(prefix, x_range=600, y_range=2000, upload=True,
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
best-practice introduced by
Too many arguments (6/5)
Loading history...
400
                            remote_folder = "01_18_Experiment", bucket='ccurtis.data'):
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
401
    """
402
    Plot number of particles per frame as a function of time.
403
404
    Parameters
405
    ----------
406
    prefix: string
407
        Prefix of file name to be plotted e.g. features_P1.csv prefix is P1.
408
    x_range: float64 or int
409
        Desire x range of graph.
410
    y_range: float64 or int
411
        Desire y range of graph.
412
    upload: boolean
413
        True if you want to upload to s3.
414
415
    """
416
    merged = pd.read_csv('msd_{}.csv'.format(prefix))
417
    frames = int(max(merged['Frame']))
418
    framespace = np.linspace(0, frames, frames)
419
    particles = np.zeros((framespace.shape[0]))
420
    for i in range(0, frames):
421
        particles[i] = merged.loc[merged.Frame == i, 'MSDs'].dropna().shape[0]
422
423
    fig = plt.figure(figsize=(5, 5))
424
    plt.plot(framespace, particles, linewidth=4)
425
    plt.xlim(0, x_range)
426
    plt.ylim(0, y_range)
427
    plt.xlabel('Frames', fontsize=20)
428
    plt.ylabel('Particles', fontsize=20)
429
430
    outfile = 'in_frame_{}.png'.format(prefix)
431
    fig.savefig(outfile, bbox_inches='tight')
432
    if upload == True:
0 ignored issues
show
introduced by
Comparison to True should be just 'expr' or 'expr is True'
Loading history...
433
        aws.upload_s3(outfile, remote_folder+'/'+outfile, bucket_name=bucket)
434
435
436 View Code Duplication
def plot_individual_msds(prefix, x_range=100, y_range=20, umppx=0.16, fps=100.02, alpha=0.01, folder='.', upload=True,
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
Coding Style introduced by
This line is too long as per the coding-style (118/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
best-practice introduced by
Too many arguments (10/5)
Loading history...
Comprehensibility introduced by
This function exceeds the maximum number of variables (22/15).
Loading history...
437
                         remote_folder="01_18_Experiment", bucket='ccurtis.data'):
438
    """
439
    Plot MSDs of trajectories and the geometric average.
440
441
    Parameters
442
    ----------
443
    prefix: string
444
        Prefix of file name to be plotted e.g. features_P1.csv prefix is P1.
445
    x_range: float64 or int
446
        Desire x range of graph.
447
    y_range: float64 or int
448
        Desire y range of graph.
449
    fps: float64
450
        Frames per second of video.
451
    umppx: float64
452
        Resolution of video in microns per pixel.
453
    alpha: float64
454
        Transparency factor.  Between 0 and 1.
455
    upload: boolean
456
        True if you want to upload to s3.
457
458
    Returns
459
    -------
460
    geo_mean: numpy array
461
        Geometric mean of trajectory MSDs at all time points.
462
    geo_SEM: numpy array
463
        Geometric standard errot of trajectory MSDs at all time points.
464
465
    """
466
467
    merged = pd.read_csv('{}/msd_{}.csv'.format(folder, prefix))
468
469
    fig = plt.figure(figsize=(10, 10))
470
    particles = int(max(merged['Track_ID']))
471
    frames = int(max(merged['Frame']))
472
    y = np.zeros((particles+1, frames+1))
0 ignored issues
show
Coding Style Naming introduced by
The name y does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
473
    for i in range(0, particles+1):
474
        y[i, :] = merged.loc[merged.Track_ID == i, 'MSDs']*umppx*umppx
475
        x = merged.loc[merged.Track_ID == i, 'Frame']/fps
0 ignored issues
show
Coding Style Naming introduced by
The name x does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
476
        plt.plot(x, y[i, :], 'k', alpha=alpha)
477
478
    geo_mean = np.nanmean(ma.log(y), axis=0)
479
    geo_SEM = stats.sem(ma.log(y), axis=0, nan_policy='omit')
0 ignored issues
show
Coding Style Naming introduced by
The name geo_SEM does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
480
    plt.plot(x, np.exp(geo_mean), 'k', linewidth=4)
0 ignored issues
show
introduced by
The variable x does not seem to be defined in case the for loop on line 473 is not entered. Are you sure this can never be the case?
Loading history...
481
    plt.plot(x, np.exp(geo_mean-geo_SEM), 'k--', linewidth=2)
482
    plt.plot(x, np.exp(geo_mean+geo_SEM), 'k--', linewidth=2)
483
    plt.xlim(0, x_range)
484
    plt.ylim(0, y_range)
485
    plt.xlabel('Tau (s)', fontsize=25)
486
    plt.ylabel(r'Mean Squared Displacement ($\mu$m$^2$/s)', fontsize=25)
487
488
    outfile = '{}/msds_{}.png'.format(folder, prefix)
489
    outfile2 = '{}/geomean_{}.csv'.format(folder, prefix)
490
    outfile3 = '{}/geoSEM_{}.csv'.format(folder, prefix)
491
    fig.savefig(outfile, bbox_inches='tight')
492
    np.savetxt(outfile2, geo_mean, delimiter=",")
493
    np.savetxt(outfile3, geo_SEM, delimiter=",")
494
    if upload==True:
0 ignored issues
show
Coding Style introduced by
Exactly one space required around comparison
Loading history...
introduced by
Comparison to True should be just 'expr' or 'expr is True'
Loading history...
495
        aws.upload_s3(outfile, remote_folder+'/'+outfile, bucket_name=bucket)
496
        aws.upload_s3(outfile2, remote_folder+'/'+outfile2, bucket_name=bucket)
497
        aws.upload_s3(outfile3, remote_folder+'/'+outfile3, bucket_name=bucket)
498
    return geo_mean, geo_SEM
499