Skip to content

Instantly share code, notes, and snippets.

@ajitjohnson
Forked from Yu-AnChen/exemplar.md
Created June 18, 2019 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajitjohnson/82450dff06e6d4445166e361988e88f9 to your computer and use it in GitHub Desktop.
Save ajitjohnson/82450dff06e6d4445166e361988e88f9 to your computer and use it in GitHub Desktop.

Exemplar - run ASHLAR on demo data with the batch script

  1. Download FIJI and put it in C:\Users\Public\Downloads\

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

  3. Launch command prompt

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

    d:

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

  5. 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

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

  7. 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

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

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

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

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

  11. 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

  12. 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
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.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('**/**/*xdce'))
files_exp.sort(key=path_to_date)
if len(files_exp) == 0:
print('No xdce files found in', str(path_exp))
continue
print('Processing files in', str(path_exp))
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('Run BaSiC')
print('\r ' + 'Generating ffp and dfp for ' + j.name)
ffp_file_name = j.name.replace('.xdce', '-ffp.tif')
dfp_file_name = j.name.replace('.xdce', '-dfp.tif')
if (j.parent / ffp_file_name).exists() and (j.parent / dfp_file_name).exists():
print('\r ' + ffp_file_name + ' already exists')
print('\r ' + dfp_file_name + ' already exists')
print('already exists')
else:
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(j.parent), j.name.replace('.xdce', ''), lambda_flat, lambda_dark))
print('\r ' + ffp_file_name + ' generated')
print('\r ' + dfp_file_name + ' generated')
pass
ffp_list.append(str(j.parent / ffp_file_name))
dfp_list.append(str(j.parent / dfp_file_name))
print('Run ashlar')
print(datetime.datetime.now())
print()
out_dir = path_exp / 'registration'
if not out_dir.exists():
out_dir.mkdir()
xdce_files = ' '.join([str(f) for f in files_exp])
command = 'ashlar ' + xdce_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
print(command)
call(command)
from __future__ import print_function
import csv
from subprocess import call
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.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))
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('.rcpnl', '-ffp.tif')
dfp_file_name = j.name.replace('.rcpnl', '-dfp.tif')
if (path_exp / ffp_file_name).exists() and (path_exp / dfp_file_name).exists():
print('\r ' + ffp_file_name + ' already exists')
print('\r ' + dfp_file_name + ' already exists')
else:
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(path_exp), j.name.replace('.rcpnl', ''), lambda_flat, lambda_dark))
print('\r ' + ffp_file_name + ' generated')
print('\r ' + dfp_file_name + ' generated')
ffp_list.append(str(path_exp / ffp_file_name))
dfp_list.append(str(path_exp / dfp_file_name))
print('Run ashlar')
print(datetime.datetime.now())
print()
out_dir = path_exp / 'registration'
if not out_dir.exists():
out_dir.mkdir()
rcpnl_files = ' '.join([str(f) for f in files_exp])
command = 'ashlar ' + rcpnl_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
# print(command)
call(command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment