This is a programming question, not so much Fiji specific. I want to extend and wrap the functionality of certain classes of the ImageJ API with Jython to make things a bit feel more Pythonic in my scripts. I can create a new object of a type inheriting from a base class without difficulty, but I am a bit confused as to how you can initialize a new object using an existing instance of the base class. To illustrate, here is an example using the ResultsTable and wrapping it into a DictTable which extends the ResultsTable class by providing a
__getitem__ method for
dict like access to the columns.
Create and work with a new instance of the super class - easy enough…
import ij.measure.ResultsTable # A class wrapping the ResultsTable to behave more dict-like... class Spreadsheet(ij.measure.ResultsTable): def __init__(self): super(Spreadsheet, self).__init__() def __getitem__(self, key): return(list(self.getColumnAsVariables(key))) # Create an instance of it... ss = Spreadsheet() # Add some data in there... for i in range(1,11,1): ss.incrementCounter() ss.addValue("C1","row %s" % i) # Display it thanks to the ResultsTable base class... ss.show("Test") # Fetch rows 1-5 using Python dict/list syntax... print ss["C1"][0:5]
[“row 1”, “row 2”, “row 3”, “row 4”, “row 5”]
Wrapping an existing ResultsTable into a Spreadsheet
However, say I already have an instance of ResultsTable, can I initialize the Spreadsheet object with it without having to repopulate the empty table of the Spreadsheet? Copying the information over is easy enough, but I would imagine you should be able to since the base class is initialized when the in parent class instance is created. At the end it would behave something like this:
# Create a ResultsTable rt = ij.measure.ResultsTable() # Do a bunch of stuff with the results table... rt.incrementCounter() rt.addValue("C1", "Some stuff") # Now lets make it a Spreadsheet instead... ss = Spreadsheet(rt) ss.show("A Spreadsheet") print(ss["C1"])
Let me know if I can clarify the problem better. Also, any help is appreciated including links out to related problems and tutorials. My internet scouring didn’t turn up anything, but I could be using lousy search terms.