hadim / read-roi

Read ROI files .zip or .roi generated with ImageJ.
BSD 3-Clause "New" or "Revised" License
51 stars 22 forks source link

optionally preserve the order or ROIs in a zip #3

Closed swharden closed 7 years ago

swharden commented 7 years ago

Preserving the order of ROIs in a ZIP is important for my application, but returning ROIs as a dictionary loses this order. The zip file list contains the dictionary keys as file names in the preserved order. Since each list item in the returned list is a dict with 'name' key, no data is lost by returning a list over a dict. Making it optional preserves backward compatibility.

hadim commented 7 years ago

I agree that is an important and great feature !

But I don't think returning a dict or a list according to an option is a best way to achieve that. Instead you could use an OrderedDict that will preserve the order of elements in it : https://docs.python.org/3.4/library/collections.html#collections.OrderedDict

That will also remove the need of an optional argument.

What do you think ? Could you modify the PR to do that ?

swharden commented 7 years ago

I like that solution! It requires importing an extra object, but it's very clean. I modified the patch to reflect this method.

Image as seen in FIJI/ImageJ roidemo

Demo code:

from read_roi import read_roi_zip
rois = read_roi_zip('RoiSet.zip')
for i,key in enumerate(rois.keys()):
    print("ROI #%d = %s"%(i+1,key))

Before the change:

ROI #1 = 0084-0169-0079
ROI #2 = 0031-0062
ROI #3 = 0194-0025
ROI #4 = 0001-0043-0125
ROI #5 = 0072-0222-0118
ROI #6 = 0098-0235
ROI #7 = 0001-0109-0042
ROI #8 = 0615-0153-0170
ROI #9 = 0293-0130-0112
ROI #10 = 0136-0151
ROI #11 = 0209-0204-0235
ROI #12 = 0590-0177-0222
ROI #13 = 0072-0057
ROI #14 = 0814-0193-0187

After the change:

ROI #1 = 0031-0062
ROI #2 = 0072-0057
ROI #3 = 0194-0025
ROI #4 = 0136-0151
ROI #5 = 0098-0235
ROI #6 = 0615-0153-0170
ROI #7 = 0209-0204-0235
ROI #8 = 0814-0193-0187
ROI #9 = 0590-0177-0222
ROI #10 = 0084-0169-0079
ROI #11 = 0001-0109-0042
ROI #12 = 0001-0043-0125
ROI #13 = 0293-0130-0112
ROI #14 = 0072-0222-0118
hadim commented 7 years ago

Great work !

Thank you

hadim commented 7 years ago

I'll release a new version this week.