#Jeff Hardin, Dept. of Integrative Biology #Univ. of Wisconsin-Madison #jdhardin@wisc.edu #Additional colors (see also https://htmlcolorcodes.com/color-picker/) #Green = #32a852 #Purple = #7132a8 #Dark red = #9C2005 #Dark brown = #754F04 #Light brown = #9C6905 #This script imports data from one or more CSV files in the form x[i],y[i] as paired columns #using the ImageJ's built-in support for importing CSV files in a ResultsTable #and ImageJ's built-in Plot functions. #Currently uses the hard-wired colors built into ImageJ's Plot class #Could be expanded to a nicer color palette using hexadecimal colors. #Use the PlotWindow "More >> " button to change range, line thickness, colors, etc. #Ideally, one could open the CSV using a parser without opening a data window. #Using JFreeChart would also be great, since JFreeChart can export #as an editable SVG file. from org.jfree.chart import ChartFactory, ChartPanel, JFreeChart from org.jfree.chart.plot import PlotOrientation, XYPlot from org.jfree.data.xy import XYDataset, XYSeries, XYSeriesCollection from org.jfree.chart.plot import PlotOrientation, XYPlot from org.jfree.data.xy import XYDataset, XYSeries, XYSeriesCollection from org.jfree.data.category import DefaultCategoryDataset from org.jfree.chart.ui import RectangleEdge from javax.swing import JFrame import java.awt.Color as Color from java.awt import Dimension, BasicStroke import java.awt.Frame as Frame import java.awt.Window as Window from java.io import File as File from java.lang import System as System from ij import WindowManager as WindowManager from ij.plugin.frame import RoiManager as RoiManager from ij.process import ImageStatistics as ImageStatistics from ij.measure import Measurements as Measurements from ij import IJ as IJ from ij.measure import CurveFitter as CurveFitter from ij.gui import Plot as Plot from ij.gui import PlotWindow as PlotWindow from ij.gui import ImageWindow as ImageWindow from ij.gui import GenericDialog from ij import ImagePlus as ImagePlus from ij.io import FileInfo as FileInfo from ij.measure import ResultsTable as ResultsTable from ij import WindowManager as WindowManager import math import os from os import path, mkdir import csv #below from #https://stackoverflow.com/questions/736043/checking-if-a-string-can-be-converted-to-float-in-python def isfloat(value): try: float(value) return True except ValueError: return False def doPlot(): lineseparator = "\n" cellseparator = "," plotColor=[] plotColor = ("black", "red", "blue", "#32a852", "#7132a8", "#9C2005", "#754F04", "magenta", "darkGray", "gray", "lightGray", "green", "#9C6905", "cyan", "orange", "pink", "yellow"); #Call below is a feature of Fiji/IJ2 #See https://imagej.net/scripting/parameters k = len(listOfPaths) #for i in range(0, k): #print(listOfPaths[i]) ## Ask if a single X and multiple Y columns or paired columns gd = GenericDialog("Data Structure") dataStructure = ["Single X column", "Paired X and Y columns"] gd.addRadioButtonGroup("Data structure:", ["Single X column", "Paired X and Y columns"], 1,2,"Paired X and Y columns") gd.showDialog() if gd.wasOKed(): pairedChoice = gd.getNextRadioButton() else: return None #if pairedChoice == "Single X column": #IJ.showMessage("Not implented yet!") #return None for i in range(0, k): #read in data from CSV file #copies the whole file to an array of lines #have to type cast pathnames to Python str text_file = open(str(listOfPaths[i]), "r") #Use a Java method instead (need JVM 7+) file = File(str(listOfPaths[i])) #get file name using getName() filename = file.getName() #read whole file to a string data = text_file.read() #close file text_file.close() #separate into lines of text lines=data.split(lineseparator) numRows = len(lines) # recreates the columns headers labels=lines[0].split(cellseparator) #get total columns numCol = len(labels) if pairedChoice == "Paired X and Y columns": # If paired columns #set up plot using ImageJ plot function myPlot=Plot(filename, "X", "Y") myPlot.setLineWidth(2) #IJ.open(str(listOfPaths[i])) legendString = labels[0] + " v. " + labels[1] for m in range(0, numCol/2): x1=[] y1=[] #Iterate over each row in the csv using reader object #assume header row; skip this, since we already did this above for j in range(1,numRows): row=lines[j].split(cellseparator) #print("Row:") #print(row) #print(len(row)) #added this because some CSV files have a trailing carriage return... if (len(row) > 1): #need to add code to check for empty cells, since ImageJ #produces CSV files in which there can be unequal numbers of rows with empty cells #Jython interpreter crashes when trying to convert to float if cell is blank #ImageJ plot functions are savvy about blank cells, but this code isn't! if ((isfloat(row[2*m])) and (isfloat(row[2*m+1]))): x1.append(float(row[2*m])) y1.append(float(row[2*m+1])) myPlot.setColor(plotColor[m]) myPlot.add("Line",x1,y1) if (m > 0): legendString = legendString + "\t" + labels[2*(m)] + " v. " + labels[2*m+1] #Removes gridlines; delete if you like these... myPlot.setFormatFlags(0x330f) myPlot.show() myPlot.setColor("black") myPlot.setLegend(legendString,Plot.AUTO_POSITION) myPlot.setLimitsToFit(True) else: # If single X column and multiple Y columns #set up plot using ImageJ plot function myPlot=Plot(filename, "X", "Y") myPlot.setLineWidth(2) #IJ.open(str(listOfPaths[i])) legendString = "" for m in range(1, numCol): x1=[] y1=[] #Iterate over each row in the csv using reader object #assume header row; skip this, since we already did this above for j in range(1,numRows): row=lines[j].split(cellseparator) #print("Row:") #print(row) #print(len(row)) #added this because some CSV files have a trailing carriage return... if (len(row) > 1): #need to add code to check for empty cells, since ImageJ #produces CSV files in which there can be unequal numbers of rows with empty cells #Jython interpreter crashes when trying to convert to float if cell is blank #ImageJ plot functions are savvy about blank cells, but this code isn't! if ((isfloat(row[0])) and (isfloat(row[m]))): x1.append(float(row[0])) y1.append(float(row[m])) myPlot.setColor(plotColor[m]) myPlot.add("Line",x1,y1) if (m == 1): legendString = labels[m] else: legendString = legendString + "\t" + labels[m] #Removes gridlines; delete if you like these... myPlot.setFormatFlags(0x330f) myPlot.show() myPlot.setColor("black") myPlot.setLegend(legendString,Plot.AUTO_POSITION) myPlot.setLimitsToFit(True) #main #@ File[] listOfPaths (label="select files", style="files") doPlot()