Skip to content

Commit 8e2bace

Browse files
committed
Added format flag for denoised output
1 parent 1cf4e6e commit 8e2bace

2 files changed

Lines changed: 22 additions & 13 deletions

File tree

partinet/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ def star(labels, images, output,conf):
8282
@click.option("--source", type=str, required=True, help="Path to Raw micrographs")
8383
@click.option('--project', type=str, required=True, help='save denoised micrographs to project/denoised', show_default=True)
8484
@click.option('--num_workers', type=int, default=None, help='Number of workers for denoising micrographs')
85-
def denoise(source, project, num_workers):
85+
@click.option('--format', type=str, type=click.Choice(["png", "jpg", "mrc"], case_sensitive=True) default="png",show_default=True, help='Output format of denoised micrographs')
86+
def denoise(source, project, num_workers,format):
8687
click.echo("Denoising micrographs...")
8788
import partinet.process_utils.pooled_denoise_proc
88-
partinet.process_utils.pooled_denoise_proc.main(source,project,num_workers)
89+
partinet.process_utils.pooled_denoise_proc.main(source,project,num_workers,format)
8990

9091

9192
@main.group()

partinet/process_utils/pooled_denoise_proc.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,31 @@
88
import gc
99
from concurrent.futures import ProcessPoolExecutor
1010
from typing import List, Tuple
11+
import mrcfile
1112

1213
# Function to perform CLAHE-based denoising
1314

14-
def clahe_denoise(args: Tuple[str, str]) -> None:
15+
def clahe_denoise(args: Tuple[str, str, str]) -> None:
1516
"""
1617
Applies the guided denoising algorithm to an input image and saves the denoised result.
1718
1819
Args:
1920
args (Tuple[str, str]):
2021
- src_path: Path to the source micrograph file.
2122
- dest_path: Path to save the denoised image.
23+
- format: output format of denoised images
2224
2325
Raises:
2426
Exception: Logs any exceptions that occur during processing.
2527
"""
2628
try:
27-
src_path, dest_path = args
29+
src_path, dest_path, format = args
2830
# Perform denoising
2931
denoised = denoise(src_path)
30-
# Save the denoised image
31-
cv2.imwrite(dest_path, denoised)
32+
if format == "mrc":
33+
mrcfile.write(dest_path,data=denoised)
34+
else:
35+
cv2.imwrite(dest_path, denoised)
3236
logging.info(f"Processed image {src_path} to dest. {dest_path}")
3337
del denoised
3438
gc.collect()
@@ -37,14 +41,15 @@ def clahe_denoise(args: Tuple[str, str]) -> None:
3741

3842
# Function to process all files in a directory
3943

40-
def process_directory(micrographs_dir: str, clahe_denoised_dir: str, max_workers: int) -> None:
44+
def process_directory(micrographs_dir: str, clahe_denoised_dir: str, max_workers: int, format: str) -> None:
4145
"""
4246
Processes all `.mrc` files in the given directory using parallel workers for denoising.
4347
4448
Args:
4549
micrographs_dir (str): Path to the directory containing raw micrograph files.
4650
clahe_denoised_dir (str): Path to the directory where denoised images will be saved.
4751
max_workers (int): Number of worker processes to use for parallel processing.
52+
format (str): Output format of denoised images
4853
4954
Notes:
5055
- Only `.mrc` files are processed.
@@ -53,16 +58,16 @@ def process_directory(micrographs_dir: str, clahe_denoised_dir: str, max_workers
5358
os.makedirs(clahe_denoised_dir, exist_ok=True)
5459
logging.info(f"Directory ready: {clahe_denoised_dir}")
5560

56-
tasks: List[Tuple[str, str]] = []
61+
tasks: List[Tuple[str, str, str]] = []
5762
# Iterate through files in the directory
5863
for file_name in os.listdir(micrographs_dir):
5964
if file_name.endswith(".mrc"):
6065
src_path = os.path.join(micrographs_dir, file_name)
61-
dest_path = os.path.join(clahe_denoised_dir, file_name.replace(".mrc", ".png"))
66+
dest_path = os.path.join(clahe_denoised_dir, file_name.replace(".mrc", "."+format))
6267
if os.path.exists(dest_path):
6368
logging.info(f"{dest_path} already exists!")
6469
else:
65-
tasks.append((src_path, dest_path))
70+
tasks.append((src_path, dest_path, format))
6671

6772
# Parallel processing of tasks
6873
with ProcessPoolExecutor(max_workers=max_workers) as executor:
@@ -73,14 +78,15 @@ def process_directory(micrographs_dir: str, clahe_denoised_dir: str, max_workers
7378

7479
# Main function
7580

76-
def main(source_dir: str, project_dir: str, ncpu: int) -> None:
81+
def main(source_dir: str, project_dir: str, ncpu: int, format: str) -> None:
7782
"""
7883
Main function to set up logging, determine available CPUs, and start the denoising process.
7984
8085
Args:
8186
source_dir (str): Path to the directory containing raw micrographs.
8287
project_dir (str): Path to the project directory where denoised images will be saved.
8388
ncpu (int): Number of CPUs to use for parallel processing. Defaults to half the available CPUs.
89+
format (str): Output format of denoised images
8490
8591
Notes:
8692
- Logging is configured to write messages to a log file and the console.
@@ -110,7 +116,7 @@ def main(source_dir: str, project_dir: str, ncpu: int) -> None:
110116
logging.info(f"Saving denoised micrographs in {denoise_dir}")
111117

112118
# Process the directory
113-
process_directory(source_dir, denoise_dir, ncpu)
119+
process_directory(source_dir, denoise_dir, ncpu, format)
114120

115121
# Command-line argument parsing
116122

@@ -123,15 +129,17 @@ def parse_args() -> argparse.Namespace:
123129
- raw: Path to raw micrographs.
124130
- project: Path to the project directory.
125131
- ncpu: Number of CPUs to use.
132+
- format: output format of denoised images
126133
"""
127134
parser = argparse.ArgumentParser(description="Denoise micrographs with guided CryoSegNet-style filter")
128135
parser.add_argument("--raw", required=True, help="Path to raw micrographs")
129136
parser.add_argument("--project", required=True, help="Denoised micrographs saved in project/denoised")
130137
parser.add_argument('--ncpu', type=int, default=None, help='Number of CPUs to use')
138+
parser.add_argument('--format', type=str, choices=["png","jpg","mrc"], default="png", help='Output format of denoised images')
131139
return parser.parse_args()
132140

133141
# Entry point for the script
134142

135143
if __name__ == "__main__":
136144
args = parse_args()
137-
main(args.raw, args.project, args.ncpu)
145+
main(args.raw, args.project, args.ncpu, args.format)

0 commit comments

Comments
 (0)