Plotting

Functions for plotting, image creation, cmaps

CMaps

Class for custom colormaps storage. To use colormaps with matplotlib.imshow just call plot.CMaps().cmap_name.

Attributes:
  • cmap_cyan (matplotlib cmap) –

    cyan colormap

  • cmap_yellow (matplotlib cmap) –

    yellow colormap

  • cmap_red_green (matplotlib cmap) –

    red-green colormap (low-green, zero-black, high-red), especially suitable for differential images

Source code in src/domb/utils/plot.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def __init__(self):
    """
    Class for custom colormaps storage.
    To use colormaps with `matplotlib.imshow` just call `plot.CMaps().cmap_name`.

    Attributes
    ----------
    cmap_cyan: matplotlib cmap
        cyan colormap
    cmap_yellow: matplotlib cmap
        yellow colormap
    cmap_red_green: matplotlib cmap
        red-green colormap (low-green, zero-black, high-red),
        especially suitable for differential images

    """
    # CFP cmap
    dict_cyan = {'red':(
                (0.0, 0.0, 0.0),
                (1.0, 0.0, 0.0)),
                'blue':(
                (0.0, 0.0, 0.0),
                (1.0, 1.0, 1.0)),
                'green':(
                (0.0, 0.0, 0.0),
                (1.0, 1.0, 1.0))}
    self.cmap_cyan = LinearSegmentedColormap('Cyan', dict_cyan)

    # YFP cmap
    dict_yellow = {'red':(
                (0.0, 0.0, 0.0),
                (1.0, 1.0, 1.0)),
                'blue':(
                (0.0, 0.0, 0.0),
                (1.0, 0.0, 0.0)),
                'green':(
                (0.0, 0.0, 0.0),
                (1.0, 1.0, 1.0))}
    self.cmap_yellow = LinearSegmentedColormap('Yellow', dict_yellow)

    # red-green cmap creation
    dict_red_green = {'red':(
                    (0.0, 0.0, 0.0),
                    (0.5, 0.0, 0.0),
                    (0.55, 0.3, 0.7),
                    (1.0, 1.0, 1.0)),
                    'blue':(
                    (0.0, 0.0, 0.0),
                    (1.0, 0.0, 0.0)),
                    'green':(
                    (0.0, 1.0, 1.0),
                    (0.45, 0.7, 0.3),
                    (0.5, 0.0, 0.0),
                    (1.0, 0.0, 0.0))}
    self.cmap_red_green = LinearSegmentedColormap('RedGreen', dict_red_green)


    @staticmethod
    def plot_linearmap(cdict):
        newcmp = LinearSegmentedColormap('testCmap', segmentdata=cdict, N=256)
        rgba = newcmp(np.linspace(0, 1, 256))
        fig, ax = plt.subplots(figsize=(4, 3), constrained_layout=True)
        col = ['r', 'g', 'b']
        for xx in [0.25, 0.5, 0.75]:
            ax.axvline(xx, color='0.7', linestyle='--')
        for i in range(3):
            ax.plot(np.arange(256)/256, rgba[:, i], color=col[i])
        ax.set_xlabel('index')
        ax.set_ylabel('RGB')
        plt.show()

overlay_line_plot

Line plot with variance whiskers for array [t,val], could take as input results ofutil.masking.label_prof_arr()` function. Plots every row from the input array as an individual line

Parameters:
  • input_arr (ndarray) –

    list of 2D arrays with profiles [t, val]

  • min_amp (float, default: 0 ) –

    minimal amplitude of displayed profiles

  • max_amp (float, default: 10 ) –

    maximal amplitude of displayed profiles

src/domb/utils/plot.py
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
def overlay_line_plot(input_arr: np.ndarray,
                      t_scale:int=2,
                      min_amp:float=0,max_amp:float=10,
                      app_bar_dict:dict()=None, show_app_bars:bool=False,
                      stim_t:list[int]=None, show_stim:bool=False,
                      figsize:tuple=(15,10), x_lab:str='NA', y_lab:str='NA', plot_title:str='NA',
                      save_path=False):
    """ Line plot with variance whiskers for array `[t,val],
    could take as input results of `util.masking.label_prof_arr()` function.
    Plots every row from the input array as an individual line

    Parameters
    ----------
    input_arr: ndarray [dF_value,t]
        list of 2D arrays with profiles `[t, val]`
    min_amp: float, optional
        minimal amplitude of displayed profiles
    max_amp: float, optional
        maximal amplitude of displayed profiles

    """
    time_line = np.linspace(0, input_arr.shape[1]*t_scale, \
                            num=input_arr.shape[1])

    max_list = []
    plt.figure(figsize=figsize)

    for num_ROI in range(input_arr.shape[0]):
        prof_ROI = input_arr[num_ROI]
        if (prof_ROI.max() < min_amp) | (prof_ROI.max() > max_amp):
            continue
        else:
            plt.plot(time_line, prof_ROI, alpha=.65, marker='o')
            max_list.append(prof_ROI.max())

    if show_stim:
        for s_t in stim_t:
            plt.axvline(x=s_t, color='k', linestyle='--', linewidth=1.5)

    if show_app_bars:
            for bar_name in app_bar_dict:
                bar_lim = app_bar_dict[bar_name]
                plt.plot(bar_lim, [max(max_list)+0.05] * len(bar_lim),
                         linewidth=4, color='k')

    plt.grid(color='grey', linewidth=.25)
    plt.xlabel(x_lab)
    plt.ylabel(y_lab)
    plt.title(f'{plot_title}, min {min_amp}, max {max_amp}')
    plt.legend()
    plt.tight_layout()

    if save_path:
        plt.savefig(save_path, dpi=300)
    else:
        plt.show()

stat_line_plot

Line plot with variance whiskers for set of arrays [t,val], could take as input results ofutil.masking.label_prof_arr()` function

Parameters:
  • arr_list (list) –

    list of 2D arrays with profiles [t, val]

  • lab_list (list) –

    list of labels for lines, should be same len with arr_list

  • t_scale (int, default: 2 ) –

    time scale for x-axis, in seconds between two frames

  • stat_method (str, default: 'se' ) –

    method for line plot calculation: se - mean +/- standat error of mean, iqr - median +/- IQR, ci - mean +/- 95% confidence interval

src/domb/utils/plot.py
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
def stat_line_plot(arr_list: list,
                   lab_list:list,
                   t_scale:int=2,
                   stat_method:str='se',
                   app_bar_dict:dict()=None, show_app_bars:bool=False,
                   stim_t:list[int]=None, show_stim:bool=False,
                   figsize:tuple=(15,10), x_lab:str='NA', y_lab:str='NA', plot_title:str='NA',
                   save_path=False):
    """ Line plot with variance whiskers for set of arrays `[t,val],
    could take as input results of `util.masking.label_prof_arr()` function

    Parameters
    ----------
    arr_list: list
        list of 2D arrays with profiles `[t, val]`
    lab_list: list
        list of labels for lines, should be same len with arr_list
    t_scale: float
        time scale for x-axis, in seconds between two frames
    stat_method: str, optional {'se', 'iqr', 'ci'}
        method for line plot calculation: `se` - mean +/- standat error of mean,
        `iqr` - median +/- IQR, `ci` - mean +/- 95% confidence interval
    """
    time_line = np.linspace(0, arr_list[0].shape[1]*t_scale, \
                            num=arr_list[0].shape[1])

    # https://aegis4048.github.io/comprehensive_confidence_intervals_for_python_developers

    # mean, se
    arr_se_stat = lambda x: (np.mean(x, axis=0), \
                             np.std(x, axis=0)/np.sqrt(x.shape[1]))  # mean, se

    # meadian, IQR
    arr_iqr_stat = lambda x: (np.median(x, axis=0), \
                              stats.iqr(x, axis=0))

    # mean, CI
    arr_ci_stat = lambda x, alpha=0.05: (np.mean(x, axis=0), \
                                         stats.t.ppf(1-alpha/2, df=x.shape[1]-1) \
                                                    *np.std(x, axis=0, ddof=1)/np.sqrt(x.shape[1]))

    stat_dict = {'se':arr_se_stat,
                 'iqr':arr_iqr_stat,
                 'ci':arr_ci_stat}
    max_list = []
    plt.figure(figsize=figsize)
    for num in range(len(arr_list)):
        arr = arr_list[num]
        lab = lab_list[num]
        arr_val, arr_var = stat_dict[stat_method](arr)
        max_list.append(arr_val.max())

        plt.errorbar(time_line, arr_val,
                     yerr = arr_var,
                     fmt ='-o', capsize=2, label=lab)

    if show_stim:
        for s_t in stim_t:
            plt.axvline(x=s_t, color='k', linestyle='--', linewidth=1.5)

    if show_app_bars:
            for bar_name in app_bar_dict:
                bar_lim = app_bar_dict[bar_name]
                plt.plot(bar_lim, [max(max_list)+0.05] * len(bar_lim),
                         linewidth=4, color='k')

    plt.grid(color='grey', linewidth=.25)
    plt.xlabel(x_lab)
    plt.ylabel(y_lab)
    plt.title(plot_title)
    plt.legend()
    plt.tight_layout()

    if save_path:
        plt.savefig(save_path, dpi=300)
    else:
        plt.show()

toRGB

Convert three 2d arrays to RGB (input arrays must have same size).

Parameters:
  • r_img (ndarray) –

    2D array for red chennel

  • g_img (ndarray) –

    2D array for green chennel

  • b_img (ndarray) –

    2D array for blue chennel

Returns:
  • rgb_img( ndarray[x, y] ) –

    RGB image

src/domb/utils/plot.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
def toRGB(r_img:np.ndarray, g_img:np.ndarray, b_img:np.ndarray):
    """ Convert three 2d arrays to RGB (input arrays must have same size).

    Parameters
    ----------
    r_img: ndarray [x,y]
        2D array for red chennel
    g_img: ndarray [x,y]
        2D array for green chennel
    b_img: ndarray [x,y]
        2D array for blue chennel

    Returns
    -------
    rgb_img: ndarray [x,y]
        RGB image

    """
    r_img = np.asarray(r_img, dtype='uint8')
    r_norm_img = (r_img - np.min(r_img)) / (np.max(r_img) - np.min(r_img)).astype(np.uint8)

    g_img = np.asarray(g_img, dtype='uint8')
    g_norm_img = (g_img - np.min(g_img)) / (np.max(g_img) - np.min(g_img)).astype(np.uint8)

    b_img = np.asarray(b_img, dtype='uint8')
    b_norm_img = (b_img - np.min(b_img)) / (np.max(b_img) - np.min(b_img)).astype(np.uint8)

    rgb_img = np.stack([r_norm_img, g_norm_img, b_norm_img], axis=-1) 
    return rgb_img