forked from RhalenaThomas/jython-macros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IPSCMeasure.py
108 lines (69 loc) · 3.08 KB
/
IPSCMeasure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'''
CellQ: Evos Cell Quantification - ImageJ Macro written in Python
Input - A main directory containing Folders containing split channel images from the EVOS Cell Imaging System
- The filenames of the images must be in it's original format, starting with scan_Plate_R and ending with d0, d1, etc.
- Optional thresholding and naming convention file can be loaded as well
- The scans in each subfolder must have the same markers
Output - CSV file containing nuclei counts and marker colocalization data for each image
Written by: Eddie Cai & Rhalena A. Thomas
'''
import os, sys, csv
from ij import IJ
from ij.io import DirectoryChooser
from ij.measure import ResultsTable
from ij.measure import Measurements
from ij.process import ImageProcessor
from ij.process import ImageConverter
from ij.plugin.frame import RoiManager
from ij.plugin.filter import ParticleAnalyzer
from ij.gui import WaitForUserDialog
# To enable displayImages mode (such as for testing thresholds), make displayImages = True
displayImages = False
outputname = "output"
t1 = 20
############# Main loop, will run for every image. ##############
def process(inputpath, outputpath):
imp = IJ.openImage(inputpath)
IJ.run(imp, "Properties...", "channels=1 slices=1 frames=1 unit=um pixel_width=0.8777017 pixel_height=0.8777017 voxel_depth=25400.0508001")
IJ.setThreshold(imp, t1, 255)
#imp.show()
#WaitForUserDialog("Title", "Look at image").show()
IJ.run(imp, "Convert to Mask", "")
IJ.run(imp, "Watershed", "")
# Counts and measures the area of particles and adds them to a table called areas. Also adds them to the ROI manager
table = ResultsTable()
roim = RoiManager(True)
ParticleAnalyzer.setRoiManager(roim);
pa = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER, Measurements.AREA, table, 50, 9999999999999999, 0.2, 1.0)
pa.setHideOutputImage(True)
pa.analyze(imp)
imp.changes = False
imp.close()
areas = table.getColumn(0)
summary = {}
if areas:
summary['Image'] = filename
summary['Nuclei.count'] = len(areas)
summary['Area.Covered'] = sum(areas)
fieldnames = list(summary.keys())
with open(outputpath, 'a') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, extrasaction='ignore', lineterminator = '\n')
if os.path.getsize(outputDirectory + "/" + outputname +".csv") < 1:
writer.writeheader()
writer.writerow(summary)
########################## code begins running here ##############################
# Get input and output directories
dc = DirectoryChooser("Choose an input directory")
inputDirectory = dc.getDirectory()
dc = DirectoryChooser("Choose an output directory")
outputDirectory = dc.getDirectory()
open(outputDirectory + "/" + outputname +".csv", 'w').close
for filename in os.listdir(inputDirectory):
if "d0.TIF" in filename:
process(inputDirectory + '/' + filename, outputDirectory + "/" + outputname +".csv")
cat = """
\ /\ Macro completed!
) ( ') meow!
( / )
\(__)|"""
print(cat)