Skip to content

infer_subc/utils

functions which help with processing batches of files

explode_mask(mask_path, postfix='masks', im_type='.tiff')

Split a 3 channel 'masks' file into 'nuc', 'cell', and 'cyto' images. WARNING: requires the channels to be nuc = 0, cell = 1, and cyto = 2

add logging instead of printing

append tiffcomments with provenance

Source code in infer_subc/utils/batch.py
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
def explode_mask(mask_path: Union[Path,str], postfix: str= "masks", im_type: str = ".tiff") -> bool:
    """ 
    Split a 3 channel 'masks' file into 'nuc', 'cell', and 'cyto' images.  WARNING: requires the channels to be nuc = 0, cell = 1, and cyto = 2
    TODO: add logging instead of printing
        append tiffcomments with provenance
    """
    if isinstance(mask_path, str): mask_path = Path(mask_path)
    # load image 
    full_stem = mask_path.stem
    if full_stem.endswith(postfix):
        stem = full_stem.rstrip(postfix)
        image = read_tiff_image(mask_path)
        assert image.shape[0]==3

        # make into np.uint16 labels
        nuclei = label_uint16(image[0])
        # export as np.uint8 (255)
        cellmask = image[1]>0            
        cytoplasm = image[2]>0

        # write wasks
        root_stem = mask_path.parent / stem
        # ret1 = imwrite(f"{root}nuclei{stem}", nuclei)
        ret1 = export_tiff(nuclei, f"{stem}nuc", mask_path.parent, None)
        # ret2 = imwrite(f"{root}cellmask{stem}", cellmask)
        ret2 = export_tiff(cellmask, f"{stem}cell", mask_path.parent, None)
        # ret3 = imwrite(f"{root}cytosol{stem}", cytosol)
        ret3 = export_tiff(cytoplasm, f"{stem}cyto", mask_path.parent, None)

        # print(f"wrote {stem}-{{nuclei,cellmask,cyto}}")
        return True
    else:
        return False

explode_masks(root_path, postfix='masks', im_type='.tiff')

add loggin instead of printing

append tiffcomments with provenance

Source code in infer_subc/utils/batch.py
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def explode_masks(root_path: Union[Path,str], postfix: str= "masks", im_type: str = ".tiff"):
    """  
    TODO: add loggin instead of printing
        append tiffcomments with provenance
    """
    if isinstance(root_path, str): root_path = Path(root_path)

    img_file_list = list_image_files(root_path,im_type, postfix)
    wrote_cnt = 0
    for img_f in img_file_list:
        if explode_mask(img_f, postfix=postfix, im_type=im_type): 
            wrote_cnt += 1
        else: 
            print(f"failed to explode {img_f}")
    # else:
    #     print(f"how thefark!!! {img_f}")


    print(f"exploded {wrote_cnt*100./len(img_file_list)} pct of {len(img_file_list)} files")
    return wrote_cnt

find_segmentation_tiff_files(prototype, organelles, int_path)

find the nescessary image files based on protype, the organelles involved, and paths

Source code in infer_subc/utils/batch.py
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
def find_segmentation_tiff_files(prototype:Union[Path,str], organelles: List[str], int_path: Union[Path,str]) -> Dict:
    """
    find the nescessary image files based on protype, the organelles involved, and paths
    """

    # raw
    prototype = Path(prototype)
    if not prototype.exists():
        print(f"bad prototype. please choose an existing `raw` file as prototype")
        return dict()
    # make sure protoype ends with czi

    out_files = {"raw":prototype}

    int_path = Path(int_path) 
    # raw
    if not int_path.is_dir():
        print(f"bad path argument. please choose an existing path containing organelle segmentations")
        return out_files

    # cyto, cellmask
    cyto_nm = int_path / f"{prototype.stem}-cyto.tiff"
    if cyto_nm.exists():
        out_files["cyto"] = cyto_nm
    else:
        print(f"cytosol mask not found.  We'll try to extract from masks ")
        if explode_mask(int_path / f"{prototype.stem}-masks.tiff"): 
            out_files["cyto"] = cyto_nm
        else: 
            print(f"failed to explode {prototype.stem}-masks.tiff")
            return out_files

    cellmask_nm = int_path / f"{prototype.stem}-cell.tiff"
    if  cellmask_nm.exists():
        out_files["cell"] = cellmask_nm
    else:
        print(f"cellmask file not found in {int_path} returning")
        out_files["cell"] = None

    # organelles
    for org_n in organelles:
        org_name = Path(int_path) / f"{prototype.stem}-{org_n}.tiff"
        if org_name.exists(): 
            out_files[org_n] = org_name
        else: 
            print(f"{org_n} .tiff file not found in {int_path} returning")
            out_files[org_n] = None

    if "nuc" not in organelles:
        nuc_nm = int_path / f"{prototype.stem}-nuc.tiff"
        if  nuc_nm.exists():
            out_files["nuc"] = nuc_nm
        else:
            print(f"nuc file not found in {int_path} returning")
            out_files["nuc"] = None



    return out_files

fixed_infer_organelles(img_data)

wrapper to infer all organelles from a single multi-channel image

Source code in infer_subc/utils/batch.py
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
def fixed_infer_organelles(img_data):
    """
    wrapper to infer all organelles from a single multi-channel image
    """
    # ch_to_agg = (LYSO_CH, MITO_CH, GOLGI_CH, PEROX_CH, ER_CH, LD_CH)

    # nuc_ch = NUC_CH
    # optimal_Z = find_optimal_Z(img_data, nuc_ch, ch_to_agg)
    # optimal_Z = fixed_find_optimal_Z(img_data)
    # # Stage 1:  nuclei, cellmask, cytoplasm
    # img_2D = fixed_get_optimal_Z_image(img_data)

    cellmask = fixed_infer_cellmask_fromaggr(img_data)

    nuclei_object = fixed_infer_nuclei(img_data, cellmask)

    cytoplasm_mask = infer_cytoplasm(nuclei_object, cellmask)

    # cyto masked objects.
    lyso_object = fixed_infer_lyso(img_data, cytoplasm_mask)
    mito_object = fixed_infer_mito(img_data, cytoplasm_mask)
    golgi_object = fixed_infer_golgi(img_data, cytoplasm_mask)
    peroxi_object = fixed_infer_perox(img_data, cytoplasm_mask)
    er_object = fixed_infer_ER(img_data, cytoplasm_mask)
    LD_object = fixed_infer_LD(img_data, cytoplasm_mask)

    img_layers = [
        nuclei_object,
        lyso_object,
        mito_object,
        golgi_object,
        peroxi_object,
        er_object,
        LD_object,
        cellmask,
        cytoplasm_mask,
    ]

    layer_names = [
        "nuclei",
        "lyso",
        "mitochondria",
        "golgi",
        "peroxisome",
        "er",
        "LD_body",
        "cell",
        "cytoplasm_mask",
    ]
    # TODO: pack outputs into something napari readable
    img_out = np.stack(img_layers, axis=0)
    return (img_out, layer_names)

process_czi_image(czi_file_name, data_root_path)

wrapper for processing

Source code in infer_subc/utils/batch.py
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
def process_czi_image(czi_file_name, data_root_path):
    """wrapper for processing"""

    img_data, meta_dict = read_czi_image(czi_file_name)
    # # get some top-level info about the RAW data
    # channel_names = meta_dict['name']
    # img = meta_dict['metadata']['aicsimage']
    # scale = meta_dict['scale']
    # channel_axis = meta_dict['channel_axis']

    inferred_organelles, layer_names, optimal_Z = fixed_infer_organelles(img_data)
    out_file_n = export_inferred_organelle(inferred_organelles, layer_names, meta_dict, data_root_path)

    ## TODO:  collect stats...

    return out_file_n

stack_organelle_layers(*layers)

wrapper to stack the inferred objects into a single numpy.ndimage

Source code in infer_subc/utils/batch.py
279
280
281
282
def stack_organelle_layers(*layers) -> np.ndarray:
    """wrapper to stack the inferred objects into a single numpy.ndimage"""

    return np.stack(layers, axis=0)

stack_organelle_objects(cellmask, nuclei_object, cytoplasm_mask, lyso_object, mito_object, golgi_object, peroxi_object, er_object, LD_object)

wrapper to stack the inferred objects into a single numpy.ndimage

Source code in infer_subc/utils/batch.py
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
def stack_organelle_objects(
    cellmask,
    nuclei_object,
    cytoplasm_mask,
    lyso_object,
    mito_object,
    golgi_object,
    peroxi_object,
    er_object,
    LD_object,
) -> np.ndarray:
    """wrapper to stack the inferred objects into a single numpy.ndimage"""
    img_layers = [
        cellmask,
        nuclei_object,
        cytoplasm_mask,
        lyso_object,
        mito_object,
        golgi_object,
        peroxi_object,
        er_object,
        LD_object,
    ]
    return np.stack(img_layers, axis=0)