Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Yu-AnChen
Last active March 10, 2021 00:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Yu-AnChen/f9030c9d6425cb293846a729d1b224ed to your computer and use it in GitHub Desktop.
Save Yu-AnChen/f9030c9d6425cb293846a729d1b224ed to your computer and use it in GitHub Desktop.

Exemplar - run ASHLAR on demo data with the batch script

  1. Download and unzip FIJI, move the FiJji.app folder to C:\Users\Public\Downloads\

  2. Install BaSiC by following the instructions under the Install via the Fiji Updater section on this page

  3. Save the imagej_basic_ashlar.py to C:\Users\Public\Downloads\Fiji.app\plugins\

  4. Launch command prompt

  5. In command prompt, change directory to the demo data folder

    d:

    cd d:\ashlar-batch-csv-demo\demo_data-20181001_Jerry

  6. In command prompt, list all folderpaths in the current directory and write it to folderslist.txt

    dir /s /b /o:n /a:d > folderslist.txt

  7. Create a csv file with three required headings - Directory, Correction, Pyramid

  8. Enter the paths for the slide directories, each contains multiple cycles of rcpnl files - you can copy the list from the folderslist.txt you just created

  9. Declare whether you want Correction and/or Pyramid for your images

    1, yes, y, true or t means yes (case insensitive), others are no

  10. Save the csv file, it's 20181001-demo.csv for this demo

  11. Make sure you have the run_ashlar_csv_batch.py file in the current directory.

  12. In command prompt, call python to run the script and take in the csv file as config

    python run_ashlar_csv_batch.py 20181001-demo.csv

  13. When the task is done, the ome-tif (if you choose to generate pyramid images) will be in the directories you specified in the csv file, the filenames are the folders' names. the ffps and dfps (if you turn on correction) will also be in the same folders.

from __future__ import print_function
import csv
from subprocess import call, check_output
try:
import pathlib
except ImportError:
import pathlib2 as pathlib
import argparse
import os
import datetime
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.getctime(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'
)
parser.add_argument(
'--sort-by-filename', dest='sort_by_name', default=False, action='store_true',
help='sort files by filename; default is sorting files by created date'
)
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]
# Check the raw_files subfolder for either rcpnl or xdce files
for exp in exps[args.from_dir : args.to_dir]:
path_exp = pathlib.Path(exp['Directory'])
raw_dir = path_exp / 'raw_images'
files_exp = sorted(raw_dir.glob('*rcpnl'))
file_type = 'rcpnl'
if len(files_exp) == 0:
files_exp = sorted(raw_dir.rglob('*xdce'))
file_type = 'xdce'
if len(files_exp) == 0:
print('No rcpnl or xdce files found in', str(raw_dir))
continue
sorting_method = 'filenames'
if not args.sort_by_name:
files_exp.sort(key=path_to_date)
sorting_method = 'created dates'
print('Processing files in', str(raw_dir))
print('Files are sorted by {}'.format(sorting_method))
print(datetime.datetime.now())
print()
if text_to_bool(exp['Correction']):
lambda_flat = '0.1'
lambda_dark = '0.01'
ffp_list = []
dfp_list = []
for j in files_exp:
print('\r ' + 'Generating ffp and dfp for ' + j.name)
ffp_file_name = j.name.replace('.' + file_type, '-ffp.tif')
dfp_file_name = j.name.replace('.' + file_type, '-dfp.tif')
illumination_dir = path_exp / 'illumination_profiles'
if (path_exp / 'illumination_profiles' /ffp_file_name).exists() and (path_exp / 'illumination_profiles' / dfp_file_name).exists():
print('\r ' + ffp_file_name + ' already exists')
print('\r ' + dfp_file_name + ' already exists')
else:
if not illumination_dir.exists():
illumination_dir.mkdir()
call("C:/Users/Public/Downloads/Fiji.app/ImageJ-win64.exe --ij2 --headless --run C:/Users/Public/Downloads/Fiji.app/plugins/imagej_basic_ashlar.py \"filename='%s', output_dir='%s', experiment_name='%s', lambda_flat=%s, lambda_dark=%s\"" %(str(j), str(illumination_dir), j.name.replace('.'+file_type, ''), lambda_flat, lambda_dark))
print('\r ' + ffp_file_name + ' generated')
print('\r ' + dfp_file_name + ' generated')
ffp_list.append(str(illumination_dir / ffp_file_name))
dfp_list.append(str(illumination_dir / dfp_file_name))
print('Run ashlar')
print(datetime.datetime.now())
print()
out_dir = path_exp / 'registration'
if not out_dir.exists():
out_dir.mkdir()
input_files = ' '.join([str(f) for f in files_exp])
command = 'ashlar ' + input_files + ' -m 30 -o ' + str(out_dir)
if text_to_bool(exp['Pyramid']):
command += ' --pyramid -f ' + path_exp.name + '.ome.tif'
if text_to_bool(exp['Correction']):
ffps = ' '.join(ffp_list)
dfps = ' '.join(dfp_list)
command += ' --ffp ' + ffps + ' --dfp ' + dfps
with open(str(out_dir / path_exp.name) + '-ashlar_command.log', 'a+') as f:
f.write('\n=== {} ===\n'.format(str(datetime.datetime.now())))
f.write('ashlar version:\n')
f.write(check_output('ashlar --version').decode("utf-8"))
f.write('ashlar command:\n')
f.write(command)
call(command)
@Yu-AnChen
Copy link
Author

Yu-AnChen commented Sep 24, 2018

List folders and subfolders path and write to f.txt (Windows command prompt)
dir /s /b /o:n /a:d > f.txt
source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment