Loading various data

These are examples Python codes to load several types of data format. To try these examples, simply download the .py files and put it in module folder of your working directory. Then you can load corresponding files by load function as follows:

from lys import *
data = load("Replace this string with your filename to be loaded")

It is noted that the Python codes in this page is not fully tested. So there may be bugs. If you want to make your own file loader, see Loading your file

Gatan Digital Micrograph .dm3 files

Install dm3_lib(https://bitbucket.org/piraynal/pydm3reader/src/master/) and put examples/load_dm3.py into module folder.

 1import sys
 2import numpy as np
 3from lys import Wave, registerFileLoader
 4
 5try:
 6    import dm3_lib as dm3
 7
 8    def __loadDm3(name):
 9        data = dm3.DM3(name)
10        w = Wave()
11        w.data = data.imagedata
12        if w.data.ndim == 2:  # image
13            w.x = np.arange(0, data.pxsize[0] * w.data.shape[0], data.pxsize[0])
14            w.y = np.arange(0, data.pxsize[0] * w.data.shape[1], data.pxsize[0])
15        elif w.data.ndim == 3:  # spectrum imaging
16            w.data = w.data.transpose(1, 2, 0)
17            w.x = np.arange(0, data.pxsize[0] * w.data.shape[0], data.pxsize[0])
18            w.y = np.arange(0, data.pxsize[0] * w.data.shape[1], data.pxsize[0])
19            e0 = float(data.tags['root.ImageList.1.ImageData.Calibrations.Dimension.2.Origin'])
20            de = float(data.tags['root.ImageList.1.ImageTags.EELS Spectrometer.Dispersion (eV/ch)'])
21            w.z = np.linspace(-e0, -e0 + de * w.data.shape[2], w.data.shape[2])
22        w.note = {}
23        try:
24            w.note['unit'] = data.pxsize[1]
25            w.note['specimen'] = data.info['specimen'].decode()
26            w.note['date'] = data.info['acq_date'].decode()
27            w.note['mag'] = float(data.info['mag'].decode())
28            w.note['time'] = data.info['acq_time'].decode()
29            w.note['voltage'] = float(data.info['hv'].decode())
30            w.note['mode'] = data.info['mode'].decode()
31            w.note['exposure'] = data.tags['root.ImageList.1.ImageTags.DataBar.Exposure Time (s)']
32            w.note['hbin'] = data.tags['root.ImageList.1.ImageTags.Acquisition.Parameters.Detector.hbin']
33            w.note['vbin'] = data.tags['root.ImageList.1.ImageTags.Acquisition.Parameters.Detector.vbin']
34            w.note['delay'] = data.tags['root.ImageList.1.ImageTags.Experiment.Laser.delay']
35            w.note['power'] = data.tags['root.ImageList.1.ImageTags.Experiment.Laser.power']
36            w.note['scanMode'] = data.tags['root.ImageList.1.ImageTags.Experiment.mode']
37        except Exception:
38            pass
39        return w
40except Exception:
41    def __loadDm3(name):
42        print("To use .dm3 files, install dm3_lib (https://bitbucket.org/piraynal/pydm3reader/src/master/)", file=sys.stderr)
43
44
45registerFileLoader(".dm3", __loadDm3)

WaveMetrics Igor .pxt files

Install igor package(https://pypi.org/project/igor/) by excuting:

pip install igor

Then put examples/load_PXT.py into module folder.

 1import igor.packed
 2import igor.igorpy
 3import numpy as np
 4
 5from lys import Wave, registerFileLoader
 6
 7
 8def __loadPxt(name):
 9    nam, ext = os.path.splitext(os.path.basename(name))
10    (rec, data) = igor.packed.load(name)
11    wav = igor.igorpy.Wave(data['root'][nam.encode('utf-8')])
12    w = Wave()
13    w.data = np.array(wav.data)
14    w.data.flags.writeable = True
15    note = [s.replace(" ", "").replace("\r", "").split("=") for s in wav.notes.decode().replace("\n\n", "\n").split("\n")]
16    w.note = {}
17    for n in note:
18        if len(n) == 2:
19            w.note[n[0].lower()] = n[1]
20    if w.data.ndim == 1:
21        w.x = wav.axis[0]
22    if w.data.ndim >= 2:
23        w.x = wav.axis[1]
24        w.y = wav.axis[0]
25    if w.data.ndim == 3:
26        w.x = wav.axis[2]
27        w.y = wav.axis[1]
28        w.z = wav.axis[0]
29    return w
30
31
32registerFileLoader(".pxt", __loadPxt)

DICOM .dicom files

Install pydicom package(https://github.com/pydicom/pydicom) by executin:

pip install pydicom

Then put examples/load_DICOM.py into module folder.

 1import pydicom
 2
 3from lys import Wave, registerFileLoader
 4
 5
 6def __loadDcm(name):
 7    data = pydicom.read_file(name)
 8    return Wave(data.pixel_array)
 9
10
11registerFileLoader(".pxt", __loadDcm)