Skip to content

Commit

Permalink
Ensure extraction_dir is absolutely converted to a Path type
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-l-kong committed Nov 26, 2024
1 parent 31de1a2 commit 873fed5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/maldi_tools/extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,19 @@ 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)
intensity: np.ndarray = intensities[np.isin(mzs, peak_df["m/z"])]

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)

Expand Down
8 changes: 4 additions & 4 deletions src/maldi_tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down

0 comments on commit 873fed5

Please sign in to comment.