Giter VIP home page Giter VIP logo

spe2py's Introduction

spe2py

spe2py is a module that imports a Princeton Instruments LightField (SPE 3.x) file into a python environment.

Basic Usage

Loading and accessing data

Use the load() function to load one or more SPE files at a time:

>>> import spe2py as spe
>>> loaded_files = spe.load()

A file selection window will open to allow browsing for source files. The result is an individual SpeFile object, or, in the case where multiple files are loaded at once, a list of SpeFile objects.

Raw data from a file is stored in NumPy arrays and can be accessed directly by

>>> frame_data = loaded_files.data[frame][regionOfInterest]  
>>> frame_data = loaded_files[n].data[frame][regionOfInterest]  # where multiple files are loaded

Alternatively one can load an individual file by passing SpeFile() the source path directly:

file_object = spe.SpeFile(path)
# is equivalent to...
file_object = spe.load()  # and selecting the same file/path
Automatic imaging and plotting

To quickly view an individual frame, region-of-interest, or spectrum, use the image() or specplot() methods. For example,

>>> loaded_file.image()  # images the first frame and region of interest
>>> loaded_file.image(f, r)  # images frame 'f' and region of interest 'r'
>>> loaded_file.specplot()  # plots the loaded spectrum
Accessing metadata

Upon loading, the metadata contained in the file's XML footer is automatically parsed and stored as an untangle object in the footer variable. Elements and attributes can be accessed by calling the different elements and subelements of footer, ending with the attribute as a string:

>>> sensor_height = loaded_file.footer.SpeFormat.Calibrations.SensorInformation['height']

One can print the full element tree by calling the xmltree() method.

Dependencies

  • NumPy - data storage and file reading
  • tkinter - file selection dialog
  • matplotlib - imaging and plotting
  • untangle - XML parsing

Version

1.0.0a - initial upload

License

MIT

spe2py's People

Contributors

ashirsch avatar gh4ag avatar minouhub avatar rashidzia avatar stevejbrown avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

spe2py's Issues

Error When loading file

Hi,

I got the following error for loading spe file generated by LightField6.7. Could you help me check what might be the problem? Thanks!

In [1]: import spe2py as spe

In [2]: loaded_files = spe.load()

ValueError Traceback (most recent call last)
in ()
----> 1 loaded_files = spe.load()

~\pyLabLib-dev\spe2py\spe2py.py in load(filepaths)
273 batch = [[] for _ in range(0, len(filepaths))]
274 for file in range(0, len(filepaths)):
--> 275 batch[file] = SpeFile(filepaths[file])
276 return_type = "list of SpeFile objects"
277 if len(batch) == 1:

~\pyLabLib-dev\spe2py\spe2py.py in init(self, filepath)
53 # Note: these methods depend on self.footer
54 self.xdim, self.ydim = self._get_dims()
---> 55 self.roi, self.nroi = self._get_roi_info()
56 self.wavelength = self._get_wavelength()
57

~\pyLabLib-dev\spe2py\spe2py.py in _get_roi_info(self)
148 else:
149 nroi = 1
--> 150 roi = np.array([regionofinterest])
151
152 return roi, nroi

ValueError: cannot copy sequence with size 0 to array axis with dimension 1

Better way to read roi

Now, the code read roi from
footer.SpeFormat.DataHistories.DataHistory.Origin.Experiment.Devices.Cameras.Camera.ReadoutControl.RegionsOfInterest.CustomRegions.RegionOfInterest
However, for LightField 6.7, if you choose Full Sensor mode, roi here is untrustworthy, like this:

<CustomRegions count="1" relevance="False" type="RegionOfInterestCollection">
    <RegionOfInterest id="0" x="71" width="286" xBinning="1" y="413" height="512" yBinning="1" />
</CustomRegions>

It should be <RegionOfInterest id="0" x="0" width="1024" xBinning="1" y="0" height="1024" yBinning="1" /> for Full Sensor mode. relevance="False" means this roi is wrong. Although the code can read main data correctly (beacuse it use xDim from footer.SpeFormat.DataFormat.DataBlock.DataBlock to calculate read how many data), but the roi is absolutely wrong, which make us confused for a long time.
At least for LightField6.7 * EMCCD 1024B_eXcelon3, there is a place storing the right roi, change CustomRegions to Result, namely:
footer.SpeFormat.DataHistories.DataHistory.Origin.Experiment.Devices.Cameras.Camera.ReadoutControl.RegionsOfInterest.Result.RegionOfInterest
like this:

<Result r:readOnly="True" type="RegionOfInterestCollection" count="1">
    <RegionOfInterest id="0" x="0" width="1024" xBinning="1" y="0" height="1024" yBinning="1" />
</Result>

It suit all mode in LightField6.7 (Full Sensor, Full Sensor(Binned), Rows Binned, Custom Regions of Interest).

matplotlib.pyplot conflict with QtCore.QThread and multiprocessing.Process

Even if only import matplotlib.pyplot as plt without use, QtCore.QThread and multiprocessing.Process will collapses when call strat() and new a SpeFile Object.
It will work better if only import matplotlib and replace plt with matplotlib.plt, only collapses when using image and specplot.
Python 3.8.1

metadata needs to be read alongside data

Hi, This is a fantastic utility.

Maybe i've missed an option someplace, but I don't think you're accounting for the possibility of metadata being present. In this case, the times and frame numbers will be present after the data and so become convoluted with the pixel values in your implementation. Easy enough fix, you just need to do a check for the values and dtypes in your MetaBlock and then read them in during _read_data. Alternatively compare the difference between the frame size and stride size to find the number of additional bytes to be read for each frame.

For example, the following would work for 3 pieces of metadata.

def _read_data(self, file):
        file.seek(4100)
        
        data = [[0 for _ in range(self.nroi)] for _ in range(self.nframes)]
        metadata = []
        for frame in range(0, self.nframes):
            for region in range(0, self.nroi):
                if self.nroi > 1:
                    data_xdim = len(self.xcoord[region])
                    data_ydim = len(self.ycoord[region])
                else:
                    data_xdim = np.asarray(self.xdim[region], np.uint32)
                    data_ydim = np.asarray(self.ydim[region], np.uint32)
                data[frame][region] = np.fromfile(file, self.dtype, data_xdim * data_ydim).reshape(data_ydim, data_xdim)
            metadata.append(np.fromfile(file, np.dtype('Int64'),3))
            
        return data, metadata

Cheers,
Cam

python2 compatibility

It would be good to have compatibility with python 2.x

I believe all that is required is having some alternatives to tkinter and switching out StringIO for ByteIO

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.