sda.io.loadspe#
Load spectroscopic files.
Original functions are taken from : http://people.seas.harvard.edu/~krussell/html-tutorial/_modules/winspec.html Fixed for Python 3 Added Imaging mode support
Classes#
Machinery to batch process .spe files. |
Functions#
Module Contents#
- sda.io.loadspe.loadspe(fname, frames='all', flatten_1D=True, flatten_frames=True, verbose=True, warnings=True, unpack=False, return_info=False)#
Read in an SPE file and return the spectrum as a numpy array.
- usage:
wavelength, luminescence = get_spectrum( spe_filename ).
Type of winspec files read: 1D spectrum, 2D spectrum, 2D imaging
- Parameters:
fname (
str) – winspec fileframes (
intor'all') – for images that contain more than 1 frame: if ‘all’, loads all the frames if an int, return a particular time frame Note that all frames can be loaded but only a few plotted or exported using frames=’all’ on import and the frames argument in get() functionflatten_1D (
boolean) – ifTrue, auto-flatten 1D arraysflatten_frames (
boolean) – ifTrue, auto-flatten luminescence when there is one frame onlyunpack (
boolean) – deprecatedverbose (
bool) – talkwarnings (
bool) – Can beFalse,Trueor2to display even more warnings (ex: if file format does not respect the suggested format. See Notes)return_info (
bool) – if True return spectrum conditions as read in spe file
- Returns:
In imaging mode,output is a (i,x,y) array– i: frame number x, y: 2D pixels luminescence value for frame iIn spectrum mode, output is a tuple (
(wavelength,(i,x,y))) – wavelength: calibrated wavelength i: frame number x, y: 2D luminescence value for frame iIf flatten_1D option is activated (by default)anddata is detectedtobe1D-spectrum then output is a tuple (
(wavelength,(i,luminescence)) with:) – wavelength: calibrated wavelength i: frame number luminescence: 1D array luminescence value for frame iIf flatten_frames option is activated (by default)andthere is only oneframe then output is (x,y) array with– x, y: 2D pixels luminescence valueIf both flatten_1Dandflatten_frames are activated we obtain the followingoutput (
(wavelength,luminescence))
Notes
File naming convention:
Warning
Winspec has a few limitations in how it stores metadata. In particular:
- the number of software accumulations is the one that was chosen
before the run. If the run was paused before all accumulations were completed then this number if wrong, and any averaging procedure using this number will result in errors.
- Winspec cannot handle numbers greaters 32767 (2^8). Then it restarts
from -32767. Comparing with filenames makes sure the number of onCCD accumulations is at least the same modulo something.
To deal with this the SpecAnalyzer expects a naming convention for your filename. This way, it will cross-check that the names match with the metadata stored in the .spe file. The format is as follow:
‘[something]-gpe[gpe]-acc[acc]-[somethingelse].SPE’
Where [gpe] and [acc] are the number of on CCD accumulations and software accumulations, respectively. So for instance:
'spark_regime-gpe100000-acc100-.SPE'See
_read_param_from_filename()for more informationSee also
- sda.io.loadspe.spe2im(fname, fout, frames='all', verbose=True, warnings=True)#
Convert Winspec file to image file.
Supported format: BMP, EPS, JPG, JPEG, PDF, PNG, PPM.
Only Tif can handle multiple frames Tif is exported in int16 to be readable by ImageJ
- class sda.io.loadspe.SpecAnalyzer(avgfunc=None, verbose=False, warnings=True, compare_with_filename=False)#
Machinery to batch process .spe files.
- Parameters:
avgfunc (
function) – function to get the real average intensity as a function of the number of on-chip accumulation. If None, then the one from Rusterholtz, 2012 is used (calibrated for the PIMax 2ns)spefilename (
str) – name of the SPE file to be readverbose (
bool) – print(debug statements (True) or not (False)) Can be ‘all’ to print every single detailwarnings (
bool) – print(warnings (True) or not (False))compare_with_filename (
boolean) – ifTrue, analyze filename to extract accumulations, onCCD, gain, features and compare them with data stored in the file. See Notesverbose – talk
warnings – Can be
False,Trueor2to display even more warnings (ex: if file format does not respect the suggested format. See Notes)
Notes
File naming convention:
Warning
Winspec has a few limitations in how it stores metadata. In particular:
- the number of software accumulations is the one that was chosen
before the run. If the run was paused before all accumulations were completed then this number if wrong, and any averaging procedure using this number will result in errors.
- Winspec cannot handle numbers greaters 32767 (2^8). Then it restarts
from -32767. Comparing with filenames makes sure the number of onCCD accumulations is at least the same modulo something.
To deal with this the SpecAnalyzer expects a naming convention for your filename. This way, it will cross-check that the names match with the metadata stored in the .spe file. The format is as follow:
'[something]-gpe[gpe]-acc[acc]-[somethingelse].SPE'Where [gpe] and [acc] are the number of on CCD accumulations and software accumulations, respectively. So for instance:
'spark_regime-gpe100000-acc100-.SPE'See
_read_param_from_filename()for more informationExamples
How to use:
S = SpecAnalyzer() S.calibrate(theory,file1) S.load(winspec_file_1) S.average(winspec_file_1) S.export(file_out_1) S.load(winspec_file_2) S.average(winspec_file_2) S.export(file_out_2)
etc.
See also
- spedict = None#
- calibrated = False#
- absolute = False#
- avgfunc = None#
- verbose = False#
- warnings = True#
- data = None#
- wavelength = None#
- luminescence = None#
- xpixels = None#
- ypixels = None#
- dnfolder = None#
- dnfiles#
- mode = None#
- is1D = None#
- single_frame = None#
- is_2D()#
- load(fname, frames='all', mode_2D='auto')#
Read in an SPE file.
- Parameters:
fname (
str) – winspec fileunpack (
boolean) – ifTrue, returns a list instead of a matrixindex (
int) – returns a time frame. (default: 0)all_frames (
boolean, or :py:class:``’auto’:py:class:``) – ifTrue, returns all the time frame (index is then unused) Default:'auto'mode_2D (
boolean, or :py:class:``’auto’:py:class:``) – ifTrue, returns 2D images as a 2D matrix. Default:'auto'
- Returns:
but stores content of file in SpecAnalyzer as 2D array (wavelength, luminescence) + metadata
- Return type:
- get_info(filt=['data', 'XCALIB'])#
Get all infos, removing binary input and keys in filt.
- calibrate(chk_w=True)#
Calibration factor: real data = measured / calibration factor.
- loadtxt(f)#
Open with numpy loadtxt functions.
Transpose if needed.
- x_calibration(input_path)#
Calibrate the wavelength.
If initial .spe file was recorded with a bad calibration in wavelength, since the data are recorder according to.
pixel positions and that wavelengths are attributed at loading time, it is possible to correct it. For this, we need another .spe or .txt file matching the original file but with the correct calibration. This function updates the self.wavelength attribute of the SpecAnalyser object using the new calibration.
- Parameters:
input_path (
str) – path of file with new wavelength calibration- Returns:
but wavelength is calibrated
- Return type:
Examples
s = SpecAnalyzer() s.load(data) s.x_calibration(self, path_to_calibration_file)
- set_calibration(ftheory, fmeasured, bplot=True)#
Parameters.
- fmeasured: str
another text file (could have been obtained and exported
with another SpecAnalyzer), or a Winspec file. Can be 1D or 2D.
- Parameters:
bplot (
boolean) – plot calibration?
Notes
Warning
if importing a .SPE file directly, there is no way to remove the straylight.
Examples
Determining 1D calfactor from 2D calfactor at a given wavelength With Im the measured intensity, I the theoretical one, C the calibration factor, i an y pixel index:
Im=Sum(Im_i) # assuming the camera vertical integration is linear I=Im/C=Sum(Im_i/C_i)
–> C=Im/I=Sum(Im_i/I)=Sum(C_i)
- set_dark_noise(folder)#
Choose the folder from which dark noise will be corrected.
Folder contains text files (could have been obtained and exported with another SpecAnalyzer) whose name explicitely display the onCCD number.
Store all dark noise files onCCD numbers in a dictionary for easy access
- remove_dark_noise(matchCCD=False, force_CCD_averaged=False)#
To do before averaging!.
Finds the dark noise measurement with the same amount of onCCD acc, and removes it. If there is no match, and matchCCD is not True, then proceed to a linear interpolation between the two closest values
- remove_residual_background(percentile=50, bplot=False)#
- average(CCD=True, soft=True)#
Divide by number of accumulations, and effective number of CCD accumulation.
See David Pai’s 2008 PhD thesis p.40 for details.
If no effective averaging function of CCD number is given, use Rusterholtz 2012 PhD thesis p. 73.
Note: you may want to average separately over software first and onCCD then if you need to remove the software-averaged dark noise in between
- average_CCD()#
Divide only by number of on CCD accumulations.
See average function for more details.
- average_software()#
Divide only by number of software accumulations.
See average function for more details.
- smooth_2D(kernel_dim=(11, 11), bplot=False)#
Smooth the luminescence with a moving average filter.
The size of the averager filter is specified by kernel_dim (wavelengths,pixels).
- integrate_wavelengths(wc, wmin, wmax, ftheory, fmeasured, bplot=False)#
To Do.
- integrate_wavelengths_V2(wc, wmin, wmax)#
To Do.
- integrate_wavelengths_auto(wc, treshold=0.15, smooth_param=2, bplot=False)#
To Do.
- plot(frames='all', label='', fig=None, style='origin')#
Plot luminescence.
If several frames, several figures are plot.
- Parameters:
- Returns:
Plot the chosen set. Intensity is normalized with minimumandmaximumvalueofthe set
- plot_options(frames='all', label='', fig=None, style='origin', abscissa_wl=False, show=False, lrange=None, scale=None, cmap='jet', mean=False, title='', div_factor=1)#
Plot luminescence.
If several frames, several figures are plot.
- Parameters:
frames (
int, or'all') – if ‘all’ plot all frames that have been loadedlabel (
str) – add label to plotstyle (
str) – which publib style to apply. Default ‘origin’abscissa_wl (
bool) – if True, plot the abscissa axis in nm. Default is Falseshow (
bool) – if True, show the image. Default is False.lrange (
listof2 floats) – minimale and maximale value of the scalescale (
listof4 floats) – scale[0] = pixel corresponding to 0 mm along the horizontal axis scale[1] = ratio pixel/mm along the horizontal axis scale[2] = pixel corresponding to 0 mm along the vertical axis scale[3] = ratio pixel/mm along the vertical axiscmap (
string) – set the colormap of the figure. Default is ‘jet’mean (
bool) – if True, plot the mean image (in case they are several frames). Default is False
- Returns:
Plot the chosen set. Intensity is normalized with minimumandmaximumvalueofthe set
- plot_Winspec(title='')#
Plot a 2D image the Winspec way, with side graphs.
Note that has a reverse y axis by default. And Python stores data row-first so the image also has to be transposed.
- get(frames='all', unpack=True, flatten_1D=True, flatten_frames=True, transpose=False)#
Return data (wavelength, luminescence).
- Parameters:
frames (
int,listofintor'all') – give possibility to get only a few frames among the one already loadedunpack (
deprecated)flatten_1D (
boolean)flatten_frames (
boolean)transpose (
boolean)force_array (
boolean) – ifTrue, results are returned under array form even in spectrum mode (if may require the luminance to be transposed)
- Returns:
In imaging mode,output is a (i,x,y) array– i: frame number x, y: 2D pixels luminescence value for frame iIn spectrum mode, output is a tuple (
(wavelength,(i,x,y))) – wavelength: calibrated wavelength i: frame number x, y: 2D luminescence value for frame iIf flatten_1D option is activated (by default)anddata is detectedtobe1D-spectrum then output is a tuple (
(wavelength,(i,luminescence)) with:) – wavelength: calibrated wavelength i: frame number luminescence: 1D array luminescence value for frame iIf flatten_frames option is activated (by default)andthere is only oneframe then output is (x,y) array with– x, y: 2D pixels luminescence valueIf both flatten_1Dandflatten_frames are activated we obtain the followingoutput (
(wavelength,luminescence))
- export_2D(filename, create_folders=True)#
Export data as a 257*1024 numpy array in text format.
first line is the wavelengths.
- Parameters:
filename (
str) – file to savecreate_folder (
boolean) – ifTrue, creates directories if needed
- export(filename, create_folders=True)#
Export data as a numpy array in text format.
- Parameters:
filename (
str) – file to savecreate_folder (
boolean) – ifTrue, creates directories if needed
- write(fname, frames='all')#
Write data as an image format.
If file format is not specified by the file extension, then “.tif” is used by default. If guessing the format from the file extension fails, a warning is raised and the image is saved as “.tif”. Note that only the .tif format can be used to save a stack of images. If you choose another format, you have to choose one single frame only.
- check_param(gain=-1, onccd=-1, gate_width=-1, gate_delay=-1, verbose=True)#
Check acquisition parameters.
Check that the acquisiton parameters filled in by the user in the function correspond to the metadata of the file.
- Parameters:
gain (
int, optional) – Intensifier gain. The default is -1, the value is not checkedonccd (
int, optional) – number of on CCD accummulations. The default is -1, the value is not checkedgate_width (
float, optional) – gate width in µs. The default is -1, the value is not checkedgate_delay (
float, optional) – gate delay in µs. The default is -1, the value is not checkedverbose (
boolean, optional)of (write the value)
- Return type:
True if the parameters correspondtothe metadata
- subtract(other, mode='by_frame')#
Subtract other to self.
- Parameters:
other (
SpecAnalyzer object) – subtract other to self.mode (
string, optional) – subtract the mean ‘frame’ of other to self or subtract frame one by one i.e.: [self[1]-other[1], self[2]-other[2],..]. The default is ‘by_frame’.
- Returns:
s – the resulting self-other.
- Return type:
SpecAnalyzer object
- flip_2Dimage(axis=2)#
- resize_roi(roi=None)#
Resize the ROI of all frames of the SpecAnalyzer object.
Parameter#
- roilist, optional
list of 4 int. The default is None.
- rtype:
None.
- binned(frames='all', flatten_1D=True, flatten_frames=True, transpose=True)#
Return data (wavelength, luminescence).
- Parameters:
frames (
int,listofintor'all') – give possibility to get only a few frames among the one already loadedunpack (
deprecated)flatten_1D (
boolean)flatten_frames (
boolean)transpose (
boolean)force_array (
boolean) – ifTrue, results are returned under array form even in spectrum mode (if may require the luminance to be transposed)
- Returns:
In imaging mode,output is a (i,x,y) array– i: frame number x, y: 2D pixels luminescence value for frame iIn spectrum mode, output is a tuple (
(wavelength,(i,x,y))) – wavelength: calibrated wavelength i: frame number x, y: 2D luminescence value for frame iIf flatten_1D option is activated (by default)anddata is detectedtobe1D-spectrum then output is a tuple (
(wavelength,(i,luminescence)) with:) – wavelength: calibrated wavelength i: frame number luminescence: 1D array luminescence value for frame iIf flatten_frames option is activated (by default)andthere is only oneframe then output is (x,y) array with– x, y: 2D pixels luminescence valueIf both flatten_1Dandflatten_frames are activated we obtain the followingoutput (
(wavelength,luminescence))
- copy()#
- reverse()#