Skip to content

Scale

central_tile(tilepos_yx, use_tiles)

returns tile in use_tiles closest to centre.

Parameters:

Name Type Description Default
tilepos_yx np.ndarray

int [n_tiles x 2]. tiff tile positions (index 0 refers to [0,0]).

required
use_tiles List[int]

int [n_use_tiles]. Tiles used in the experiment.

required

Returns:

Type Description
int

tile in use_tiles closest to centre.

Source code in coppafish/extract/scale.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def central_tile(tilepos_yx: np.ndarray, use_tiles: List[int]) -> int:
    """
    returns tile in use_tiles closest to centre.

    Args:
        tilepos_yx: ```int [n_tiles x 2]```.
            tiff tile positions (index ```0``` refers to ```[0,0]```).
        use_tiles: ```int [n_use_tiles]```.
            Tiles used in the experiment.

    Returns:
        tile in ```use_tiles``` closest to centre.
    """
    mean_yx = np.round(np.mean(tilepos_yx, 0))
    nearest_t = np.linalg.norm(tilepos_yx[use_tiles] - mean_yx, axis=1).argmin()
    return int(use_tiles[nearest_t])

get_scale(nbp_file, nbp_basic, r, use_tiles, use_channels, use_z, scale_norm, filter_kernel, smooth_kernel=None)

Convolves the image for tile t, channel c, z-plane z with filter_kernel then gets the multiplier to apply to filtered nd2 images by dividing scale_norm by the max value of this filtered image.

Parameters:

Name Type Description Default
nbp_file NotebookPage

file_names notebook page

required
nbp_basic NotebookPage

basic_info notebook page

required
r int

Round to get scale from. Should be 0 to determine scale and the anchor round (last round) to determine scale_anchor.

required
use_tiles List[int]

int [n_use_tiles]. tiff tile indices to consider when finding tile.

required
use_channels List[int]

int [n_use_channels]. Channels to consider when finding channel.

required
use_z List[int]

int [n_z]. Z-planes to consider when finding z_plane.

required
scale_norm int

Desired maximum pixel value of npy images. Typical: 40000.

required
filter_kernel np.ndarray

float [ny_kernel x nx_kernel]. Kernel to convolve nd2 data with to produce npy tiles. Typical shape: [13 x 13].

required
smooth_kernel Optional[np.ndarray]

float [ny_smooth x nx_smooth]. 2D kernel to smooth filtered image with npy with. Typical shape: [3 x 3]. If None, no smoothing is applied

None

Returns:

Type Description
int
  • t - int. npy tile index (index 0 refers to tilepos_yx_npy=[MaxY, MaxX]) scale found from.
int
  • c - int. Channel scale found from.
int
  • z - int. Z-plane scale found from.
float
  • scale - float. Multiplier to apply to filtered nd2 images before saving as npy so full npy uint16 range occupied.
Source code in coppafish/extract/scale.py
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
def get_scale(nbp_file: NotebookPage, nbp_basic: NotebookPage, r: int, use_tiles: List[int],
              use_channels: List[int], use_z: List[int], scale_norm: int,
              filter_kernel: np.ndarray, smooth_kernel: Optional[np.ndarray] = None) -> Tuple[int, int, int, float]:
    """
    Convolves the image for tile ```t```, channel ```c```, z-plane ```z``` with ```filter_kernel```
    then gets the multiplier to apply to filtered nd2 images by dividing ```scale_norm``` by the max value of this
    filtered image.

    Args:
        nbp_file: `file_names` notebook page
        nbp_basic: `basic_info` notebook page
        r: Round to get `scale` from.
            Should be 0 to determine `scale` and the anchor round (last round) to determine `scale_anchor`.
        use_tiles: ```int [n_use_tiles]```.
            tiff tile indices to consider when finding tile.
        use_channels: ```int [n_use_channels]```.
            Channels to consider when finding channel.
        use_z: ```int [n_z]```.
            Z-planes to consider when finding z_plane.
        scale_norm: Desired maximum pixel value of npy images. Typical: ```40000```.
        filter_kernel: ```float [ny_kernel x nx_kernel]```.
            Kernel to convolve nd2 data with to produce npy tiles. Typical shape: ```[13 x 13]```.
        smooth_kernel: ```float [ny_smooth x nx_smooth]```.
            2D kernel to smooth filtered image with npy with. Typical shape: ```[3 x 3]```.
            If None, no smoothing is applied

    Returns:
        - ```t``` - ```int```.
            npy tile index (index ```0``` refers to ```tilepos_yx_npy=[MaxY, MaxX]```) scale found from.
        - ```c``` - ```int```.
            Channel scale found from.
        - ```z``` - ```int```.
            Z-plane scale found from.
        - ```scale``` - ```float```.
            Multiplier to apply to filtered nd2 images before saving as npy so full npy ```uint16``` range occupied.
    """
    # tile to get scale from is central tile
    t = central_tile(nbp_basic.tilepos_yx, use_tiles)
    # find z-plane with max pixel across all channels of tile t
    c, z, image = get_z_plane(nbp_file, nbp_basic, r, t, use_channels, use_z)
    # convolve_2d image in same way we convolve_2d before saving tiff files
    im_filtered = utils.morphology.convolve_2d(image, filter_kernel)
    if smooth_kernel is not None:
        im_filtered = utils.morphology.imfilter(im_filtered, smooth_kernel, oa=False)
    scale = scale_norm / im_filtered.max()
    return t, c, z, float(scale)

get_scale_from_txt(txt_file, scale, scale_anchor, tol=0.001)

This checks whether scale and scale_anchor values used for producing npy files in tile_dir match values used and saved to txt_file on previous run.

Will raise error if they are different.

Parameters:

Name Type Description Default
txt_file str

nb.file_names.scale, path to text file where scale values are saved. File contains two values, scale first and scale_anchor second. Values will be 0 if not used or not yet computed.

required
scale Optional[float]

Value of scale used for current run of extract method i.e. config['extract']['scale'].

required
scale_anchor Optional[float]

Value of scale_anchor used for current run of extract method i.e. config['extract']['scale_anchor'].

required
tol float

Two scale values will be considered the same if they are closer than this.

0.001

Returns:

Type Description
float

scale - If txt_file exists, this will be the value saved in it otherwise will just be the input value.

float

scale_anchor - If txt_file exists, this will be the value saved in it otherwise will just be the input value.

Source code in coppafish/extract/scale.py
 9
10
11
12
13
14
15
16
17
18
19
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
def get_scale_from_txt(txt_file: str, scale: Optional[float], scale_anchor: Optional[float],
                       tol: float = 0.001) -> Tuple[float, float]:
    """
    This checks whether `scale` and `scale_anchor` values used for producing npy files in *tile_dir* match
    values used and saved to `txt_file` on previous run.

    Will raise error if they are different.

    Args:
        txt_file: `nb.file_names.scale`, path to text file where scale values are saved.
            File contains two values, `scale` first and `scale_anchor` second.
            Values will be 0 if not used or not yet computed.
        scale: Value of `scale` used for current run of extract method i.e. `config['extract']['scale']`.
        scale_anchor: Value of `scale_anchor` used for current run of extract method
            i.e. `config['extract']['scale_anchor']`.
        tol: Two scale values will be considered the same if they are closer than this.

    Returns:
        scale - If txt_file exists, this will be the value saved in it otherwise will just be the input value.
        scale_anchor - If txt_file exists, this will be the value saved in it otherwise will just be the input value.
    """
    if os.path.isfile(txt_file):
        scale_saved, scale_anchor_saved = np.genfromtxt(txt_file)
        if np.abs(scale_saved) < tol:
            pass  # 0 means scale not used so do nothing
        elif scale is None:
            warnings.warn("Using value of scale = {:.2f} saved in\n".format(scale_saved) + txt_file)
            scale = float(scale_saved)  # Set to saved value used up till now if not specified
        elif np.abs(scale - scale_saved) > tol:
            raise ValueError(f"\nImaging round (Not anchor) tiles saved so far were calculated with scale = "
                             f"{scale_saved}\nas saved in {txt_file}\n"
                             f"This is different from config['extract']['scale'] = {scale}.")
        if np.abs(scale_anchor_saved) < tol:
            pass  # 0 means scale_anchor not computed yet so do nothing
        elif scale_anchor is None:
            warnings.warn("Using value of scale_anchor = {:.2f} saved in\n".format(scale_anchor_saved) + txt_file)
            scale_anchor = float(scale_anchor_saved)  # Set to saved value used up till now if not specified
        elif np.abs(scale_anchor - scale_anchor_saved) > tol:
            raise ValueError(f"\nAnchor round tiles saved so far were calculated with scale_anchor = "
                             f"{scale_anchor_saved}\nas saved in {txt_file}\n"
                             f"This is different from config['extract']['scale_anchor'] = {scale_anchor}.")
    return scale, scale_anchor

get_z_plane(nbp_file, nbp_basic, r, t, use_channels, use_z)

Finds z plane and channel that has maximum pixel value for given round and tile.

Parameters:

Name Type Description Default
nbp_file NotebookPage

file_names notebook page

required
nbp_basic NotebookPage

basic_info notebook page

required
r int

Round to consider.

required
t int

npy tile index (index 0 refers to tilepos_yx_npy=[MaxY, MaxX]) to find z-plane from.

required
use_channels List[int]

int [n_use_channels]. Channels to consider.

required
use_z List[int]

int [n_z]. Z-planes to consider.

required

Returns:

Type Description
int
  • max_channel - int. Channel to which image with max pixel value corresponds.
int
  • max_z - int. Z-plane to which image with max pixel value corresponds.
np.ndarray
  • image - int [tile_sz x tile_sz]. Corresponding image.
Source code in coppafish/extract/scale.py
 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
def get_z_plane(nbp_file: NotebookPage, nbp_basic: NotebookPage, r: int, t: int, use_channels: List[int],
                use_z: List[int]) -> Tuple[int, int, np.ndarray]:
    """
    Finds z plane and channel that has maximum pixel value for given round and tile.

    Args:
        nbp_file: `file_names` notebook page
        nbp_basic: `basic_info` notebook page
        r: Round to consider.
        t: npy tile index (index ```0``` refers to ```tilepos_yx_npy=[MaxY, MaxX]```) to find z-plane from.
        use_channels: ```int [n_use_channels]```.
            Channels to consider.
        use_z: ```int [n_z]```.
            Z-planes to consider.

    Returns:
        - ```max_channel``` - ```int```.
            Channel to which image with max pixel value corresponds.
        - ```max_z``` - ```int```.
            Z-plane to which image with max pixel value corresponds.
        - ```image``` - ```int [tile_sz x tile_sz]```.
            Corresponding image.
    """
    round_dask_array = utils.raw.load(nbp_file, nbp_basic, r=r)
    image_max = np.zeros((len(use_channels), len(use_z)))
    for i in range(len(use_channels)):
        image_max[i, :] = np.max(np.max(utils.raw.load(nbp_file, nbp_basic, round_dask_array, r,
                                                       t, use_channels[i], use_z), axis=0), axis=0)
    max_channel = use_channels[np.max(image_max, axis=1).argmax()]
    max_z = use_z[np.max(image_max, axis=0).argmax()]
    return max_channel, max_z, utils.raw.load(nbp_file, nbp_basic, round_dask_array, r, t, max_channel, max_z)

save_scale(txt_file, scale, scale_anchor)

This saves scale and scale_anchor to txt_file. If either scale and scale_anchor are None, they will be set to 0 when saving.

Parameters:

Name Type Description Default
txt_file str

nb.file_names.scale, path to text file where scale values are to be saved. File will contain two values, scale first and scale_anchor second. Values will be 0 if not used or not yet computed.

required
scale Optional[float]

Value of scale used for current run of extract method i.e. config['extract']['scale'] or value computed from get_scale.

required
scale_anchor Optional[float]

Value of scale_anchor used for current run of extract method i.e. config['extract']['scale_anchor'] or value computed from get_scale.

required
Source code in coppafish/extract/scale.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def save_scale(txt_file: str, scale: Optional[float], scale_anchor: Optional[float]):
    """
    This saves `scale` and `scale_anchor` to `txt_file`. If either `scale` and `scale_anchor` are `None`,
    they will be set to 0 when saving.

    Args:
        txt_file: `nb.file_names.scale`, path to text file where scale values are to be saved.
            File will contain two values, `scale` first and `scale_anchor` second.
            Values will be 0 if not used or not yet computed.
        scale: Value of `scale` used for current run of extract method i.e. `config['extract']['scale']` or
            value computed from `get_scale`.
        scale_anchor: Value of `scale_anchor` used for current run of extract method
            i.e. `config['extract']['scale_anchor']` or value computed from `get_scale`.

    """
    scale, scale_anchor = get_scale_from_txt(txt_file, scale, scale_anchor)  # check if match current saved values
    if scale is None:
        scale = 0
    if scale_anchor is None:
        scale_anchor = 0
    np.savetxt(txt_file, [scale, scale_anchor], header='scale followed by scale_anchor')