Skip to content

Tile Detail

get_tile_file_names(tile_directory, file_base, n_tiles, n_channels=0)

Gets array of all tile file paths which will be saved in tile directory.

Parameters:

Name Type Description Default
tile_directory str

Path to folder where tiles npy files saved.

required
file_base List[str]

str [n_rounds]. file_base[r] is identifier for round r.

required
n_tiles int

Number of tiles in data set.

required
n_channels int

Total number of imaging channels if using 3D. 0 if using 2D pipeline as all channels saved in same file.

0

Returns:

Type Description
np.ndarray

object [n_tiles x n_rounds (x n_channels)].

np.ndarray

tile_files such that

np.ndarray
  • If 2D so n_channels = 0, tile_files[t, r] is the full path to npy file containing all channels of tile t, round r.
np.ndarray
  • If 3D so n_channels > 0, tile_files[t, r] is the full path to npy file containing all z-planes of
np.ndarray

tile t, round r, channel c.

Source code in coppafish/setup/tile_details.py
 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
def get_tile_file_names(tile_directory: str, file_base: List[str], n_tiles: int, n_channels: int = 0) -> np.ndarray:
    """
    Gets array of all tile file paths which will be saved in tile directory.

    Args:
        tile_directory: Path to folder where tiles npy files saved.
        file_base: `str [n_rounds]`.
            `file_base[r]` is identifier for round `r`.
        n_tiles: Number of tiles in data set.
        n_channels: Total number of imaging channels if using 3D.
            `0` if using 2D pipeline as all channels saved in same file.

    Returns:
        `object [n_tiles x n_rounds (x n_channels)]`.
        `tile_files` such that

        - If 2D so `n_channels = 0`, `tile_files[t, r]` is the full path to npy file containing all channels of
            tile `t`, round `r`.
        - If 3D so `n_channels > 0`, `tile_files[t, r]` is the full path to npy file containing all z-planes of
        tile `t`, round `r`, channel `c`.
    """
    n_rounds = len(file_base)
    if n_channels == 0:
        # 2D
        tile_files = np.zeros((n_tiles, n_rounds), dtype=object)
        for r in range(n_rounds):
            for t in range(n_tiles):
                tile_files[t, r] = \
                    get_tile_name(tile_directory, file_base, r, t)
    else:
        # 3D
        tile_files = np.zeros((n_tiles, n_rounds, n_channels), dtype=object)
        for r in range(n_rounds):
            for t in range(n_tiles):
                for c in range(n_channels):
                    tile_files[t, r, c] = \
                        get_tile_name(tile_directory, file_base, r, t, c)
    return tile_files

get_tile_name(tile_directory, file_base, r, t, c=None)

Finds the full path to tile, t, of particular round, r, and channel, c, in tile_directory.

Parameters:

Name Type Description Default
tile_directory str

Path to folder where tiles npy files saved.

required
file_base List[str]

str [n_rounds]. file_base[r] is identifier for round r.

required
r int

Round of desired npy image.

required
t int

Tile of desired npy image.

required
c Optional[int]

Channel of desired npy image.

None

Returns:

Type Description
str

Full path of tile npy file.

Source code in coppafish/setup/tile_details.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def get_tile_name(tile_directory: str, file_base: List[str], r: int, t: int, c: Optional[int] = None) -> str:
    """
    Finds the full path to tile, `t`, of particular round, `r`, and channel, `c`, in `tile_directory`.

    Args:
        tile_directory: Path to folder where tiles npy files saved.
        file_base: `str [n_rounds]`.
            `file_base[r]` is identifier for round `r`.
        r: Round of desired npy image.
        t: Tile of desired npy image.
        c: Channel of desired npy image.

    Returns:
        Full path of tile npy file.
    """
    if c is None:
        tile_name = os.path.join(tile_directory, '{}_t{}.npy'.format(file_base[r], t))
    else:
        tile_name = os.path.join(tile_directory, '{}_t{}c{}.npy'.format(file_base[r], t, c))
    return tile_name

get_tilepos(xy_pos, tile_sz)

Using xy_pos from nd2 metadata, this obtains the yx position of each tile. I.e. how tiles are arranged with respect to each other. Note that this is indexed differently in nd2 file and npy files in the tile directory.

Parameters:

Name Type Description Default
xy_pos np.ndarray

float [n_tiles x 2]. xy position of tiles in pixels. Obtained from nd2 metadata.

required
tile_sz int

xy dimension of tile in pixels.

required

Returns:

Type Description
np.ndarray
  • tilepos_yx_nd2 - int [n_tiles x 2]. tilepos_yx_nd2[i] is yx index of tile with fov index i in nd2 file. Index 0 refers to YX = [0, 0]. Index 1 refers to YX = [0, 1] if MaxX > 0.
np.ndarray
  • tilepos_yx_npy - int [n_tiles x 2]. tilepos_yx_npy[i, 0] is yx index of tile with tile directory (npy files) index i. Index 0 refers to YX = [MaxY, MaxX]. Index 1 refers to YX = [MaxY, MaxX - 1] if MaxX > 0.
Source code in coppafish/setup/tile_details.py
 6
 7
 8
 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
def get_tilepos(xy_pos: np.ndarray, tile_sz: int) -> Tuple[np.ndarray, np.ndarray]:
    """
    Using `xy_pos` from nd2 metadata, this obtains the yx position of each tile.
    I.e. how tiles are arranged with respect to each other.
    Note that this is indexed differently in nd2 file and npy files in the tile directory.

    Args:
        xy_pos: `float [n_tiles x 2]`.
            xy position of tiles in pixels. Obtained from nd2 metadata.
        tile_sz: xy dimension of tile in pixels.

    Returns:
        - `tilepos_yx_nd2` - `int [n_tiles x 2]`.
            `tilepos_yx_nd2[i]` is yx index of tile with fov index `i` in nd2 file.
            Index 0 refers to ```YX = [0, 0]```.
            Index 1 refers to ```YX = [0, 1] if MaxX > 0```.
        - `tilepos_yx_npy` - `int [n_tiles x 2]`.
            `tilepos_yx_npy[i, 0]` is yx index of tile with tile directory (npy files) index `i`.
            Index 0 refers to ```YX = [MaxY, MaxX]```.
            Index 1 refers to ```YX = [MaxY, MaxX - 1] if MaxX > 0```.
    """
    tilepos_yx_nd2 = np.zeros_like(xy_pos, dtype=int)
    tilepos_yx_npy = np.zeros_like(xy_pos, dtype=int)
    if np.shape(xy_pos)[0] != 1:
        # say y coordinate changes when successive tiles have pixel separation of more than tile_sz/2
        change_y_coord = np.abs(np.ediff1d(xy_pos[:, 1])) > tile_sz / 2
        if False in change_y_coord:
            # sometimes get faulty first xy_pos
            # know that if there are more than one y coordinates, then the first
            # and second tile must have the same y coordinate.
            change_y_coord[0] = False
        ny = sum(change_y_coord) + 1
        nx = np.shape(xy_pos)[0] / ny
        if round(nx) != nx:
            raise ValueError('nx is not an integer')
        tilepos_yx_nd2[:, 0] = np.arange(ny).repeat(nx)
        tilepos_yx_nd2[:, 1] = np.tile(np.concatenate((np.arange(nx), np.flip(np.arange(nx)))),
                                       np.ceil(ny / 2).astype(int))[:np.shape(xy_pos)[0]]
        tilepos_yx_npy[:, 0] = np.flip(np.arange(ny).repeat(nx))
        tilepos_yx_npy[:, 1] = np.tile(np.concatenate((np.flip(np.arange(nx)), np.flip(np.arange(nx)))),
                                        np.ceil(ny / 2).astype(int))[:np.shape(xy_pos)[0]]
    return tilepos_yx_nd2, tilepos_yx_npy