System Attributes and Common Data

Defining system attributes

System attributes should be defined as constants in whichever module makes the most sense. When other modules require access to an attribute, it should be imported from the module in which it is defined. In this way, attributes can be used throughout the model but only need to be changed/updated in one place. For example:

import lentil

FOCAL_LENGTH = 3

class TinyPupil(lentil.Pupil):
    def __init__(self):
        super().__init__(focal_length=FOCAL_LENGTH)

Loading Numpy .npy and .npz files

Numpy data stored in .npy and .npz files should be loaded with the numpy.load command.

  • .npy files store single arrays

  • .npz files store multiple arrays in a dictionary-like mapping

We’ll update the example above to load in some additional Numpy data:

import numpy as np
import lentil

FOCAL_LENGTH = 3

AMPLITUDE = np.load('amp.npy')
with np.load('mask.npz') as data:
    MASK = data['mask']

pupil = lentil.Pupil(focal_length=FOCAL_LENGTH,
                     mask=MASK,
                     amplitude=AMPLITUDE)

Loading FITS files

FITS files are most easily loaded using Astropy. You’ll have to figure out which Header Data Unit (HDU) stores the data you need, but the interface is otherwise fairly straightforward:

from astropy.io import fits

hdul = fits.open('opd.fits')
OPD = hdul[0].data

More details are available in the astropy.io.fits documentation.

Loading MATLAB .MAT files

For v6 and v7 to v7.2 MAT files, scipy.io.loadmat does the trick. MATLAB 7.3 format MAT files are HDF5 and not supported by scipy.io.loadmat but can probably be loaded by some other HDF5 Python library.

from scipy.io import loadmat

mat_contents = loadmat('opd.mat')
OPD = mat_contents('opd')

More details are available in the scipy.io.loadmat documentation.

Note

The easiest way to deal with MAT files is to convert them to Numpy arrays with mat2ndarray, get them in to Python, and save them using numpy.save.

Loading CSV files

import numpy as np

OPD = np.genfromtxt('opd.csv', delimiter=',', ship_header=1)

More details are available in the numpy.genfromtxt documenation.