Red-Green Detection, Single Stim

Based on Dovgan et al., 2010, Osypenko et al., 2019

Detecting and Comparing Translocations in Two NCSs

A - wide-field image of EYFP channel, B - differential image for wild-type NCS channel, C - differential image for mutant NCS channel, D1 - insertions mask overlay, D2 - insertions regions area comparison

wt_vs_mut

Class is designed to create differential images and insertion masks for two separate image time series (for instance, wild-type NCS and mutant NCS, or two distinct NCSs). The up_mask_calc method is responsible for detecting insertion regions.

Suitable for image series with single application/stimulation only.

The class attributes contain various types of insertion masks and corresponding profiles for these masks (raw intensity and ΔF/F profiles).

Requires wf_x2_m2 instance type as input!

NB: Optimized for long stimuli/applications with strong translocation and analysis of colocalization of the insertion sites. Methods of this class may not work with image series with weak translocation range.

Parameters:
  • wt_img (ndarray) –

    wild type images time series

  • mut_img (ndarray) –

    mutant images time series

  • proc_mask (ndarray) –

    cell processes boolean mask, extended

  • narrow_proc_mask (ndarray) –

    cell processes boolean mask, unextended

Attributes:
  • wt_img (ndarray[t, x, y]) –

    wild type images time series

  • mut_img (ndarray[t, x, y]) –

    mutant images time series

  • proc_mask (ndarray[x, y]) –

    cell processes boolean mask, extended

  • narrow_proc_mask (ndarray[x, y]) –

    cell processes boolean mask, unextended

  • wt_up_mask (ndarray[x, y]) –

    boolean mask of intensity increase regions for wild-type channel, created with red_green.wt_ws_mut.up_mask_calc()

  • wt_up_label (ndarray[x, y]) –

    label image of intensity increase regions for wild-type channel, created with red_green.wt_ws_mut.up_mask_calc()

  • wt_diff_img (ndarray[x, y]) –

    differential image of intensity changes after stimulation for wild-type channel, created with red_green.wt_ws_mut.up_mask_calc()

  • mut_up_mask (ndarray[x, y]) –

    boolean mask of intensity increase regions for mutant/2nd NCS channel, created with red_green.wt_ws_mut.up_mask_calc()

  • mut_up_label (ndarray[x, y]) –

    label image of intensity increase regions for mutant/2nd NCS channel, created with red_green.wt_ws_mut.up_mask_calc()

  • mut_diff_img (ndarray[x, y]) –

    differential image of intensity changes after stimulation for mutant/2nd NCS channel, created with red_green.wt_ws_mut.up_mask_calc()

  • connected_up_mask (ndarray[x, y]) –

    regions of wt_up_mask which overlay with mut_up_mask, created with utils.masking.mask_connection()

  • halo_up_mask (ndarray[x, y]) –

    connecter_up_mask without mutant intensity increase regions

  • init_up_mask (ndarray[x, y]) –

    connected_up_mask without halo_up_mask (only regions of mut_up_mask which overlay with wt_up_mask)

  • wt_conn_arr (ndarray[int, t]) –

    intensity profiles of wild-type channel for each label element of connected_up_mask, created with utils.masking.label_prof_arr()

  • wt_conn_df_arr (ndarray[ΔF / F, t]) –

    ΔF/F profiles of wild-type channel for each label element of connected_up_mask, created with utils.masking.label_prof_arr()

  • wt_halo_arr (ndarray[int, t]) –

    intensity profiles of wild-type channel for each label element of halo_up_mask, created with utils.masking.label_prof_arr()

  • wt_halo_df_arr (ndarray[ΔF / F, t]) –

    ΔF/F profiles of wild-type channel for each label element of halo_up_mask, created with utils.masking.label_prof_arr()

  • wt_init_arr (ndarray[int, t]) –

    intensity profiles of wild-type channel for each label element of init_up_mask, created with utils.masking.label_prof_arr()

  • wt_init_df_arr (ndarray[ΔF / F, t]) –

    ΔF/F profiles of wild-type channel for each label element of init_up_mask, created with utils.masking.label_prof_arr()

  • mut_conn_arr (ndarray[int, t]) –

    intensity profiles of mutant/2nd NCS channel for each label element of connected_up_mask, created with utils.masking.label_prof_arr()

  • mut_conn_df_arr (ndarray[ΔF / F, t]) –

    ΔF/F profiles of mutant/2nd NCS channel for each label element of connected_up_mask, created with utils.masking.label_prof_arr()

  • mut_halo_arr (ndarray[int, t]) –

    intensity profiles of mutant/2nd NCS channel for each label element of halo_up_mask, created with utils.masking.label_prof_arr()

  • mut_halo_df_arr (ndarray[ΔF / F, t]) –

    ΔF/F profiles of mutant/2nd NCS channel for each label element of halo_up_mask, created with utils.masking.label_prof_arr()

  • mut_init_arr (ndarray[int, t]) –

    intensity profiles of mutant/2nd NCS channel for each label element of init_up_mask, created with utils.masking.label_prof_arr()

  • mut_init_df_arr (ndarray[ΔF / F, t]) –

    ΔF/F profiles of mutant/2nd NCS channel for each label element of init_up_mask, created with utils.masking.label_prof_arr()

Source code in src/domb/red_green/wt_vs_mut.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
 90
 91
 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
def __init__(self, wt_img:np.ndarray, mut_img:np.ndarray,
             proc_mask:np.ndarray, narrow_proc_mask:np.ndarray,
             **kwargs):
    """ Class is designed to create differential images and insertion masks
    for two separate image time series (for instance, wild-type NCS and mutant NCS,
    or two distinct NCSs). The up_mask_calc method is responsible for detecting
    insertion regions.

    Suitable for image series with __single__ application/stimulation only.

    The class attributes contain various types of insertion masks
    and corresponding profiles for these masks (raw intensity and ΔF/F profiles).

    __Requires wf_x2_m2 instance type as input!__

    __NB: Optimized for long stimuli/applications with strong translocation
    and analysis of colocalization of the insertion sites.
    Methods of this class may not work with image series with weak translocation range.__

    Parameters
    ----------
    wt_img: ndarray [t,x,y]
        wild type images time series
    mut_img: ndarray [t,x,u]
        mutant images time series
    proc_mask: ndarray [x,y]
        cell processes boolean mask, extended
    narrow_proc_mask: ndarray [x,y]
        cell processes boolean mask, unextended

    Attributes
    ----------
    wt_img: ndarray [t,x,y]
        wild type images time series
    mut_img: ndarray [t,x,y]
        mutant images time series
    proc_mask: ndarray [x,y]
        cell processes boolean mask, extended
    narrow_proc_mask: ndarray [x,y]
        cell processes boolean mask, unextended
    wt_up_mask: ndarray [x,y]
        boolean mask of intensity increase regions for wild-type channel,
        created with `red_green.wt_ws_mut.up_mask_calc()`
    wt_up_label: ndarray [x,y]
        label image of intensity increase regions for wild-type channel,
        created with `red_green.wt_ws_mut.up_mask_calc()`
    wt_diff_img: ndarray [x,y]
        differential image of intensity changes after stimulation for wild-type channel,
        created with `red_green.wt_ws_mut.up_mask_calc()`
    mut_up_mask: ndarray [x,y]
        boolean mask of intensity increase regions for mutant/2nd NCS channel,
        created with `red_green.wt_ws_mut.up_mask_calc()`
    mut_up_label: ndarray [x,y]
        label image of intensity increase regions for mutant/2nd NCS channel,
        created with `red_green.wt_ws_mut.up_mask_calc()`
    mut_diff_img: ndarray [x,y]
        differential image of intensity changes after stimulation for mutant/2nd NCS channel,
        created with `red_green.wt_ws_mut.up_mask_calc()`
    connected_up_mask: ndarray [x,y]
        regions of `wt_up_mask` which overlay with `mut_up_mask`,
        created with `utils.masking.mask_connection()`
    halo_up_mask: ndarray [x,y]
        `connecter_up_mask` without mutant intensity increase regions
    init_up_mask: ndarray [x,y]
        `connected_up_mask` without `halo_up_mask`
        (only regions of `mut_up_mask` which overlay with `wt_up_mask`)
    wt_conn_arr: ndarray [int, t]
        intensity profiles of wild-type channel for each label element of `connected_up_mask`,
        created with `utils.masking.label_prof_arr()`
    wt_conn_df_arr: ndarray [ΔF/F, t]
        ΔF/F profiles of wild-type channel for each label element of `connected_up_mask`,
        created with `utils.masking.label_prof_arr()`
    wt_halo_arr: ndarray [int, t]
        intensity profiles of wild-type channel for each label element of `halo_up_mask`,
        created with `utils.masking.label_prof_arr()`
    wt_halo_df_arr: ndarray [ΔF/F, t]
        ΔF/F profiles of wild-type channel for each label element of `halo_up_mask`,
        created with `utils.masking.label_prof_arr()`
    wt_init_arr: ndarray [int, t]
        intensity profiles of wild-type channel for each label element of `init_up_mask`,
        created with `utils.masking.label_prof_arr()`
    wt_init_df_arr: ndarray [ΔF/F, t]
        ΔF/F profiles of wild-type channel for each label element of `init_up_mask`,
        created with `utils.masking.label_prof_arr()`
    mut_conn_arr: ndarray [int, t]
        intensity profiles of mutant/2nd NCS channel for each label element of `connected_up_mask`,
        created with `utils.masking.label_prof_arr()`
    mut_conn_df_arr: ndarray [ΔF/F, t]
        ΔF/F profiles of mutant/2nd NCS channel for each label element of `connected_up_mask`,
        created with `utils.masking.label_prof_arr()`
    mut_halo_arr: ndarray [int, t]
        intensity profiles of mutant/2nd NCS channel for each label element of `halo_up_mask`,
        created with `utils.masking.label_prof_arr()`
    mut_halo_df_arr: ndarray [ΔF/F, t]
        ΔF/F profiles of mutant/2nd NCS channel for each label element of `halo_up_mask`,
        created with `utils.masking.label_prof_arr()`
    mut_init_arr: ndarray [int, t]
        intensity profiles of mutant/2nd NCS channel for each label element of `init_up_mask`,
        created with `utils.masking.label_prof_arr()`
    mut_init_df_arr: ndarray [ΔF/F, t]
        ΔF/F profiles of mutant/2nd NCS channel for each label element of `init_up_mask`,
        created with `utils.masking.label_prof_arr()`

    """
    self.wt_img = wt_img
    self.mut_img = mut_img
    self.proc_mask = proc_mask
    self.narrow_proc_mask = narrow_proc_mask

    # WT masking
    self.wt_up_mask, self.wt_up_label, self.wt_diff_img = self.up_mask_calc(self.wt_img,
                                                                            self.proc_mask,
                                                                            **kwargs)

    # mutant masking
    self.mut_up_mask, self.mut_up_label, self.mut_diff_img = self.up_mask_calc(self.mut_img,
                                                                               self.proc_mask,
                                                                               **kwargs)

    # masks modification
    self.connected_up_mask, self.connected_up_label = masking.mask_connection(input_master_mask=self.wt_up_mask,
                                                                              input_minor_mask=self.mut_up_mask)
    self.halo_up_mask = np.copy(self.connected_up_mask)
    self.halo_up_mask[self.mut_up_mask] = 0
    self.halo_up_label = np.copy(self.connected_up_label)
    self.halo_up_label[self.mut_up_mask] = 0

    self.init_up_mask = np.copy(self.connected_up_mask)
    self.init_up_mask[self.halo_up_mask] = 0
    self.init_up_label = np.copy(self.connected_up_label)
    self.init_up_label[self.halo_up_mask] = 0

    # WT ins profiles calc
    self.wt_conn_df_arr, self.wt_conn_arr = masking.label_prof_arr(input_label=self.connected_up_label,
                                                                   input_img_series=self.wt_img)
    self.wt_halo_df_arr, self.wt_halo_arr = masking.label_prof_arr(input_label=self.halo_up_label,
                                                                   input_img_series=self.wt_img)
    self.wt_init_df_arr, self.wt_init_arr = masking.label_prof_arr(input_label=self.init_up_label,
                                                                   input_img_series=self.wt_img)

    # # WT trans profiles calc
    # self.wt_conn_rois_trans_arr, self.wt_conn_tot_trans_arr = masking.trans_prof_arr(input_total_mask=self.narrow_proc_mask,
    #                                                                                  input_labels=self.connected_up_label,
    #                                                                                  input_img_series=self.wt_img)
    # self.wt_halo_rois_trans_arr, self.wt_halo_tot_trans_arr = masking.trans_prof_arr(input_total_mask=self.narrow_proc_mask,
    #                                                                                  input_labels=self.halo_up_label,
    #                                                                                  input_img_series=self.wt_img)
    # self.wt_init_rois_trans_arr, self.wt_init_tot_trans_arr = masking.trans_prof_arr(input_total_mask=self.narrow_proc_mask,
    #                                                                                  input_labels=self.init_up_label,
    #                                                                                  input_img_series=self.wt_img)

    # mut profiles calc
    self.mut_conn_df_arr, self.mut_conn_arr = masking.label_prof_arr(input_label=self.connected_up_label,
                                                                     input_img_series=self.mut_img)
    self.mut_halo_df_arr, self.mut_halo_arr = masking.label_prof_arr(input_label=self.halo_up_label,
                                                                     input_img_series=self.mut_img)
    self.mut_init_df_arr, self.mut_init_arr = masking.label_prof_arr(input_label=self.init_up_label,
                                                                     input_img_series=self.mut_img)

up_mask_calc staticmethod

Function for generating an insertion regions mask using the differential image method.

Could be used in stand-alone mode as a static method of the WTvsMut class.

Parameters:
  • input_img_series (ndarray) –

    image time series

  • input_img_mask (ndarray) –

    cell region boolean mask

  • sd_tolerance (int, default: 2 ) –

    insertion ("up") region detection threshold: the number of standard deviations of extracellular noise (measured in the area outside of input_img_mask)

  • base_frames (int, default: 5 ) –

    number of frames from the beginning of the image series used to create an image of basal fluorescence

  • stim_start (int, default: 7 ) –

    index of the frame where stimulation begins

  • stim_win (int, default: 5 ) –

    stimulation window, the number of frames following stim_start index that are used to create an image displaying maximal insertions

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

    boolean mask of intensity increase regions

  • up_label( ndarray[x, y] ) –

    label image of intensity increase regions

  • img_diff( ndarray[x, y] ) –

    differential image of intensity changes after stimulation

src/domb/red_green/wt_vs_mut.py
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
@staticmethod
def up_mask_calc(input_img_series:np.ndarray, input_img_mask:np.ndarray,
                 sd_tolerance:int=2,
                 base_frames:int=5, stim_start:int=7, stim_win:int=5):
    """ Function for generating an insertion regions mask
    using the differential image method.

    Could be used in stand-alone mode as a static method of the WTvsMut class.

    Parameters
    ----------
    input_img_series: ndarray [t,x,y]
        image time series
    input_img_mask: ndarray [x,y]
        cell region boolean mask
    sd_tolerance: int
        insertion ("up") region detection threshold: the number
        of standard deviations of extracellular noise
        (measured in the area outside of `input_img_mask`)
    base_frames: int
        number of frames from the beginning
        of the image series used to create
        an image of basal fluorescence
    stim_start: int
        index of the frame where stimulation begins
    stim_win: int
        stimulation window, the number of frames following `stim_start` index
        that are used to create an image displaying maximal insertions

    Returns
    -------
    up_mask_filt: ndarray [x,y]
        boolean mask of intensity increase regions
    up_label: ndarray [x,y]
        label image of intensity increase regions  
    img_diff: ndarray [x,y]
        differential image of intensity changes after stimulation

    """
    ref_img_series = filters.gaussian(input_img_series, sigma=1.25, channel_axis=0)

    img_base = np.mean(ref_img_series[:base_frames], axis=0)
    img_max = np.mean(ref_img_series[stim_start:stim_start+stim_win], axis=0)

    img_diff = img_max - img_base
    img_diff = img_diff/np.max(np.abs(img_diff))

    diff_sd = np.std(ma.masked_where(~input_img_mask, img_diff))
    up_mask = img_diff > diff_sd * sd_tolerance

    up_mask_filt = morphology.opening(up_mask, footprint=morphology.disk(2))
    up_mask_filt = morphology.dilation(up_mask_filt, footprint=morphology.disk(1))
    up_label = measure.label(up_mask_filt)

    return up_mask_filt, up_label, img_diff