tlambert03 / nd2

Full-featured nd2 (Nikon NIS Elements) file reader for python. Outputs to numpy, dask, and xarray. Exhaustive metadata extraction
https://tlambert03.github.io/nd2
BSD 3-Clause "New" or "Revised" License
54 stars 15 forks source link

feat: add roi parsing #102

Closed tlambert03 closed 2 years ago

tlambert03 commented 2 years ago

This PR adds an rois property to nd2 files that parses the RoiMetadata_v1 metdata portion of an nd2 file into nice data structures (nd2.structures.ROI). it returns a dict of ROI objects keyed on the ROI id. A quick example for 1 ROI:

>>> import nd2
>>> f  = nd2.ND2File('tests/data/rois.nd2')
>>> f.rois
{
    1: ROI(
        id=1,
        info=RoiInfo(
            shapeType=<RoiShapeType.Rectangle: 3>,
            interpType=<InterpType.BackgroundROI: 2>,
            cookie=1,
            color=255,
            label='rect bgrd',
            stimulationGroup=0,
            scope=1,
            appData=0,
            multiFrame=False,
            locked=False,
            compCount=1,
            bpc=16,
            autodetected=False,
            gradientStimulation=False,
            gradientStimulationBitDepth=0,
            gradientStimulationLo=0.0,
            gradientStimulationHi=0.0
        ),
        guid='{0CC87494-DF49-47FE-A5DB-72C89670AB88}',
        animParams=[
            AnimParam(
                timeMs=0.0,
                enabled=1,
                centerX=-0.8171912832929782,
                centerY=-0.8656174334140436,
                centerZ=0.0,
                rotationZ=3.0,
                boxShape=BoxShape(
                    sizeX=0.21549636803874095,
                    sizeY=0.14285714285714285,
                    sizeZ=0.0
                ),
                extrudedShape=ExtrudedShape(sizeZ=0, basePoints=[])
            )
        ]
    )
}

here, is the full output of .rois for the rois.nd2 test file (which includes every variant of ROI that I could create in NIS-elements)

```python { 1: ROI( id=1, info=RoiInfo( shapeType=, interpType=, cookie=1, color=255, label='rect bgrd', stimulationGroup=0, scope=1, appData=0, multiFrame=False, locked=False, compCount=1, bpc=16, autodetected=False, gradientStimulation=False, gradientStimulationBitDepth=0, gradientStimulationLo=0.0, gradientStimulationHi=0.0 ), guid='{0CC87494-DF49-47FE-A5DB-72C89670AB88}', animParams=[ AnimParam( timeMs=0.0, enabled=1, centerX=-0.8171912832929782, centerY=-0.8656174334140436, centerZ=0.0, rotationZ=3.0, boxShape=BoxShape( sizeX=0.21549636803874095, sizeY=0.14285714285714285, sizeZ=0.0 ), extrudedShape=ExtrudedShape(sizeZ=0, basePoints=[]) ) ] ), 2: ROI( id=2, info=RoiInfo( shapeType=, interpType=, cookie=2, color=65280, label='ellipse bgrd', stimulationGroup=0, scope=1, appData=0, multiFrame=False, locked=False, compCount=1, bpc=16, autodetected=False, gradientStimulation=False, gradientStimulationBitDepth=0, gradientStimulationLo=0.0, gradientStimulationHi=0.0 ), guid='{C4C3910C-023D-465D-9A2E-D4706313B3B0}', animParams=[ AnimParam( timeMs=0.0, enabled=1, centerX=-0.5277481840193704, centerY=-0.8577875500280608, centerZ=0.0, rotationZ=44.99999999999989, boxShape=BoxShape( sizeX=0.26024268944395945, sizeY=0.12590799031476999, sizeZ=0.0 ), extrudedShape=ExtrudedShape(sizeZ=0, basePoints=[]) ) ] ), 3: ROI( id=3, info=RoiInfo( shapeType=, interpType=, cookie=3, color=16711680, label='polygon bgrd', stimulationGroup=0, scope=1, appData=0, multiFrame=False, locked=False, compCount=1, bpc=16, autodetected=False, gradientStimulation=False, gradientStimulationBitDepth=0, gradientStimulationLo=0.0, gradientStimulationHi=0.0 ), guid='{06F6E641-1B01-44AE-A953-FC4EC71190BF}', animParams=[ AnimParam( timeMs=0.0, enabled=1, centerX=-0.08289473684210513, centerY=-0.81, centerZ=0.0, rotationZ=0.0, boxShape=BoxShape(sizeX=0.0, sizeY=0.0, sizeZ=0.0), extrudedShape=ExtrudedShape( sizeZ=0, basePoints=[ XYPoint(x=-0.1592360137632217, y=-0.0423002421307506), XYPoint(x=0.14584873199949017, y=-0.05440677966101696), XYPoint(x=0.14100611698738363, y=-0.0011380145278450371), XYPoint(x=0.024783356696826787, y=0.07150121065375302), XYPoint(x=-0.07933286606346386, y=0.03760290556900728), XYPoint(x=-0.0841754810755704, y=0.02791767554479421), XYPoint(x=-0.12533770867847593, y=0.0109685230024213), XYPoint(x=-0.135022938702689, y=-0.005980629539951572) ] ) ) ] ), 4: ROI( id=4, info=RoiInfo( shapeType=, interpType=, cookie=4, color=65535, label='square bgrd', stimulationGroup=0, scope=1, appData=0, multiFrame=False, locked=False, compCount=1, bpc=16, autodetected=False, gradientStimulation=False, gradientStimulationBitDepth=0, gradientStimulationLo=0.0, gradientStimulationHi=0.0 ), guid='{F08F234B-7A1E-44DA-A089-C12F3A0B0796}', animParams=[ AnimParam( timeMs=0.0, enabled=1, centerX=0.25544794188861997, centerY=-0.8777239709443099, centerZ=0.0, rotationZ=0.0, boxShape=BoxShape( sizeX=0.11864406779661024, sizeY=0.11864406779661017, sizeZ=0.0 ), extrudedShape=ExtrudedShape(sizeZ=0, basePoints=[]) ) ] ), 5: ROI( id=5, info=RoiInfo( shapeType=, interpType=, cookie=5, color=16711935, label='bezier bgrd', stimulationGroup=0, scope=1, appData=0, multiFrame=False, locked=False, compCount=1, bpc=16, autodetected=False, gradientStimulation=False, gradientStimulationBitDepth=0, gradientStimulationLo=0.0, gradientStimulationHi=0.0 ), guid='{46549840-1D22-49B2-A96C-98319484BF58}', animParams=[ AnimParam( timeMs=0.0, enabled=1, centerX=0.6015999999999999, centerY=-0.8214, centerZ=0.0, rotationZ=0.0, boxShape=BoxShape(sizeX=0.0, sizeY=0.0, sizeZ=0.0), extrudedShape=ExtrudedShape( sizeZ=0, basePoints=[ XYPoint(x=-0.07859757869249392, y=-0.047849394673123484), XYPoint(x=-0.06891234866828086, y=-0.052692009685230023), XYPoint(x=0.05215302663438252, y=-0.09385423728813558), XYPoint(x=0.12237094430992727, y=0.015104600484261503), XYPoint(x=-0.09796803874092007, y=0.08290121065375303), XYPoint(x=-0.11491719128329293, y=-0.040585472154963685) ] ) ) ] ), 6: ROI( id=6, info=RoiInfo( shapeType=, interpType=, cookie=7, color=16776960, label='rect std', stimulationGroup=0, scope=1, appData=0, multiFrame=False, locked=False, compCount=1, bpc=16, autodetected=False, gradientStimulation=False, gradientStimulationBitDepth=0, gradientStimulationLo=0.0, gradientStimulationHi=0.0 ), guid='{7C2B4BDD-22A2-457E-BA17-398ECC52C57E}', animParams=[ AnimParam( timeMs=0.0, enabled=1, centerX=-0.8268765133171913, centerY=-0.3498789346246972, centerZ=0.0, rotationZ=0.0, boxShape=BoxShape( sizeX=0.2009685230024213, sizeY=0.13317191283292978, sizeZ=0.0 ), extrudedShape=ExtrudedShape(sizeZ=0, basePoints=[]) ) ] ), 7: ROI( id=7, info=RoiInfo( shapeType=, interpType=, cookie=8, color=33023, label='square std locked', stimulationGroup=0, scope=1, appData=0, multiFrame=False, locked=True, compCount=1, bpc=16, autodetected=False, gradientStimulation=False, gradientStimulationBitDepth=0, gradientStimulationLo=0.0, gradientStimulationHi=0.0 ), guid='{86CA682E-3FAE-48A3-933F-651C1E501E09}', animParams=[ AnimParam( timeMs=0.0, enabled=1, centerX=-0.5121065375302662, centerY=-0.30871670702179177, centerZ=0.0, rotationZ=0.0, boxShape=BoxShape( sizeX=0.15738498789346253, sizeY=0.15738498789346259, sizeZ=0.0 ), extrudedShape=ExtrudedShape(sizeZ=0, basePoints=[]) ) ] ), 8: ROI( id=8, info=RoiInfo( shapeType=, interpType=, cookie=9, color=65408, label='circ std', stimulationGroup=0, scope=1, appData=0, multiFrame=False, locked=False, compCount=1, bpc=16, autodetected=False, gradientStimulation=False, gradientStimulationBitDepth=0, gradientStimulationLo=0.0, gradientStimulationHi=0.0 ), guid='{4F73A49B-F0C2-446E-AE56-572CA196783A}', animParams=[ AnimParam( timeMs=0.0, enabled=1, centerX=-0.2584503631961259, centerY=-0.447409200968523, centerZ=0.0, rotationZ=0.0, boxShape=BoxShape( sizeX=0.1917577711692332, sizeY=0.1917577711692332, sizeZ=0.0 ), extrudedShape=ExtrudedShape(sizeZ=0, basePoints=[]) ) ] ), 9: ROI( id=9, info=RoiInfo( shapeType=, interpType=, cookie=10, color=16711808, label='3-pnt-circ std', stimulationGroup=0, scope=1, appData=0, multiFrame=False, locked=False, compCount=1, bpc=16, autodetected=False, gradientStimulation=False, gradientStimulationBitDepth=0, gradientStimulationLo=0.0, gradientStimulationHi=0.0 ), guid='{26AEB2D2-6EAD-4E91-8397-EB538BFE01C4}', animParams=[ AnimParam( timeMs=0.0, enabled=1, centerX=0.06821407698088011, centerY=-0.34599649327878435, centerZ=0.0, rotationZ=0.0, boxShape=BoxShape( sizeX=0.19428853375575592, sizeY=0.19428853375575592, sizeZ=0.0 ), extrudedShape=ExtrudedShape(sizeZ=0, basePoints=[]) ) ] ), 10: ROI( id=10, info=RoiInfo( shapeType=, interpType=, cookie=11, color=12583167, label='ellipse std', stimulationGroup=0, scope=1, appData=0, multiFrame=False, locked=False, compCount=1, bpc=16, autodetected=False, gradientStimulation=False, gradientStimulationBitDepth=0, gradientStimulationLo=0.0, gradientStimulationHi=0.0 ), guid='{BF255C93-AA68-462A-9B84-C1D25B9C17C6}', animParams=[ AnimParam( timeMs=0.0, enabled=1, centerX=0.35835351089588396, centerY=-0.3753026634382567, centerZ=0.0, rotationZ=90.0, boxShape=BoxShape( sizeX=0.2761989197404203, sizeY=0.14527845036319606, sizeZ=0.0 ), extrudedShape=ExtrudedShape(sizeZ=0, basePoints=[]) ) ] ), 11: ROI( id=11, info=RoiInfo( shapeType=, interpType=, cookie=13, color=12648192, label='4-pt-ellipse std', stimulationGroup=0, scope=1, appData=0, multiFrame=False, locked=False, compCount=1, bpc=16, autodetected=False, gradientStimulation=False, gradientStimulationBitDepth=0, gradientStimulationLo=0.0, gradientStimulationHi=0.0 ), guid='{489680A2-9ADD-4F97-965B-723EF5ACF6B8}', animParams=[ AnimParam( timeMs=0.0, enabled=1, centerX=0.6150121065375302, centerY=-0.37046004842615, centerZ=0.0, rotationZ=23.62937773065679, boxShape=BoxShape(sizeX=0.16, sizeY=0.12, sizeZ=0.0), extrudedShape=ExtrudedShape(sizeZ=0, basePoints=[]) ) ] ), 12: ROI( id=12, info=RoiInfo( shapeType=, interpType=, cookie=14, color=16760832, label='12: poly std', stimulationGroup=0, scope=1, appData=0, multiFrame=False, locked=False, compCount=1, bpc=16, autodetected=False, gradientStimulation=False, gradientStimulationBitDepth=0, gradientStimulationLo=0.0, gradientStimulationHi=0.0 ), guid='{25958736-BF1D-4D9D-9AA2-64832039559D}', animParams=[ AnimParam( timeMs=0.0, enabled=1, centerX=-0.7717283950617284, centerY=0.024320987654321113, centerZ=0.0, rotationZ=0.0, boxShape=BoxShape(sizeX=0.0, sizeY=0.0, sizeZ=0.0), extrudedShape=ExtrudedShape( sizeZ=0, basePoints=[ XYPoint(x=-0.09994230711744838, y=-0.05579798523301349), XYPoint(x=0.03807222072758794, y=-0.11148805787223864), XYPoint(x=0.1203966759333991, y=0.00715600992437146), XYPoint(x=-0.1217340746719278, y=0.15243446028756766), XYPoint(x=-0.04183092697216992, y=0.002313394912264926), XYPoint(x=-0.09401257444956283, y=-0.054612038699436316), XYPoint(x=-0.06362269452664933, y=-0.04853406271485369), XYPoint(x=-0.09509969210534185, y=-0.05579798523301349), XYPoint(x=-0.09401257444956283, y=-0.054612038699436316) ] ) ) ] ), 13: ROI( id=13, info=RoiInfo( shapeType=, interpType=, cookie=15, color=255, label='13: bezier std', stimulationGroup=0, scope=1, appData=0, multiFrame=False, locked=False, compCount=1, bpc=16, autodetected=False, gradientStimulation=False, gradientStimulationBitDepth=0, gradientStimulationLo=0.0, gradientStimulationHi=0.0 ), guid='{AC454281-4E0D-4076-AA97-1A1E2D90CE23}', animParams=[ AnimParam( timeMs=0.0, enabled=1, centerX=-0.3951282051282051, centerY=0.04794871794871791, centerZ=0.0, rotationZ=0.0, boxShape=BoxShape(sizeX=0.0, sizeY=0.0, sizeZ=0.0), extrudedShape=ExtrudedShape( sizeZ=0, basePoints=[ XYPoint(x=-0.07460545104612905, y=-0.10848140560004964), XYPoint(x=0.12152045694418576, y=-0.0745831005153039), XYPoint(x=-0.021336685912957093, y=0.14333457502949032), XYPoint(x=-0.11334637114298132, y=0.05858881231762581), XYPoint(x=-0.10850375613087479, y=0.051324889799466006), XYPoint(x=-0.021336685912957093, y=-0.02615695039423855), XYPoint(x=-0.07944806605823558, y=-0.062476562985037565), XYPoint(x=-0.10850375613087479, y=-0.1036387905879431) ] ) ) ] ), 14: ROI( id=14, info=RoiInfo( shapeType=, interpType=, cookie=16, color=65280, label='S1:14 stim1 rect', stimulationGroup=0, scope=1, appData=0, multiFrame=False, locked=False, compCount=1, bpc=16, autodetected=False, gradientStimulation=False, gradientStimulationBitDepth=0, gradientStimulationLo=0.0, gradientStimulationHi=0.0 ), guid='{ED328DF4-974F-419D-9C10-69A825029D0A}', animParams=[ AnimParam( timeMs=0.0, enabled=1, centerX=-0.7990314769975786, centerY=0.5484261501210654, centerZ=0.0, rotationZ=0.0, boxShape=BoxShape( sizeX=0.2324455205811138, sizeY=0.16222760290556892, sizeZ=0.0 ), extrudedShape=ExtrudedShape(sizeZ=0, basePoints=[]) ) ] ), 15: ROI( id=15, info=RoiInfo( shapeType=, interpType=, cookie=17, color=16711680, label='S2:15: stim 2 circle', stimulationGroup=1, scope=1, appData=0, multiFrame=False, locked=False, compCount=1, bpc=16, autodetected=False, gradientStimulation=False, gradientStimulationBitDepth=0, gradientStimulationLo=0.0, gradientStimulationHi=0.0 ), guid='{85D4DB84-60C1-4E9E-861D-009244E1BE35}', animParams=[ AnimParam( timeMs=0.0, enabled=1, centerX=-0.47457627118644063, centerY=0.486682808716707, centerZ=0.0, rotationZ=0.0, boxShape=BoxShape( sizeX=0.22739674347455488, sizeY=0.22739674347455488, sizeZ=0.0 ), extrudedShape=ExtrudedShape(sizeZ=0, basePoints=[]) ) ] ), 16: ROI( id=16, info=RoiInfo( shapeType=, interpType=, cookie=18, color=65535, label='S3:16 stim 3 poly', stimulationGroup=2, scope=1, appData=0, multiFrame=False, locked=False, compCount=1, bpc=16, autodetected=False, gradientStimulation=False, gradientStimulationBitDepth=0, gradientStimulationLo=0.0, gradientStimulationHi=0.0 ), guid='{A3ABB2DA-8F5D-4967-B8D2-2A88EE197157}', animParams=[ AnimParam( timeMs=0.0, enabled=1, centerX=-0.1825490196078431, centerY=0.4801960784313726, centerZ=0.0, rotationZ=0.0, boxShape=BoxShape(sizeX=0.0, sizeY=0.0, sizeZ=0.0), extrudedShape=ExtrudedShape( sizeZ=0, basePoints=[ XYPoint(x=-0.05231780847932399, y=-0.10247210748706266), XYPoint(x=0.09780325689597874, y=0.04522765038218665), XYPoint(x=0.030006646726487105, y=0.11302426055167814), XYPoint(x=-0.0959013435882828, y=-0.05646726487205058) ] ) ) ] ), 17: ROI( id=17, info=RoiInfo( shapeType=, interpType=, cookie=19, color=65280, label='R:17: reference rect', stimulationGroup=0, scope=1, appData=0, multiFrame=False, locked=False, compCount=1, bpc=16, autodetected=False, gradientStimulation=False, gradientStimulationBitDepth=0, gradientStimulationLo=0.0, gradientStimulationHi=0.0 ), guid='{720C601A-12B3-4201-B7E0-545FF23B0688}', animParams=[ AnimParam( timeMs=0.0, enabled=1, centerX=0.2276029055690072, centerY=0.11138014527845042, centerZ=0.0, rotationZ=0.0, boxShape=BoxShape( sizeX=0.305084745762712, sizeY=0.21791767554479421, sizeZ=0.0 ), extrudedShape=ExtrudedShape(sizeZ=0, basePoints=[]) ) ] ), 18: ROI( id=18, info=RoiInfo( shapeType=, interpType=, cookie=21, color=16711680, label='raster (0ptrans)', stimulationGroup=0, scope=1, appData=0, multiFrame=False, locked=False, compCount=0, bpc=0, autodetected=False, gradientStimulation=False, gradientStimulationBitDepth=0, gradientStimulationLo=0.0, gradientStimulationHi=0.0 ), guid='{A57022EC-044D-41EF-BDAF-50EAE43204E8}', animParams=[] ) } ``` <\details>