Last active
February 25, 2019 15:16
-
-
Save Yu-AnChen/5000a28d59721278a5edff92dd064bdd to your computer and use it in GitHub Desktop.
Take a csv config file and make thumbnail for all the .rcpnl files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import csv | |
from subprocess import call | |
try: | |
import pathlib | |
except ImportError: | |
import pathlib2 as pathlib | |
import argparse | |
import os | |
from ashlar import reg, thumbnail | |
import csv | |
def text_to_bool(text): | |
return False \ | |
if not str(text) \ | |
else str(text).lower() in '1,yes,y,true,t' | |
def path_to_date(path): | |
return os.path.getmtime(str(path)) | |
parser = argparse.ArgumentParser( | |
description='Read a csv config file and run ashlar' | |
) | |
parser.add_argument( | |
'csv_filepath', metavar='CSVFILE', | |
help='a csv file with header row: Directory, Correction, Pyramid' | |
) | |
parser.add_argument( | |
'-f', '--from-dir', dest='from_dir', default=0, type=int, metavar='FROM', | |
help=('starting directory; numbering starts at 0') | |
) | |
parser.add_argument( | |
'-t', '--to-dir', dest='to_dir', default=None, type=int, metavar='TO', | |
help=('ending directory; numbering starts at 0') | |
) | |
args = parser.parse_args() | |
csv_path = pathlib.Path(args.csv_filepath) | |
if not csv_path.is_file() or csv_path.suffix != '.csv': | |
print('A csv file is required to proceed.') | |
parser.print_usage() | |
parser.exit() | |
with open(str(csv_path)) as exp_config: | |
exp_config_reader = csv.DictReader(exp_config) | |
exps = [dict(row) for row in exp_config_reader] | |
for exp in exps[args.from_dir : args.to_dir]: | |
path_exp = pathlib.Path(exp['Directory']) | |
files_exp = sorted(path_exp.glob('*rcpnl')) | |
files_exp.sort(key=path_to_date) | |
if len(files_exp) == 0: | |
print('No rcpnl files found in', str(path_exp)) | |
continue | |
print('Processing files in', str(path_exp)) | |
rcpnl_files = [f for f in files_exp] | |
ref_reader = reg.BioformatsReader(str(rcpnl_files[0])) | |
report_file_path = pathlib.Path(rcpnl_files[0].name + '-loading_shift.csv') | |
if not report_file_path.exists(): | |
with open(str(report_file_path), 'wb') as f: | |
writer = csv.writer(f, delimiter=',') | |
writer.writerow([rcpnl_files[0].name, '0.0', '0.0']) | |
loading_offsets = {} | |
else: | |
with open(str(report_file_path)) as report_file: | |
report_file_reader = csv.DictReader( | |
report_file, | |
fieldnames=['file_name', 'y_offset', 'x_offset'] | |
) | |
loading_offsets = [dict(row) for row in report_file_reader] | |
for rcpnl_file in rcpnl_files[1:]: | |
if rcpnl_file.name in [i['file_name'] for i in loading_offsets]: | |
continue | |
if not hasattr(ref_reader, 'thumbnail_img'): | |
ref_reader.thumbnail_img = thumbnail.thumbnail( | |
ref_reader, channel=0 | |
) | |
cycle_reader = reg.BioformatsReader(str(rcpnl_file)) | |
cycle_offset = thumbnail.calculate_cycle_offset( | |
ref_reader, cycle_reader, | |
save=(False, True) | |
) | |
with open(str(report_file_path), 'ab') as f: | |
writer = csv.writer(f, delimiter=',') | |
writer.writerow([rcpnl_file.name] + [str(o) for o in cycle_offset]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment