diff --git a/src/napari_mm3/_compile.py b/src/napari_mm3/_compile.py index b155697..55b4bfc 100644 --- a/src/napari_mm3/_compile.py +++ b/src/napari_mm3/_compile.py @@ -66,42 +66,42 @@ def get_time(filepath: str) -> Union[np.int_, None]: return None -def restack_planes(found_files_c1: list, found_files_c2: list, params: dict) -> None: +def stack_channels(found_files: np.ndarray, params: dict) -> None: """Check if phase and fluorescence channels are separate tiffs. If so, restack them as one tiff file and save out to TIFF_dir. - Move the original files to a new directory TIFF_unstacked. + Move the original files to a new directory original_TIFF. Parameters --------- - found_files_c1: list of files for 1st imaging plane - found_files_c1: list of files for 2nd imaging plane + found_files: ndarray of filepaths for each imaging plane + params: dictionary of parameters Returns --------- None """ - found_files_c1 = sorted(found_files_c1) - found_files_c2 = sorted(found_files_c2) + found_files = np.transpose(found_files) - for f1, f2 in zip(found_files_c1, found_files_c2): - information("Merging images " + str(f1) + " and " + str(f2)) - im1 = tiff.imread(f1) - im2 = tiff.imread(f2) - im_out = np.stack((im1, im2), axis=0) + for files in found_files: + information('Merging files') + print(*files, sep='\n') + ims = [tiff.imread(f) for f in files] + im_out = np.stack(ims, axis=0) # need to insert regex here to catch variants - name_out = re.sub("c*01", "", re.IGNORECASE) + name_out = re.sub("c\d+", "", str(files[0]), flags = re.IGNORECASE) # 'minisblack' necessary to ensure that it interprets image as black/white. tiff.imwrite(name_out, im_out, photometric="minisblack") - ## make a new directory rather than just deleting the old images - old_tiff_path = params["TIFF_dir"].parent / "TIFF_unstacked" + ## make a new directory for the old images + old_tiff_path = params["TIFF_dir"].parent / "original_TIFF" if not old_tiff_path.exists(): old_tiff_path.mkdir() information("Creating directory for original TIFFs") - f1.replace(str(f1).replace(str(params["TIFF_dir"]), "TIFF_unstacked")) - f2.replace(str(f2).replace(str(params["TIFF_dir"]), "TIFF_unstacked")) + + for f in files: + f.replace(str(f).replace(str(params["TIFF_dir"]), "original_TIFF")) return @@ -290,7 +290,7 @@ def get_tif_metadata_filename(tif: tiff.TiffFile) -> dict: "t": get_time(tif.filename), # time point "jd": -1 * 0.0, # absolute julian time "planes": get_plane(tif.filename), - } # y position on stage [um] + } return idata @@ -1283,20 +1283,29 @@ def compile(params: dict) -> None: ## need to stack phase and fl plane if not exported from .nd2 if params["TIFF_source"] == "BioFormats / other TIFF": - information("Checking if phase & fluorescence planes are separated") + information("Checking if imaging channels are separated") found_files = list(params["TIFF_dir"].glob("*.tif")) found_files = sorted(found_files) # sort by timepoint - string_c1 = re.compile("c(0)*1", re.IGNORECASE) - string_c2 = re.compile("c(0)*2", re.IGNORECASE) - - found_files_c1 = [f for f in found_files if re.search(string_c1, f.name)] - found_files_c2 = [f for f in found_files if re.search(string_c2, f.name)] + files_list = [] + i = 0 + while True: + c_string = re.compile(f"c(0)*{i}[._]", re.IGNORECASE) + matched_files = [f for f in found_files if re.search(c_string, f.name)] + if matched_files: + files_list.append(matched_files) + i+=1 + elif i == 0: + # continue in case channels indexed from 1 + i+=1 + else: + break - ## if there is a second plane, stack and save them out - if found_files_c2: - information("Restacking TIFFs") - restack_planes(found_files_c1, found_files_c2, params) + files_array = np.array(files_list).squeeze() + if files_array.ndim > 1: + information("Merging TIFFs across channel") + stack_channels(files_array, params) + else: pass @@ -1363,10 +1372,9 @@ def compile(params: dict) -> None: def load_fov(image_directory: Path, fov_id: int, filter_str: str = "") -> Union[np.ndarray, None]: information("getting files") found_files_paths = image_directory.glob("*.tif") - file_string = re.compile(f"xy{fov_id:02d}.*.tif", re.IGNORECASE) + file_string = re.compile(f"xy0*{fov_id}\w*.tif", re.IGNORECASE) found_files = [f.name for f in found_files_paths if re.search(file_string, f.name)] if filter_str: - # found_files = [f for f in found_files if filter_str in f] found_files = [ f for f in found_files if re.search(filter_str, f, re.IGNORECASE) ]