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


class TinyPupil(lentil.Pupil):
    def __init__(self):

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


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

pupil = lentil.Pupil(focal_length=FOCAL_LENGTH,

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 import fits

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

More details are available in the documentation.

Loading MATLAB .MAT files

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

from import loadmat

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

More details are available in the documentation.


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

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.