Skip to content

Instantly share code, notes, and snippets.

@hirokai
Last active August 29, 2015 14:15
Show Gist options
  • Save hirokai/b05995fc4e88bf29b4a4 to your computer and use it in GitHub Desktop.
Save hirokai/b05995fc4e88bf29b4a4 to your computer and use it in GitHub Desktop.
Line scan
# Fiji Jython macro
# Dependencies
# (1) Microjson: https://github.com/phensley/microjson
# Copy is here: https://gist.github.com/hirokai/2fc4353cf6b28e5d2aac
# (2) hiroutil.py: https://gist.github.com/hirokai/77779cdade6d33d0702b
from ij import IJ
from ij.gui import Line, ProfilePlot
import csv
import os
# http://fiji.sc/Jython_Scripting#Importing_other_.py_scripts_.28modules.29
from sys import path
from java.lang.System import getProperty
# extend the search path by $FIJI_ROOT/scripts/
path.append(getProperty('fiji.dir') + '/scripts')
import microjson
from hiroutil import *
# base = '/Volumes/Groves/Scope 7/'
base = '/Volumes/MacintoshHD/Google Drive/Groves/Scope 7/'
tsv_path = base + '20150214 LAT-EGFP T cells on PLL-PEG-biotin pattern TCR labeled/process/20150214 LAT-EGFP T cells on PLL-PEG-biotin - Line scan of images.tsv'
out_path_base = base + '20150214 LAT-EGFP T cells on PLL-PEG-biotin pattern TCR labeled/process/linescan/20150214 linescan'
def parse_row(row):
vs = row[0:2]+map(mayint,row[2:13])
if len(row) > 13:
xys = filter(lambda v: v is not None, map(mayint,row[13:]))
vs = vs + ([xys] if len(xys) > 0 else [None])
else:
vs = vs + [None]
return vs
datasets = map(parse_row, load_tsv(tsv_path))
def process(name, folder, ch, frm, to, interval, w, h, x, y, x1,y1,x2,y2,xys):
frames = to - frm + 1 # Or custom number of frames.
in_path = '%s20150214 LAT-EGFP T cells on PLL-PEG-biotin pattern TCR labeled/%s'%(base,folder)
imp = read_sequence(in_path)
take_slices(imp,range(3-ch,1000,2)[frm-1:to])
remove_scale(imp)
crop(imp,w,h,x,y)
IJ.run(imp,"Green" if ch == 1 else "Red","");
imp.setTitle(folder + '_' + ("Green" if ch == 1 else "Red"))
if xys:
from ij.gui import Roi, PolygonRoi
xs = [x1,x2]+xys[0::2]
ys = [y1,y2]+xys[1::2]
else:
xs = [x1,x2]
ys = [y1,y2]
vs = transpose(map_slices(linescan_poly(xs,ys),imp))
out_path = out_path_base + '_' + name + '_' + ("Green" if ch == 1 else "Red") + '.tsv'
out_path_condition = out_path_base + '_' + name + '_' + ("Green" if ch == 1 else "Red") + '_condition.tsv'
write_tsv(out_path, vs)
metadata_path = os.path.join(in_path,'metadata.txt')
f = open(metadata_path,'r')
json_data = f.read()
f.close()
obj = microjson.from_json(json_data)
uuid = obj['Summary']['UUID']
write_tsv(out_path_condition,
[['Image folder', 'UUID', 'Name', 'Subfolder',
'ch', 'Frame start', 'Frame end', 'Crop X', 'Crop Y', 'Crop W', 'Crop H',
'Line x1', 'Line y1', 'Line x2', 'Line y2'],
[in_path, uuid, name, folder, ch, frm, to, x, y, w, h, x1,y1,x2,y2]])
count = 1
prev_args = None
def do_set(fc, folder, x, y, w, h, frm,to,interval,x1,y1,x2,y2,xys):
global count, prev_args
print(x)
if x is None:
fc, folder, x, y, w, h = prev_args
else:
prev_args = fc, folder, x, y, w, h
name = folder.replace('/','_') + str(count)
count += 1
process(name, folder, 1,frm,to,interval,w,h,x,y,x1,y1,x2,y2,xys)
process(name, folder, 2,frm,to,interval,w,h,x,y,x1,y1,x2,y2,xys)
for ds in datasets:
print(ds)
do_set(*ds)
print('Done.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment