From 873fed59bf40c34cab7b13cae25bb06a09aafde7 Mon Sep 17 00:00:00 2001 From: Alex Kong Date: Tue, 26 Nov 2024 10:23:58 -0800 Subject: [PATCH] Ensure extraction_dir is absolutely converted to a Path type --- src/maldi_tools/extraction.py | 10 ++++++---- src/maldi_tools/plotting.py | 8 ++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/maldi_tools/extraction.py b/src/maldi_tools/extraction.py index e849f89..b5c426e 100644 --- a/src/maldi_tools/extraction.py +++ b/src/maldi_tools/extraction.py @@ -260,8 +260,10 @@ def coordinate_integration(peak_df: pd.DataFrame, imz_data: ImzMLParser, extract image_shape: Tuple[int, int] = (x_size, y_size) - os.makedirs(extraction_dir / "float") - os.makedirs(extraction_dir / "int") + float_peak_dir: Path = Path(extraction_dir) / "float" + int_peak_dir: Path = Path(extraction_dir) / "int" + os.makedirs(float_peak_dir) + os.makedirs(int_peak_dir) for idx, (x, y, _) in tqdm(enumerate(imz_data.coordinates), total=len(imz_data.coordinates)): mzs, intensities = imz_data.getspectrum(idx) @@ -269,8 +271,8 @@ def coordinate_integration(peak_df: pd.DataFrame, imz_data: ImzMLParser, extract for i_idx, peak in peak_df.loc[peak_df["m/z"].isin(mzs), "peak"].reset_index(drop=True).items(): img_name: str = f"{peak:.4f}".replace(".", "_") - float_peak_path: Path = extraction_dir / "float" / f"{img_name}.tiff" - int_peak_path: Path = extraction_dir / "int" / f"{img_name}.tiff" + float_peak_path: Path = float_peak_dir / f"{img_name}.tiff" + int_peak_path: Path = int_peak_dir / f"{img_name}.tiff" peak_exists: bool = os.path.exists(float_peak_path) peak_img: np.ndarray = imread(float_peak_path).T if peak_exists else np.zeros(image_shape) diff --git a/src/maldi_tools/plotting.py b/src/maldi_tools/plotting.py index 7dee2ec..07706ea 100644 --- a/src/maldi_tools/plotting.py +++ b/src/maldi_tools/plotting.py @@ -184,8 +184,8 @@ def save_matched_peak_images( extraction_dir (Path): The directory to save extracted data in. """ # Create image directories if they do not exist - float_dir: Path = extraction_dir / "library_matched" / "float" - int_dir: Path = extraction_dir / "library_matched" / "int" + float_dir: Path = Path(extraction_dir) / "library_matched" / "float" + int_dir: Path = Path(extraction_dir) / "library_matched" / "int" for img_dir in [float_dir, int_dir]: if not os.path.exists(img_dir): img_dir.mkdir(parents=True, exist_ok=True) @@ -196,8 +196,8 @@ def save_matched_peak_images( if row.matched is True: peak_file_name: str = f"{row.lib_mz:.4f}".replace(".", "_") + ".tiff" # load in the corresponding float and integer images - float_img: np.ndarray = io.imread(extraction_dir / "float" / peak_file_name) - integer_img: np.ndarray = io.imread(extraction_dir / "int" / peak_file_name) + float_img: np.ndarray = io.imread(Path(extraction_dir) / "float" / peak_file_name) + integer_img: np.ndarray = io.imread(Path(extraction_dir) / "int" / peak_file_name) img_name: str = row.composition