pyspecdata package¶
Subpackages¶
Submodules¶
pyspecdata.core module¶
Provides the core components of pyspecdata. Currently, this is a very large file that we will slowly break down into separate modules or packages.
The classes nddata
, nddata_hdf
, ndshape
, the
function plot()
, and the class fitdata
are the core components of the N-Dimensional processing routines.
Start by familiarizing yourself with those.
The figlist
is the base class for “Figure lists.”
Figure lists allows you to organize plots and text and to refer to plots
by name, rather than number.
They are designed so that same code can be used seamlessly from within
ipython, jupyter, a python script, or a python environment within latex
(JMF can also distribute latex code for this – nice python based
installer is planned).
The user does not initialize the figlist class directly,
but rather initializes figlist_var
.
At the end of this file,
there is a snippet of code that sets
figlist_var
to choice that’s appropriate for the working environment
(i.e., python, latex environment, *etc.)
There are many helper and utility functions that need to be sorted an documented by JMF,
and can be ignored.
These are somewhat wide-ranging in nature.
For example, box_muller()
is a helper function (based on numerical recipes) used by nddata.add_noise()
,
while h5 functions are helper functions for using pytables in a fashion that
will hopefull be intuitive to those familiar with SQL, etc.
-
exception
pyspecdata.core.
CustomError
(*value, **kwargs)¶ Bases:
exceptions.Exception
-
pyspecdata.core.
addlabels
(labelstring, x, y, labels)¶ obsolete – use plot_label_points
-
pyspecdata.core.
apply_oom
(average_oom, numbers, prev_label='')¶ scale numbers by the order of magnitude average_oom and change the label prev_label by adding the appropriate SI prefix
-
pyspecdata.core.
applyto_rec
(myfunc, myarray, mylist)¶ apply myfunc to myarray with the intention of collapsing it to a smaller number of values
-
pyspecdata.core.
autolegend
(*args, **kwargs)¶
-
pyspecdata.core.
autopad_figure
(pad=0.2, centered=False, figname='unknown')¶
-
pyspecdata.core.
box_muller
(length, return_complex=True)¶ algorithm to generate normally distributed noise
-
pyspecdata.core.
colormap
(points, colors, n=256)¶
-
pyspecdata.core.
concat
(datalist, dimname, chop=False)¶
-
pyspecdata.core.
contour_plot
(xvals, yvals, zvals, color='k', alpha=1.0, npts=300, **kwargs)¶
-
pyspecdata.core.
copy_maybe_none
(input)¶
-
pyspecdata.core.
decorate_rec
()¶ Decorate the rows in A with information in B –> if names overlap, keep the ones in A b_ind and a_ind can be either a single key, or a list of keys; if more than one element in B matches that in A, include both options!!
-
pyspecdata.core.
dp
(number, decimalplaces, scientific=False)¶
-
pyspecdata.core.
emptyfunction
()¶
-
pyspecdata.core.
emptytest
(x)¶
-
pyspecdata.core.
expand_x
(*args)¶ expand the axes. If an argument is passed, then it refers to the position relative to the current coordinates. Values can be: :0: set this side of the axis to 0 :None: leave this side of the axis alone :a double: rescale the distance from the center of the axis to this side by this number
-
pyspecdata.core.
expand_y
(*args)¶ expand the axes. If an argument is passed, then it refers to the position relative to the current coordinates. Values can be: :0: set this side of the axis to 0 :None: leave this side of the axis alone :a double: rescale the distance from the center of the axis to this side by this number
-
pyspecdata.core.
explain_error
(e)¶ Allows you to wrap existing errors with more explanation
For example:
> except BaseException as e: > raise IndexError(“I can’t find the node “+pathstring+explain_error(e)) > + ‘
‘.join([‘> ‘+j for j in str(e).split(‘ ‘)]))# this indents
-
pyspecdata.core.
fa
(input, dtype='complex128')¶
-
class
pyspecdata.core.
figlist
(*arg, **kwargs)¶ Bases:
object
-
basename
¶ A basename that can be changed to generate different sets of figures with different basenames. For example, this is useful if you are looping over different sets of data, and generating the same set of figures for each set of data (which would correspond to a basename).
Type: str
-
figurelist
¶ A list of the figure names
Type: list
-
figdict
¶ A dictionary containing the figurelist and the figure numbers or objects that they correspond to. Keys of this dictionary must be elements of figurelist.
Type: dict
-
propdict
¶ Maintains various properties for each element in figurelist. Keys of this dictionary must be elements of figurelist.
Type: dict
-
adjust_spines
(spines)¶
-
check_units
(testdata, x_index, y_index)¶
-
generate_ticks
(plotdata, axes, rescale, z_norm=None, y_rescale=1, text_scale=0.05, follow_surface=False, lensoffset=0.005, line_width=0.001, tube_radius=0.001, fine_grid=False)¶ generate 3d ticks and grid for mayavi
-
get_fig_number
(name)¶
-
get_num_figures
()¶
-
grid
()¶
-
header
(number_above, input_string)¶
-
image
(A, **kwargs)¶ Called as fl.image() where fl is the figlist_var object
Note that this code just wraps the figlist properties, and the heavy lifting is done by the image( function. Together, the effect is as follows:
check_units converts to human-readable units, and makes sure they match the units already used in the plot.
if A has more than two dimensions, the final dimension in A.dimlabels is used as the column dimension, and a direct-product of all non-column dimensions (a Kronecker product, such that the innermost index comes the latest in the list A.dimlabels) is used as the row dimension. A white/black line is drawn after the innermost index used to create the direct product is finished iterating.
If A consists of complex data, then an HSV plot (misnomer, actually an HV plot) is used: - convert to polar form: \(z=\rho \exp(i \phi)\) - \(\phi\) determines the color (Hue)
- Color wheel is cyclical, like \(\exp(i \phi)\)
- red is taken as \(\phi=0\), purely real and positive
- green-blue is \(pi\) radians out of phase with red and therefore negative real
\(\rho\) determines the intensity (value) - Depending on whether or not black is set (either as a
keyword argument, or fl.black, the background will be black with high \(\rho\) values “lit up” (intended for screen plotting) or the background will be white with the high \(\rho\) values “colored in” (intended for printing)
If the data type (dtype) of the data in A is real (typically achieved by calling abs(A) or A.runcopy(real)), then A is plotted with a colormap and corresponding colorbar.
If no title has been given, it’s set to the name of the current plot in the figurelist
-
x
¶ If A is a numpy array, then this gives the values along the x axis (columns). Defaults to the size of the array. Not used if A is nddata.
Type: Optional[double] or Optional[scalar]
-
y
¶ If A is a numpy array, then this gives the values along the y axis (columns). Defaults to the size of the array. Not used if A is nddata.
Type: Optional[double] or Optional[scalar]
-
x_first
¶ Since it’s designed to represent matrices, an image plot by defaults is “transposed” relative to all other plots. If you want the first dimension on the x-axis (e.g., if you are plotting a contour plot on top of an image), then set x_first to True.
Type: boolean
-
spacing
¶ Determines the size of the white/black line drawn Defaults to 1
Type: integer
-
ax
¶ the Axis object where the plot should go.
Type: matplotlib Axes
-
all remaning
are passed through to matplotlib imshow
-
origin
¶ upper and lower are passed to matplotlib. Flip is for 2D nmr, and flips the data manually.
Type: {‘upper’, ‘lower’, ‘flip’}
-
.. code-block:: python
from pyspecdata import * fl = figlist_var()
t = r_[-1:1:300j] x = nddata(t,[-1],[‘x’]).labels(‘x’,t) y = nddata(t,[-1],[‘y’]).labels(‘y’,t)
z = x**2 + 2*y**2 print “dimlabels of z:”,z.dimlabels
fl.next(‘image with contours’) fl.image(z,x_first = True) # x_first is needed to align # with the contour plot z.contour(colors = ‘w’,alpha = 0.75)
fl.next(‘simple plot’) # just to show that x is the same # here as well fl.plot(z[‘y’:(0,0.01)])
fl.show(‘compare_image_contour_150911.pdf’)
-
label_point
(data, axis, value, thislabel, show_point=True, xscale=1, **new_kwargs)¶ only works for 1D data: assume you’ve passed a single-point nddata, and label it
xscale gives the unit scaling
..todo:
Improve the unit scaling, so that this would also work. Allow it to include a format string that would use the value.
Parameters: show_point (bool) – Defaults to True. Actually generate a point (circle), vs. just the label.
-
marked_text
(marker, input_text='', sep='\n')¶ Creates a named marker where we can place text. If marker has been used, goes back and places text there.
-
mesh
(plotdata, Z_normalization=None, equal_scale=True, lensoffset=0.001, show_contours=False, grey_surf=False, **kwargs)¶
-
next
(input_name, legend=False, boundaries=None, twinx=None, **kwargs)¶ Switch to the figure given by input_name, which is used not only as a string-based name for the figure, but also as a default title and as a base name for resulting figure files.
In the future, we actually want this to track the appropriate axis object!
Parameters: - legend (bool) – If this is set, a legend is created outside the figure.
- twinx ({0,1}) –
1: plots on an overlayed axis (the matplotlib twinx) whose y axis is labeled on the right when you set this for the first time, you can also set a color kwarg that controls the coloring of the right axis. 0: used to switch back to the left (default) axis - boundaries – need to add description
- kwargs (dict) – Any other keyword arguments are passed to the matplotlib (mayavi) figure() function that’s used to switch (create) figures.
-
phaseplot_finalize
()¶ Performs plot decorations that are typically desired for a manual phasing plot. This assumes that the
y
-axis is given in units of half-cycles ($pi$ radians).
-
plot
(*args, **kwargs)¶ Parameters: - linestyle ({':','--','.','etc.'}) – the style of the line
- plottype ({'semilogy','semilogx','loglog'}) – Select a logarithmic plotting style.
- nosemilog (True) – Typically, if you supply a log-spaced axis, a semilogx plot will be automatically selected. This overrides that behavior. Defaults to False.
-
pop_marker
()¶
-
push_marker
()¶
-
setprops
(**kwargs)¶
-
show
(*args, **kwargs)¶
-
show_prep
()¶
-
text
(mytext)¶
-
twinx
(autopad=False, orig=False, color=None)¶
-
use_autolegend
(value=None)¶ No argument sets to true if it’s not already set
-
-
pyspecdata.core.
figlist_var
¶ alias of
pyspecdata.core.figlist
-
pyspecdata.core.
figlistini
(first_figure)¶ processes a figure list argument: typically, you want to have a figure_list keyword argument for every function, which is by default set to None, then call this on the argument – it always returns a figure list, creating a new one if required similarly, somewhere I have another guy that processes the output, so that if it’s set to None, it will by default dump and show the figure list, and not return a figure list in the output
-
pyspecdata.core.
figlistini_old
(first_figure)¶
-
pyspecdata.core.
figlistret
(first_figure, figure_list, *args, **kwargs)¶
-
class
pyspecdata.core.
fitdata
(*args, **kwargs)¶ Bases:
pyspecdata.core.nddata
-
add_inactive_p
(p)¶
-
analytical_covariance
()¶
-
bootstrap
(points, swap_out=None, seedval=10347, minbounds={}, maxbounds={})¶
-
copy
()¶ Return a full copy of this instance.
Because methods typically change the data in place, you might want to use this frequently.
Parameters: data (boolean) – Default to True. False doesn’t copy the data – this is for internal use, e.g. when you want to copy all the metadata and perform a calculation on the data.
The code for this also provides the definitive list of the nddata metadata.
-
covar
(*names)¶ give the covariance for the different symbols
-
covarmat
(*names)¶
-
errfunc
(p, x, y, sigma)¶ just the error function
-
eval
(taxis, set=None, set_to=None)¶ after we have fit, evaluate the fit function along the axis taxis set and set_to allow you to forcibly set a specific symbol to a specific value –> however, this does not affect the class, but only the return value
-
fit
(set=None, set_to=None, force_analytical=False, silent=False)¶ actually run the fit
-
fitfunc
(p, x)¶ this wraps fitfunc_raw (which gives the actual form of the fit function) to take care of forced variables
-
gen_indices
(this_set, set_to)¶ pass this this_set and this_set_to parameters, and it will return: indices,values,mask indices –> gives the indices that are forced values –> the values they are forced to mask –> p[mask] are actually active in the fit
-
gen_symbolic
(function_name)¶ generates the symbolic representations the function
-
guess
(super_verbose=False)¶ provide the guess for our parameters; by default, based on pseudoinverse
-
latex
()¶ show the latex string for the function, with all the symbols substituted by their values
-
linear
(*args, **kwargs)¶ return the linear-form function, either smoothly along the fit function, or on the raw data, depending on whether or not the taxis argument is given can take optional arguments and pass them on to eval
-
makereal
()¶
-
output
(*name)¶ give the fit value of a particular symbol
-
parameter_derivatives
(xvals, set=None, set_to=None)¶ return a matrix containing derivatives of the parameters, can set dict set, or keys set, vals set_to
-
parameter_gradient
(p, x, y, sigma)¶ this gives the specific format wanted by leastsq
-
pinv
(*args, **kwargs)¶
-
remove_inactive_p
(p)¶
-
rename
(previous, new)¶
-
settoguess
()¶ a debugging function, to easily plot the initial guess
-
-
pyspecdata.core.
gensearch
(labelname, format='%0.3f', value=None, precision=None)¶ obsolete – use h5gensearch
-
pyspecdata.core.
gridandtick
(ax, rotation=(0, 0), precision=(2, 2), labelstring=('', ''), gridcolor=None, formatonly=False, fixed_y_locator=None, logarithmic=False, use_grid=True, spines=None, y=True)¶
-
pyspecdata.core.
gridon
(gridcolor=None)¶
-
pyspecdata.core.
h5addrow
(bottomnode, tablename, *args, **kwargs)¶ add a row to a table, creating it if necessary, but don’t add if the data matches the search condition indicated by match_row match_row can be either text or a dictionary – in the latter case it’s passed to h5searchstring
-
pyspecdata.core.
h5attachattributes
(node, listofattributes, myvalues)¶
-
pyspecdata.core.
h5child
(thisnode, childname, clear=False, create=None)¶ grab the child, optionally clearing it and/or (by default) creating it
-
pyspecdata.core.
h5inlist
(columnname, mylist)¶ returns rows where the column named columnname is in the value of mylist
-
pyspecdata.core.
h5join
(firsttuple, secondtuple, additional_search='', select_fields=None, pop_fields=None)¶
-
pyspecdata.core.
h5loaddict
(thisnode)¶
-
pyspecdata.core.
h5nodebypath
(h5path, force=False, only_lowest=False, check_only=False, directory='.')¶ return the node based on an absolute path, including the filename
-
pyspecdata.core.
h5remrows
(bottomnode, tablename, searchstring)¶
-
pyspecdata.core.
h5searchstring
(*args, **kwargs)¶ generate robust search strings :parameter fieldname,value: search AROUND a certain value (overcomes some type conversion issues) optional arguments are the format specifier and the fractional precision: OR :parameter field_and_value_dictionary: generate a search string that matches one or more criteria
-
pyspecdata.core.
h5table
(bottomnode, tablename, tabledata)¶ create the table, or if tabledata is None, just check if it exists
-
pyspecdata.core.
issympy
(x)¶ tests if something is sympy (based on the module name)
-
pyspecdata.core.
join_rec
()¶
-
pyspecdata.core.
lambda_rec
(myarray, myname, myfunction, *varargs)¶ make a new field “myname” which consists of “myfunction” evaluated with the fields given by “myargs” as arguments the new field is always placed after the last argument name if myname is in myargs, the original row is popped
-
pyspecdata.core.
lookup_rec
(A, B, indexpair)¶ look up information about A in table B (i.e. chemical by index, etc) indexpair is either the name of the index or – if it’s differently named – the pair of indices given in (A,B) respectively
This will just drop any fields in B that are also in A, and the output uses the first indexname
note that it it seems like the join_rec function above may be more efficient!!
-
pyspecdata.core.
lrecordarray
(*x, **kwargs)¶
-
pyspecdata.core.
lsafe
(*string, **kwargs)¶ replacement for normal lsafe – no escaping
-
pyspecdata.core.
lsafen
(*string, **kwargs)¶ see lsafe, but with an added double newline
-
pyspecdata.core.
make_bar_graph_indices
(mystructarray, list_of_text_fields, recursion_depth=0, spacing=0.1)¶ This is a recursive function that is used as part of textlabel_bargraph; it does NOT work without the sorting given at the beginning of that function
-
pyspecdata.core.
make_ndarray
(array_to_conv, name_forprint='unknown')¶
-
pyspecdata.core.
make_rec
(*args, **kwargs)¶ input,names or a single argument, which is a dictionarynstrlen = 100 gives length of the strings (which need to be specified in record arrays)nyou can also specify (especially useful with the dictionary format) the list order = [str1,str2,…] which orders the output records with the field containing str1 first, then the field containing str2, then any remaining fields
-
pyspecdata.core.
maprep
(*mylist)¶
-
pyspecdata.core.
meanstd_rec
(myarray, mylist, standard_error=False)¶ this is something like applyto_rec, except that it applies the mean and creates new rows for the “error,” where it puts the standard deviation
-
pyspecdata.core.
mybasicfunction
(first_figure=None)¶ this gives the format for doing the image thing note also nextfigure(fl,’name’) and nextfigure({‘lplotproperty’:value})
-
pyspecdata.core.
mydiff
(data, axis=-1)¶ this will replace diff with a version that has the same number of indices, with the last being the copy of the first
-
pyspecdata.core.
myfilter
(x, center=250000.0, sigma=100000.0)¶
-
class
pyspecdata.core.
nddata
(*args, **kwargs)¶ Bases:
object
This is the detailed API reference. For an introduction on how to use ND-Data, see the Main ND-Data Documentation.
-
C
¶ shortcut for copy
- ..todo::
- We should offer “N”, which generates something like a copy,
but which is sets the equivalent of “nopop”.
For example, currently, you need to do something like
d.C.argmax('t2')
, which is very inefficient, since it copies the whole array. So, instead, we should dod.N.argmax('t2')
, which tells argmax and all other functions not to overwrite “self” but to return a new object. This would cause things like “run_nopop” to become obsolete.
-
add_noise
(intensity)¶ Add Gaussian (box-muller) noise to the data.
Parameters: intensity (double OR function) – If a double, gives the standard deviation of the noise. If a function, used to calculate the standard deviation of the noise from the data: e.g. lambda x: max(abs(x))/10.
-
aligndata
(arg)¶ This is a fundamental method used by all of the arithmetic operations. It uses the dimension labels of self (the current instance) and arg (an nddata passed to this method) to generate two corresponding output nddatas that I refer to here, respectively, as A and B. A and B have dimensions that are “aligned” – that is, they are identical except for singleton dimensions (note that numpy automatically tiles singleton dimensions). Regardless of how the dimensions of self.data and arg.data (the underlying numpy data) were ordered, A.data and B.data are now ordered identically, where dimensions with the same label (.dimlabel) correspond to the same numpy index. This allows you do do math.
Note that, currently, both A and B are given a full set of axis labels, even for singleton dimensions. This is because we’re assuming you’re going to do math with them, and that the singleton dimensions will be expanded.
Parameters: arg (nddata or ndarray) – The nddata that you want to align to self. If arg is an ndarray, it will try to match dimensions to self based on the length of the dimension. Note: currently there is an issue where this will only really work for 1D data, since it first makes an nddata instance based on arg, which apparently collapses multi-D data to 1D data. Returns: - A (nddata) – realigned version of self
- B (nddata) – realigned version of arg (the argument)
-
angle
¶ Return the angle component of the data
-
argmax
(*args, **kwargs)¶ find the max along a particular axis, and get rid of that axis, replacing it with the index number of the max value
Parameters: raw_index (bool) – return the raw (ndarray) numerical index, rather than the corresponding axis value Note that the result returned is still, however, an nddata (rather than numpy ndarray) object.
-
argmin
(*axes, **kwargs)¶ If argmin(‘axisname’) find the min along a particular axis, and get rid of that axis, replacing it with the index number of the max value. If argmin(): return a dictionary giving the coordinates of the overall minimum point.
Parameters: raw_index (bool) – Return the raw (ndarray) numerical index, rather than the corresponding axis value. Note that the result returned is still, however, an nddata (rather than numpy ndarray) object.
-
axis
(axisname)¶ returns a 1-D axis for further manipulation
-
axlen
(axis)¶ return the size (length) of an axis, by name
Parameters: axis (str) – name of the axis whos length you are interested in
-
axn
(axis)¶ Return the index number for the axis with the name “axis”
This is used by many other methods. As a simple example, self.:func:axlen`(axis) (the axis length) returns ``shape(self.data)[self.axn(axis)]`
Parameters: axis (str) – name of the axis
-
cdf
(normalized=True, max_bins=500)¶ calculate the Cumulative Distribution Function for the data along axis_name
only for 1D data right now
Returns: Return type: A new nddata object with an axis labeled values, and data corresponding to the CDF.
-
check_axis_coords_errors
()¶
-
chunk
(axisin, *otherargs)¶ - “Chunking” is defined here to be the opposite of taking a direct product, increasing the number of dimensions by the inverse of the process by which taking a direct product decreases the number of dimensions. This function chunks axisin into multiple new axes arguments.:
- axesout – gives the names of the output axes shapesout – optional – if not given, it assumes equal length – if given, one of the values can be -1, which is assumed length
When there are axes, it assumes that the axes of the new dimensions are nested – e.g., it will chunk a dimension with axis: [1,2,3,4,5,6,7,8,9,10] into dimensions with axes: [0,1,2,3,4], [1,6]
- ..todo::
- Deal with this efficiently when we move to new-style axes
-
chunk_auto
(axis_name, which_field, dimname=None)¶ assuming that axis “axis_name” is currently labeled with a structured array, choose one field (“which_field”) of that structured array to generate a new dimension Note that for now, by definition, no error is allowed on the axes. However, once I upgrade to using structured arrays to handle axis and data errors, I will want to deal with that appropriately here.
-
circshift
(axis, amount)¶
-
contiguous
(lambdafunc, axis=None)¶ Return contiguous blocks that satisfy the condition given by lambdafunc
this function returns the start and stop positions along the axis for the contiguous blocks for which lambdafunc returns true Currently only supported for 1D data
Note
adapted from stackexchange post http://stackoverflow.com/questions/4494404/find-large-number-of-consecutive-values-fulfilling-condition-in-a-numpy-array
Parameters: - lambdafunc (types.FunctionType) – If only one argument (lambdafunc) is given, then lambdafunc is a function that accepts a copy of the current nddata object (self) as the argument. If two arguments are given, the second is axis, and lambdafunc has two arguments, self and the value of axis.
- axis ({None,str}) – the name of the axis along which you want to find contiguous blocks
Returns: retval – An \(N\times 2\) matrix, where the \(N\) rows correspond to pairs of axis label that give ranges over which lambdafunc evaluates to True. These are ordered according to descending range width.
Return type: ndarray
Examples
> #{{{ debugging block > sum_for_contiguous = abs(forplot).mean(‘t1’) > fl.next(“test contiguous”) > forplot = sum_for_contiguous.copy().set_error(None) > fl.plot(forplot,alpha = 0.25,linewidth = 3) > print “this is what the max looks like”,0.5*sum_for_contiguous.set_error(None).runcopy(max,’t2’) > print sum_for_contiguous > 0.5*sum_for_contiguous.runcopy(max,’t2’) > retval = sum_for_contiguous.contiguous(quarter_of_max,’t2’) > print “contiguous range / 1e6:”,retval/1e6 > for j in range(retval.shape[0]): > a,b = retval[j,:] > fl.plot(forplot[‘t2’:(a,b)]) > fl.show() > exit() > #}}}
-
contour
(labels=True, **kwargs)¶ Contour plot – kwargs are passed to the matplotlib contour function.
See docstring of figlist_var.image() for an example
-
labels
¶ Whether or not the levels should be labeled. Defaults to True
Type: boolean
-
-
convolve
(axisname, filterwidth, convfunc=<function <lambda>>)¶ Perform a convolution.
Parameters: - axisname (str) – apply the convolution along axisname
- filterwidth (double) – width of the convolution function.
- convfunc (function) – A function that takes two arguments – the first are the axis coordinates and the second is filterwidth.
Default is a normalized Gaussian of width (\(\sigma\))
filterwidth
\(\frac{1}{2 \sigma^2}\exp\left( - \frac{x^2}{2 \sigma^2} \right)\)
For example if you want a complex lorentzian with filterwidth controlled by the rate $R$,
i.e.
\(\frac{-1}{-i 2 \pi f - R}\)
then
convfunc = lambda f,R: -1./(-1j*2*pi*f-R)
-
copy
(data=True)¶ Return a full copy of this instance.
Because methods typically change the data in place, you might want to use this frequently.
Parameters: data (boolean) – Default to True. False doesn’t copy the data – this is for internal use, e.g. when you want to copy all the metadata and perform a calculation on the data.
The code for this also provides the definitive list of the nddata metadata.
-
copy_props
(other)¶ Copy all properties (see
get_prop()
) from another nddata object – note that these include properties pertaining the the FT status of various dimensions.
-
copyaxes
(other)¶
-
cropped_log
(subplot_axes=None, magnitude=4)¶ For the purposes of plotting, this generates a copy where I take the log, spanning “magnitude” orders of magnitude This is designed to be called as abs(instance).cropped_log(), so it doesn’t make a copy
-
diff
(thisaxis, backwards=False)¶
-
dot
(arg)¶ Tensor dot of self with arg – dot all matching dimension labels. This can be used to do matrix multiplication, but note that the order of doesn’t matter, since the dimensions that are contracted are determined by matching the dimension names, not the order of the dimension.
>>> a = nddata(r_[0:9],[3,3],['a','b']) >>> b = nddata(r_[0:3],'b') >>> print a.C.dot(b) >>> print a.data.dot(b.data)
>>> a = nddata(r_[0:27],[3,3,3],['a','b','c']) >>> b = nddata(r_[0:9],[3,3],['a','b']) >>> print a.C.dot(b) >>> print tensordot(a.data,b.data,axes=((0,1),(0,1)))
>>> a = nddata(r_[0:27],[3,3,3],['a','b','c']) >>> b = nddata(r_[0:9],[3,3],['a','d']) >>> print a.C.dot(b) >>> print tensordot(a.data,b.data,axes=((0),(0)))
-
extend
(axis, extent, fill_with=0, tolerance=1e-05)¶ If axis is uniformly ascending with spacing \(dx\), then extend by adding a point every \(dx\) until the axis includes the point extent. Fill the newly created datapoints with fill_with.
Parameters: - axis (str) – name of the axis to extend
- extent (double) – extend the axis axis out to this point
- fill_with (double) – fill the new data points with this value (defaults to 0)
- tolerance (double) – when checking for ascending axis labels, etc., values/differences must match to within tolerance (assumed to represent the actual precision, given various errors, etc.)
-
extend_for_shear
(altered_axis, propto_axis, skew_amount, verbose=False)¶ this is propto_axis helper function for .fourier.shear
-
fld
(dict_in, noscalar=False)¶ flatten dictionary – return list
-
fourier_shear
(altered_axis, propto_axis, by_amount, zero_fill=False, start_in_conj=False)¶ the fourier shear method – see .shear() documentation
-
fromaxis
(*args, **kwargs)¶ Generate an nddata object from one of the axis labels.
Can be used in one of several ways:
self.fromaxis('axisname')
: Returns an nddata where retval.data consists of the given axis values.self.fromaxis('axisname',inputfunc)
: use axisname as the input for inputfunc, and load the result into retval.dataself.fromaxis(inputsymbolic)
: Evaluate inputsymbolic and load the result into retval.data
Parameters: - axisname (str | list) – The axis (or list of axes) to that is used as the argument of inputfunc or the function represented by inputsymbolic. If this is the only argument, it cannot be a list.
- inputsymbolic (sympy.Expr) – A sympy expression whose only symbols are the names of axes. It is preferred, though not required, that this is passed without an axisname argument – the axis names are then inferred from the symbolic expression.
- inputfunc (function) – A function (typically a lambda function) that taxes the values of the axis given by axisname as input.
- overwrite (bool) – Defaults to False. If set to True, it overwrites self with retval.
- as_array (bool) – Defaults to False. If set to True, retval is a properly dimensioned numpy ndarray rather than an nddata.
Returns: retval – An expression calculated from the axis(es) given by axisname or inferred from inputsymbolic.
Return type: nddata | ndarray
-
ft
(axes, tolerance=1e-05, cosine=False, verbose=False, **kwargs)¶ This performs a Fourier transform along the axes identified by the string or list of strings axes.
- It adjusts normalization and units so that the result conforms to
- \(\tilde{s}(f)=\int_{x_min}^{x_max} s(t) e^{-i 2 \pi f t} dt\)
Note that, as noted in the
ift()
documentation, the inverse transform doesn’t correspond to the equivalent expression for the IFT.pre-FT, we use the axis to cyclically permute \(t=0\) to the first index
post-FT, we assume that the data has previously been IFT’d If this is the case, passing
shift=True
will cause an error If this is not the case, passingshift=True
generates a standard fftshiftshift=None
will choose True, if and only if this is not the caseParameters: - pad (int or boolean) – pad specifies a zero-filling. If it’s a number, then it gives the length of the zero-filled dimension. If it is just True, then the size of the dimension is determined by rounding the dimension size up to the nearest integral power of 2.
- automix (double) – automix can be set to the approximate frequency value. This is useful for the specific case where the data has been captured on a sampling scope, and it’s severely aliased over.
- cosine (boolean) – yields a sum of the fft and ifft, for a cosine transform
-
ft_clear_startpoints
(axis, t=None, f=None, nearest=None)¶ Clears memory of where the origins in the time and frequency domain are. This is useful, e.g. when you want to ift and center about time=0. By setting shift=True you can also manually set the points.
Parameters: - t (float, 'current', 'reset', or None) – keyword arguments t and f can be set by (1) manually setting the start point (2) using the string ‘current’ to leave the current setting a lone (3) ‘reset’, which clears the startpoint and (4) None, which will be changed to ‘current’ when the other is set to a number or ‘rest’ if both are set to None.
- t – see t
- nearest (bool) –
Shifting the startpoint can only be done by an integral number of datapoints (i.e. an integral number of dwell times, dt, in the time domain or integral number of df in the frequency domain). While it is possible to shift by a non-integral number of datapoints, this is done by applying a phase-dependent shift in the inverse domain. Applying such a axis-dependent shift can have vary unexpected effects if the data in the inverse domain is aliased, and is therefore heavily discouraged. (For example, consider what happens if we attempt to apply a frequency-dependent phase shift to data where a peak at 110 Hz is aliased and appears at the 10 Hz position.)
Setting nearest to True will choose a startpoint at the closest integral datapoint to what you have specified.
Setting nearest to False will explicitly override the safeties – essentially telling the code that you know the data is not aliased in the inverse domain and/or are willing to deal with the consequences.
-
ft_state_to_str
(*axes)¶ Return a string that lists the FT domain for the given axes.
\(u\) refers to the original domain (typically time) and \(v\) refers to the FT’d domain (typically frequency) If no axes are passed as arguments, it does this for all axes.
-
ftshift
(axis, value)¶ FT-based shift. Currently only works in time domain.
This was previously made obsolete, but is now a demo of how to use the ft properties. It is not the most efficient way to do this.
-
get_covariance
()¶ this returns the covariance matrix of the data
-
get_error
(*args)¶ get a copy of the errors either set_error(‘axisname’,error_for_axis) or set_error(error_for_data)
-
get_ft_prop
(axis, propname=None)¶ Gets the FT property given by propname. For both setting and getting, None is equivalent to an unset value if no propname is given, this just sets the FT property, which tells if a dimension is frequency or time domain
-
get_plot_color
()¶
-
get_prop
(propname=None)¶ return arbitrary ND-data properties (typically acquisition parameters etc.) by name (propname)
In order to allow ND-data to store acquisition parameters and other info that accompanies the data, but might not be structured in a gridded format, nddata instances always have a other_info dictionary attribute, which stores these properties by name.
If the property doesn’t exist, this returns None.
Parameters: propname (str) – Name of the property that you’re want returned. If this is left out or set to “None” (not given), the names of the available properties are returned. If no exact match is found, and propname contains a . or * or [, it’s assumed to be a regular expression. If several such matches are found, the error message is informative.
Todo
have it recursively search dictionaries (e.g. bruker acq)
Returns: Return type: The value of the property (can by any type) or None if the property doesn’t exist.
-
get_units
(*args)¶
-
getaxis
(axisname)¶
-
getaxisshape
(axisname)¶
-
gnuplot_save
(filename)¶
-
hdf5_write
(h5path, directory='.')¶ Write the nddata to an HDF5 file.
h5path is the name of the file followed by the node path where you want to put it – it does not include the directory where the file lives. The directory can be passed to the directory argument.
Parameters: - h5path (str) – The name of the file followed by the node path where you want to put it – it does not include the directory where the file lives. (Because HDF5 files contain an internal directory-like group structure.)
- directory (str) – the directory where the HDF5 file lives.
-
histogram
(**kwargs)¶
-
human_units
()¶
-
ift
(axes, n=False, tolerance=1e-05, verbose=False, **kwargs)¶ This performs a Fourier transform along the axes identified by the string or list of strings axes.
- It adjusts normalization and units so that the result conforms to
- \(s(t)=t_{dw} \int_{x_min}^{x_max} \tilde{s}(t) e^{i 2 \pi f t} df\)
Where \(t_{dw}=\frac{1}{\Delta f}\), is the dwell time (with \(\Delta f\) the spectral width).
Why do we do this? Note that while the analytical integral this corresponds to is normalized, performing
ft()
followed byift()
on a discrete sequence is NOT completely invertible (due to integration of the implied comb function??), and would require division by a factor of $Delta f$ (the spectral width) in order to retrieve the original functionpre-IFT, we use the axis to cyclically permute $f=0$ to the first index
post-IFT, we assume that the data has previously been FT’d If this is the case, passing
shift=True
will cause an error If this is not the case, passingshift=True
generates a standard ifftshiftshift=None
will choose True, if and only if this is not the caseParameters: pad (int or boolean) – pad specifies a zero-filling. If it’s a number, then it gives the length of the zero-filled dimension. If it is just True, then the size of the dimension is determined by rounding the dimension size up to the nearest integral power of 2. It uses the start_time ft property to determine the start of the axis. To do this, it assumes that it is a stationary signal (convolved with infinite comb function). The value of start_time can differ from by a non-integral multiple of \(\Delta t\), though the routine will check whether or not it is safe to do this.
- ..note ::
- In the code, this is controlled by p2_post (the integral \(\Delta t\) and p2_post_discrepancy – the non-integral.
-
imag
¶ Return the imag component of the data
-
indices
(axis_name, values)¶ Return a string of indeces that most closely match the axis labels corresponding to values. Filter them to make sure they are unique.
-
inhomog_coords
(direct_dim, indirect_dim, tolerance=1e-05, method='linear', plot_name=None, fl=None, debug_kwargs={})¶ Apply the “inhomogeneity transform,” which rotates the data by \(45^{\circ}\), and then mirrors the portion with \(t_2<0\) in order to transform from a \((t_1,t_2)\) coordinate system to a \((t_{inh},t_{homog})\) coordinate system.
Parameters: - direct_dim (str) – Label of the direct dimension (typically \(t_2\))
- indirect_dim (str) – Label of the indirect dimension (typically \(t_1\))
- method ('linear', 'fourier') – The interpolation method used to rotate the data and to mirror the data. Note currently, both use a fourier-based mirroring method.
- plot_name (str) – the base name for the plots that are generated
- fl (figlist_var) –
- debug_kwargs (dict) –
with keys:
correct_overlap: if False, doesn’t correct for the overlap error that occurs during mirroring
-
integrate
(thisaxis, backwards=False, cumulative=False)¶ this performs an integration – which is similar to a sum, except that it takes the axis into account, i.e., it performs: $int f(x) dx$ rather than $sum_i f(x_i)$
Gaussian quadrature, etc, is planned for a future version.
-
interp
(axis, axisvalues, past_bounds=None, return_func=False, **kwargs)¶ interpolate data values given axis values
Parameters: return_func (boolean) – defaults to False. If True, it returns a function that accepts axis values and returns a data value.
-
invinterp
(axis, values, **kwargs)¶ interpolate axis values given data values
-
item
()¶ like numpy item – returns a number when zero-dimensional
-
labels
(*args) label the dimensions, given in listofstrings with the axis labels given in listofaxes – listofaxes must be a numpy array; you can pass either a dictionary or a axis name (string)/axis label (numpy array) pair
-
like
(value)¶ provide “zeros_like” and “ones_like” functionality
Parameters: value (float) – 1 is “ones_like” 0 is “zeros_like”, etc.
-
linear_shear
(along_axis, propto_axis, shear_amnt, zero_fill=True)¶ the linear shear – see self.shear for documentation
-
matchdims
(other)¶ add any dimensions to self that are not present in other
-
matrices_3d
(also1d=False, invert=False, max_dimsize=1024, downsample_self=False)¶ returns X,Y,Z,x_axis,y_axis matrices X,Y,Z, are suitable for a variety of mesh plotting, etc, routines x_axis and y_axis are the x and y axes
-
mayavi_surf
()¶ use the mayavi surf function, assuming that we’ve already loaded mlab during initialization
-
mean
(*args, **kwargs)¶ Take the mean and set the error to the standard deviation
Parameters: return_error (bool) – whether or note to return the standard deviation as an error
-
mean_all_but
(listofdims)¶ take the mean over all dimensions not in the list
-
mean_nopop
(axis)¶
-
mean_weighted
(axisname)¶ perform the weighted mean along axisname (use $sigma$ from $sigma = $self.get_error() do generate $1/sigma$ weights) for now, it clears the error of self, though it would be easy to calculate the new error, since everything is linear
unlike other functions, this creates working objects that are themselves nddata objects this strategy is easier than coding out the raw numpy math, but probably less efficient
-
meshplot
(stride=None, alpha=1.0, onlycolor=False, light=None, rotation=None, cmap=<matplotlib.colors.LinearSegmentedColormap object>, ax=None, invert=False, **kwargs)¶ takes both rotation and light as elevation, azimuth only use the light kwarg to generate a black and white shading display
-
mkd
(*arg, **kwargs)¶ make dictionary format
-
multimin
(minfunc, axisname, filterwidth, numberofmins)¶
-
name
(*arg)¶ args: .name(newname) –> Name the object (for storage, etc) .name() –> Return the name
-
nnls
(dimname, newaxis_dict, kernel_func, l=0)¶ Perform regularized non-negative least-squares “fit” on self.
Capable of solving for solution in 1 or 2 dimensions.
We seek to minimize \(Q = \| Ax - b \|_2 + \|\lambda x\|_2\) in order to obtain solution vector \(x\) subject to non-negativity constraint given input matrix \(A\), the kernel, and input vector \(b\), the data.
The first term assesses agreement between the fit \(Ax\) and the data \(b\), and the second term accounts for noise with the regularization parameter \(\lambda\) according to Tikhonov regularization.
To perform regularized minimization in 2 dimensions, set l to :str:`BRD` and provide a tuple of parameters :str:`dimname`, :nddata:`newaxis_dict`, and :function:`kernel_func`. Algorithm described in Venkataramanan et al. 2002 is performed which determines optimal \(\lambda\) for the data (DOI:10.1109/78.995059).
See: Wikipedia page on NNLS, Wikipedia page on Tikhonov regularization
Parameters: - dimname (str) – Name of the “data” dimension that is to be replaced by a distribution (the “fit” dimension); e.g. if you are regularizing a set of functions \(\exp(-\tau*R_1)\), then this is \(\tau\)
- newaxis_dict (dict or nddata) – a dictionary whose key is the name of the “fit” dimension (\(R_1\) in the example above) and whose value is an array with the new axis labels. OR this can be a 1D nddata – if it has an axis, the axis will be used to create the fit axis; if it has no axis, the data will be used
- kernel_func (function) – a function giving the kernel for the regularization.
The first argument is the “data” variable
and the second argument is the “fit” variable
(in the example above, this would be something like
lambda x,y: exp(-x*y)
) - l (double (default 0) or str) – the regularization parameter \(lambda\) – if this is set to 0, the algorithm reverts to standard nnls. If this is set to :str:`BRD`, then algorithm expects tuple of each parameter described above in order to perform a 2-dimensional fit.
Returns: The regularized result. For future use, both the kernel (as an nddata, in a property called “nnls_kernel”) and the residual (as an nddata, in a property called “nnls_residual”) are stored as properties of the nddata. The regularized dimension is always last (innermost). If :str:`BRD` is specified, then the individual, uncompressed kernels \(K_{1}\) and \(K_{2}\) are returned as properties of the nddata “K1” and “K2” respectively. The number of singular values used to compressed each kernel is returned in properties of the nddata called, respectively, “s1” and “s2”.
Return type: self
-
normalize
(axis, first_figure=None)¶
-
oldtimey
(alpha=0.5, ax=None, linewidth=None, sclinewidth=20.0, light=True, rotation=None, invert=False, **kwargs)¶
-
plot_labels
(labels, fmt=None, **kwargs_passed)¶ this only works for one axis now
-
polyfit
(axis, order=1, force_y_intercept=None)¶ polynomial fitting routine – return the coefficients and the fit ..note:
later, should probably branch this off as a new type of fit class- ..warning:
- for some reason, this version doesn’t use orthogonal polynomials, as the numpy routine does – we had diagnosed and determined that that creates noticeably different results, so fix that here.
Parameters: - axis (str) – name of the axis that you want to fit along (not sure if this is currently tested for multi-dimensional data, but the idea should be that multiple fits would be returned.)
- order (int) – the order of the polynomial to be fit
- force_y_intercept (double or None) – force the y intercept to a particular value (e.g. 0)
Returns: - c (ndarray) – a standard numpy array containing the coefficients (in ascending polynomial order)
- formult (nddata) – an nddata containing the result of the fit
-
popdim
(dimname)¶
-
random_mask
(axisname, threshold=None, inversion=False)¶ generate a random mask with about ‘threshold’ of the points thrown out
-
real
¶ Return the real component of the data
-
register_axis
(arg)¶ Interpolate the data so that the given axes are in register with a set of specified values. Does not change the spacing of the axis labels.
It finds the axis label position that is closest to the values given in arg, then interpolates (Fourier/sinc method) the data onto a new, slightly shifted, axis that passes exactly through the value given. To do this, it uses
.ft_clear_startpoints()
and uses.set_ft_prop()
to override the “not aliased” flag.Parameters: arg (dict (key,value = str,double)) – A list of the dimensions that you want to place in register, and the values you want them registered to.
-
rename
(previous, new)¶
-
reorder
(*axes, **kwargs)¶ Reorder the dimensions the first arguments are a list of dimensions
Parameters: - *axes (str) – Accept any number of arguments that gives the dimensions, in the order that you want thee.
- first (bool) – (default True) Put this list of dimensions first, while False puts them last (where they then come in the order given).
-
replicate_units
(other)¶
-
repwlabels
(axis)¶
-
retaxis
(axisname)¶
-
run
(*args)¶ run a standard numpy function on the nddata:
d.run(func,'axisname')
will run function func (e.g. a lambda function) along axis named ‘axisname’d.run(func)
will run function func on the datain general: if the result of func reduces a dimension size to 1, the ‘axisname’ dimension will be “popped” (it will not exist in the result) – if this is not what you want, use
run_nopop
-
run_avg
(thisaxisname, decimation=20, centered=False)¶ a simple running average
-
run_nopop
(func, axis)¶
-
runcopy
(*args)¶
-
secsy_transform
(direct_dim, indirect_dim, has_indirect=True, method='fourier', truncate=True)¶ Shift the time-domain data backwards by the echo time.
As opposed to
secsy_transform_manual
, this calls on onskew
, rather than directly manipulating the phase of the function, which can lead to aliasing.Parameters: - has_indirect (bool) –
(This option is largely specific to data loaded by
acert_hdf5
)Does the data actually have an indirect dimension? If not, assume that there is a constant echo time, that can be retrieved with
.get_prop('te')
. - truncate (bool) – If this is set, register_axis <pyspecdata.axis_manipulation.register_axis> to \(t_{direct}=0\), and then throw out the data for which \(t_{direct}<0\).
- method (str) – The shear method (linear or fourier).
- has_indirect (bool) –
-
secsy_transform_manual
(direct_dim, indirect_dim, has_indirect=True, truncate=False)¶ Shift the time-domain data backwards by the echo time. As opposed to
secsy_transform
, this directlly manipulates the phase of the function, rather than calling onskew
.Parameters: - has_indirect (bool) –
(This option is largely specific to data loaded by
acert_hdf5
)Does the data actually have an indirect dimension? If not, assume that there is a constant echo time, that can be retrieved with
.get_prop('te')
. - truncate (bool) – If this is set, register_axis <pyspecdata.axis_manipulation.register_axis> to \(t_{direct}=0\), and then throw out the data for which \(t_{direct}<0\).
- has_indirect (bool) –
-
set_error
(*args)¶ set the errors: either
set_error(‘axisname’,error_for_axis) or set_error(error_for_data)
error_for_data can be a scalar, in which case, all the data errors are set to error_for_data
-
set_ft_prop
(axis, propname=None, value=True)¶ Sets the FT property given by propname. For both setting and getting, None is equivalent to an unset value if propname is a boolean, and value is True (the default), it’s assumed that propname is actually None, and that value is set to the propname argument (this allows us to set the FT property more easily)
-
set_plot_color
(thiscolor)¶
-
set_prop
(*args)¶ set a ‘property’ of the nddata This is where you can put all unstructured information (e.g. experimental parameters, etc)
-
set_to
(otherinst)¶ Set data inside the current instance to that of the other instance.
Goes through the list of attributes specified in copy, and assigns them to the element of the current instance.
This is to be used:
- for constructing classes that inherit nddata with additional methods.
- for overwriting the current data with the result of a slicing operation
-
set_units
(*args)¶
-
setaxis
(*args)¶ set or alter the value of the coordinate axis
Can be used in one of several ways:
self.setaxis('axisname', values)
: just sets the valuesself.fromaxis('axisname',inputfunc)
: take the existing function, apply inputfunc, and replaceself.fromaxis(inputsymbolic)
: Evaluate inputsymbolic and load the result into the axes, appropriately
-
shear
(along_axis, propto_axis, shear_amnt, zero_fill=True, start_in_conj=False, method='linear')¶ Shear the data \(s\):
\(s(x',y,z) = s(x+ay,y,z)\)
where \(x\) is the altered_axis and \(y\) is the propto_axis. (Actually typically 2D, but \(z\) included just to illustrate other dimensions that aren’t involved)
Parameters: - method ({'fourier','linear'}) –
- fourier
- Use the Fourier shift theorem (i.e., sinc interpolation). A
shear is equivalent to the following in the conjugate domain:
..math: tilde{s}(f_x,f’_y,z) = tilde{s}(f_x,f_y-af_x,f_z)
Because of this, the algorithm also automatically extend`s the data in `f_y axis. Equivalently, it increases the resolution (decreases the interval between points) in the propto_axis dimension. This prevents aliasing in the conjugate domain, which will corrupt the data w.r.t. successive transformations. It does this whether or not zero_fill is set (zero_fill only controls filling in the “current” dimension)
- linear
- Use simple linear interpolation.
- altered_axis (str) – The coordinate for which data is altered, i.e. ..math: x such that ..math: f(x+ay,y).
- by_amount (double) – The amount of the shear (..math: a in the previous)
- propto_axis (str) – The shift along the altered_axis dimension is proportional to the shift along propto_axis. The position of data relative to the propto_axis is not changed. Note that by the shift theorem, in the frequency domain, an equivalent magnitude, opposite sign, shear is applied with the propto_axis and altered_axis dimensions flipped.
- start_in_conj ({False, True}, optional) –
Defaults to False
For efficiency, one can replace a double (I)FT call followed by a shear call with a single shear call where start_in_conj is set.
self before the call is given in the conjugate domain (i.e., \(f\) vs. \(t\)) along both dimensions from the one that’s desired. This means: (1) self after the function call transformed into the conjugate domain from that before the call and (2) by_amount, altered_axis, and propto_axis all refer to the shear in the conjugate domain that the data is in at the end of the function call.
- method ({'fourier','linear'}) –
-
smoosh
(dimstocollapse, dimname=0, noaxis=False)¶ Collapse (smoosh) multiple dimensions into one dimension.
Parameters: - dimstocollapse (list of strings) – the dimensions you want to collapse to one result dimension
- dimname (None, string, integer (default 0)) –
if dimname is:
- None: create a new (direct product) name,
- a number: an index to the
dimstocollapse
list. The resulting smooshed dimension will be nameddimstocollapse[dimname]
. Because the default is the number 0, the new dimname will be the first dimname given in the list. - a string: the name of the resulting smooshed dimension (can be part of the
dimstocollapse
list or not)
- noaxis (bool) – if set, then just skip calculating the axis for the new dimension, which otherwise is typically a complicated record array
Returns: - self (nddata) – the dimensions dimstocollapse are smooshed into a single dimension, whose name is determined by dimname. The axis for the resulting, smooshed dimension is a structured array consisting of two fields that give the labels along the original axes.
- ..todo:: – when we transition to axes that are stored using a slice/linspace-like format, allow for smooshing to determine a new axes that is standard (not a structured array) and that increases linearly.
-
sort
(axisname, reverse=False)¶
-
sort_and_xy
()¶
-
squeeze
(return_dropped=False)¶ squeeze singleton dimensions
Parameters: return_dropped (bool (default False)) – return a list of the dimensions that were dropped as a second argument Returns: - self
- return_dropped (list) – (optional, only if return_dropped is True)
-
sum
(axes)¶
-
sum_nopop
(axes)¶
-
to_ppm
()¶ Function that converts from Hz to ppm using Bruker parameters
Todo
Figure out what the units of PHC1 in Topspin are (degrees per what??), and apply those as well.
make this part of an inherited bruker class
-
unitify_axis
(axis_name, is_axis=True)¶ this just generates an axis label with appropriate units
-
units_texsafe
(*args)¶
-
unset_prop
(arg)¶ remove a ‘property’
-
want_to_prospa_decim_correct
= False¶
-
waterfall
(alpha=0.3, ax=None, rotation=None, color='b', edgecolor='k')¶
-
-
class
pyspecdata.core.
nddata_hdf5
(pathstring, directory='.')¶ Bases:
pyspecdata.core.nddata
-
pyspecdata.core.
ndgrid
(*input)¶
-
class
pyspecdata.core.
ndshape
(*args)¶ Bases:
pyspecdata.ndshape.ndshape_base
The ndshape class, including the allocation method
-
alloc
(dtype='complex128', labels=False, format=0)¶ Use the shape object to allocate an empty nddata object.
Parameters: - labels – Needs documentation
- format (0, 1, or None) – What goes in the allocated array. None uses numpy empty.
-
-
pyspecdata.core.
newcol_rec
(A, new_dtypes)¶ add new, empty (i.e. random numbers) fields to A, as given by new_dtypes –> note that there are deeply nested numpy functions to do this, but the options are confusing, and I think the way these work is efficient
-
pyspecdata.core.
nextfigure
(figurelist, name)¶ obsolete – now use class
-
pyspecdata.core.
normal_attrs
(obj)¶
-
pyspecdata.core.
obs
(*x)¶
-
pyspecdata.core.
obsn
(*x)¶
-
pyspecdata.core.
othergridandtick
(ax, rotation=(0, 0), precision=(2, 2), labelstring=('', ''), gridcolor=None, y=True, x=True, spines=None)¶
-
pyspecdata.core.
pinvr
(C, alpha)¶
-
pyspecdata.core.
plot
(*args, **kwargs)¶ The base plotting function that wraps around matplotlib to do a couple convenient things.
Parameters: - label_format_string (str) – If supplied, it formats the values of the other dimension to turn them into a label string.
- human_units (bool) –
-
pyspecdata.core.
plot_color_counter
(*args, **kwargs)¶ Try not to use this function any more – the version-to-version support for capturing and setting color cycles in matplotlib is very very bad. (And, the cycler object in newer versions of matplolib is confusing.) So, just import cycle from itertools, and use it to build a cycle that you directly call to set your properties.
Note
previous description:
if passed an argument: make it so that the next line will have the properties given by the argument
if not passed an argument: just return the current plot properties,so that I can cycle back to it
-
pyspecdata.core.
plot_label_points
(x, y, labels, **kwargs_passed)¶
-
pyspecdata.core.
plot_updown
(data, axis, color1, color2, symbol='', **kwargs)¶
-
pyspecdata.core.
reorder_rec
(myarray, listofnames, first=True)¶
-
pyspecdata.core.
sech
(x)¶
-
pyspecdata.core.
showtype
(x)¶
-
pyspecdata.core.
spectrogram
(waveform, f_start, f_stop, npoints_fdom=40, tdom_div=2)¶
-
pyspecdata.core.
sqrt
(arg)¶
-
class
pyspecdata.core.
testclass
¶
-
pyspecdata.core.
text_on_plot
(x, y, thistext, coord='axes', **kwargs)¶
-
pyspecdata.core.
textlabel_bargraph
(mystructarray, othersort=None, spacing=0.1, ax=None, tickfontsize=8)¶
-
pyspecdata.core.
unmake_ndarray
(array_to_conv, name_forprint='unknown')¶ Convert this item to an ndarray
-
pyspecdata.core.
whereblocks
(a)¶ returns contiguous chunks where the condition is true but, see the “contiguous” method, which is more OO
pyspecdata.datadir module¶
Allows the user to run the same code on different machines, even though the location of the raw spectral data might change.
This is controlled by the ~/.pyspecdata
or ~/_pyspecdata
config file.
-
class
pyspecdata.datadir.
MyConfig
¶ Bases:
object
Provides an easy interface to the pyspecdata configuration file. Only one instance _my_config should be created – this instance is used by the other functions in this module.
-
config_vars
= None¶ The dictionary that stores the current settings – keep these in a dictionary, which should be faster than reading from environ, or from a file.
-
get_setting
(this_key, environ=None, default=None, section='General')¶ Get a settings from the “General” group. If the file does not exist, or the option is not set, then set the option, creating the file as needed. The option is determined in the following order:
- The value in the config_vars dictionary.
- The value of the environment variable named environ.
- The value stored in the configuration file at
~/.pyspecdata
(~/_pyspecdata
on Windows). - The value given by the default argument. If default is
None
, then returnNone
.
Parameters: - this_key (str) – The name of the settings that we want to retrieve.
- environ (str) – If the value corresponding to this_key is not present in the self.config_vars dictionary, look for an environment variable called environ that stores the value of this_key. If this is not set, it’s set from the config file or the default argument.
- default (str) –
If the value for this_key is not in self.config_vars, not in the environment variable called environ, and not in the config file, set it to this value, and ask for user response to confirm. Then, set the config file value, the environment, and the self.config_vars value to the result.
For platform compatibility, leading period characters are converted to self.hide_start, and ‘/’ are converted to os.path.sep.
- section (str) – Look in this section of the config file.
Returns: Return type: The value corresponding to this_key.
-
hide_start
= None¶ This filename prefix denotes a configuration file on the OS.
-
set_setting
(this_section, this_key, this_value)¶ set this_key to this_value inside section this_section, creating it if necessary
-
-
pyspecdata.datadir.
dirformat
(file)¶
-
pyspecdata.datadir.
getDATADIR
(*args, **kwargs)¶ Returns the base directory where you put all your data. If arguments are passed, it returns the directory underneath the data directory, ending in a trailing (back)slash
It is determined by a call to MyConfig.get_setting with the setting name data_directory and the environment variable set to
PYTHON_DATA_DIR
.Parameters: exp_type (str) – A string identifying the experiment type. It searches for exp_type in this order:
- Look in the
ExpTypes
section of the config file. - Note that by using this, you can store data in locations other
- than your main data directory.
For example, consider the following section of the
~/.pyspecdata
config file:` [ExpTypes] alternate_base = /opt/other_data alternate_type_one = %(alternate_base)s/type_one `
which would find data with exp_typealternate_type_one
in/opt/other_data/type_one
.
- Look in the
- use os.walk to search for a directory with this name
- inside the directory identified by experimental_data. excluding things that start with ‘.’, ‘_’ or containing ‘.hfssresults’, always choosing the thing that’s highest up in the tree. If it doesn’t find a directory inside experimental_data, it will search inside all the directories already listed in ExpTypes. Currently, in both attempts, it will only walk 2 levels deep (since NMR directories can be rather complex, and otherwise it would take forever).
-
pyspecdata.datadir.
get_notebook_dir
(*args)¶ Returns the notebook directory. If arguments are passed, it returns the directory underneath the notebook directory, ending in a trailing (back)slash
It is determined by a call to MyConfig.get_setting with the environment variable set to
PYTHON_NOTEBOOK_DIR
and default~/notebook
.
-
pyspecdata.datadir.
grab_data_directory
()¶
pyspecdata.figlist module¶
Contains the figure list class
The figure list gives us three things:
Automatically handle the display and scaling of nddata units.
Refer to plots by name, rather than number (matplotlib has a mechanism for this, which we ignore)
A “basename” allowing us to generate multiple sets of plots for different datasets – e.g. 5 plots with 5 names plotted for 3 different datasets and labeled by 3 different basenames to give 15 plots total
Ability to run the same code from the command line or from within a python environment inside latex. * this is achieved by choosing figlist (default gui) and figlistl (inherits
from figlist – renders to latex – the
figlist.show()
method is changed)- potential planned future ability to handle html
Ability to handle mayavi plots and matplotlib plots (switch to glumpy, etc.?) * potential planned future ability to handle gnuplot
Todo
Currently the “items” that the list tracks correspond to either plot formatting directives (see figlist.setprops()
), text, or figures.
We should scrap most elements of the current implementation of figlist and rebuild it
currently the figlist is set up to use a context block. We will not only keep this, but also make it so the individual axes. Syntax (following a
fl = figlist_var()
should look like this:with fl['my plot name'] as p:
and contents of the block would then bep.plot(...)
, etc.define an “organization” function of the figlist block. This allows us to use standard matplotlib commands to set up and organize the axes, using standard matplotlib commands (twinx, subplot, etc.)
figlist will still have a “next” function, but its purpose will be to simply: * grab the current axis using matplotlib gca() (assuming the id of the axis isn’t yet assigned to an existing figlist_axis – see below) * otherwise, if the name argument to “next” has not yet been called,
call matplotlib’s figure(), followed by subplot(111), then do the previous bullet point
- the next function is only intended to be called explicitly from within the organization function
figlist will consist simply of a list of figlist_axis objects (a new object type), which have the following attributes: * type – indicating the type of object:
- axis (default)
- text (raw latex (or html))
- H1 (first level header – translates to latex section)
- H2 (second level…)
the name of the plot
a matplotlib or mayavi axes object
the units associated with the axes
a collection.OrderedDict giving the nddata that are associated with the plot, by name. * If these do not have a name, they will be automatically assigned a name. * The name should be used by the new “plot” method to generate
the “label” for the legend, and can be subsequently used to quickly replace data – e.g. in a Qt application.
a dictionary giving any arguments to the pyspecdata.core.plot (or countour, waterfall, etc) function
the title – by default the name of the plot – can be a setter
the result of the id(…) function, called on the axes object –> this can be used to determine if the axes has been used yet
do not use check_units – the plot method (or contour, waterfall, etc.) will only add the nddata objects to the OrderedDict, add the arguments to the argument dictionary, then exit * In the event that more than one plot method is called, the name of the underlying nddaata should be changed
a boolean legend_suppress attribute
a boolean legend_internal attribute (to place the legend internally, rather than outside the axis)
a show method that is called by the figlistl show method. This will determine the appropriate units and use them to determine the units and scale of the axes, and then go through and call pyspecdata.core.plot on each dataset (in matplotlib, this should be done with a formatting statement rather than by manipulating the axes themselves) and finally call autolegend, unless the legend is supressed
The “plottype” (currently an argument to the plot function) should be an attribute of the axis object
-
class
pyspecdata.figlist.
figlist
(*arg, **kwargs)¶ Bases:
object
-
basename
¶ A basename that can be changed to generate different sets of figures with different basenames. For example, this is useful if you are looping over different sets of data, and generating the same set of figures for each set of data (which would correspond to a basename).
Type: str
-
figurelist
¶ A list of the figure names
Type: list
-
figdict
¶ A dictionary containing the figurelist and the figure numbers or objects that they correspond to. Keys of this dictionary must be elements of figurelist.
Type: dict
-
propdict
¶ Maintains various properties for each element in figurelist. Keys of this dictionary must be elements of figurelist.
Type: dict
-
adjust_spines
(spines)¶
-
check_units
(testdata, x_index, y_index, verbose=False)¶
-
generate_ticks
(plotdata, axes, rescale, z_norm=None, y_rescale=1, text_scale=0.05, verbose=False, follow_surface=False, lensoffset=0.005, line_width=0.001, tube_radius=0.001, fine_grid=False)¶ generate 3d ticks and grid for mayavi
-
get_fig_number
(name)¶
-
grid
()¶
-
header
(number_above, input_string)¶
-
image
(A, **kwargs)¶ Called as fl.image() where fl is the figlist_var object
Note that this code just wraps the figlist properties, and the heavy lifting is done by the image( function. Together, the effect is as follows:
check_units converts to human-readable units, and makes sure they match the units already used in the plot.
if A has more than two dimensions, the final dimension in A.dimlabels is used as the column dimension, and a direct-product of all non-column dimensions (a Kronecker product, such that the innermost index comes the latest in the list A.dimlabels) is used as the row dimension. A white/black line is drawn after the innermost index used to create the direct product is finished iterating.
If A consists of complex data, then an HSV plot (misnomer, actually an HV plot) is used: - convert to polar form: \(z=\rho \exp(i \phi)\) - \(\phi\) determines the color (Hue)
- Color wheel is cyclical, like \(\exp(i \phi)\)
- red is taken as \(\phi=0\), purely real and positive
- green-blue is \(pi\) radians out of phase with red and therefore negative real
\(\rho\) determines the intensity (value) - Depending on whether or not black is set (either as a
keyword argument, or fl.black, the background will be black with high \(\rho\) values “lit up” (intended for screen plotting) or the background will be white with the high \(\rho\) values “colored in” (intended for printing)
If the data type (dtype) of the data in A is real (typically achieved by calling abs(A) or A.runcopy(real)), then A is plotted with a colormap and corresponding colorbar.
If no title has been given, it’s set to the name of the current plot in the figurelist
-
x
¶ If A is a numpy array, then this gives the values along the x axis (columns). Defaults to the size of the array. Not used if A is nddata.
Type: Optional[double] or Optional[scalar]
-
y
¶ If A is a numpy array, then this gives the values along the y axis (columns). Defaults to the size of the array. Not used if A is nddata.
Type: Optional[double] or Optional[scalar]
-
x_first
¶ Since it’s designed to represent matrices, an image plot by defaults is “transposed” relative to all other plots. If you want the first dimension on the x-axis (e.g., if you are plotting a contour plot on top of an image), then set x_first to True.
Type: boolean
-
spacing
¶ Determines the size of the white/black line drawn Defaults to 1
Type: integer
-
ax
¶ the Axis object where the plot should go.
Type: matplotlib Axes
-
all remaning
are passed through to matplotlib imshow
-
origin
¶ upper and lower are passed to matplotlib. Flip is for 2D nmr, and flips the data manually.
Type: {‘upper’, ‘lower’, ‘flip’}
-
.. code-block:: python
from pyspecdata import * fl = figlist_var()
t = r_[-1:1:300j] x = nddata(t,[-1],[‘x’]).labels(‘x’,t) y = nddata(t,[-1],[‘y’]).labels(‘y’,t)
z = x**2 + 2*y**2 print “dimlabels of z:”,z.dimlabels
fl.next(‘image with contours’) fl.image(z,x_first = True) # x_first is needed to align # with the contour plot z.contour(colors = ‘w’,alpha = 0.75)
fl.next(‘simple plot’) # just to show that x is the same # here as well fl.plot(z[‘y’:(0,0.01)])
fl.show(‘compare_image_contour_150911.pdf’)
-
label_point
(data, axis, value, thislabel, show_point=True, xscale=1, **new_kwargs)¶ only works for 1D data: assume you’ve passed a single-point nddata, and label it
xscale gives the unit scaling
..todo:
Improve the unit scaling, so that this would also work. Allow it to include a format string that would use the value.
Parameters: show_point (bool) – Defaults to True. Actually generate a point (circle), vs. just the label.
-
marked_text
(marker, input_text='', sep='\n')¶ Creates a named marker where we can place text. If marker has been used, goes back and places text there.
-
mesh
(plotdata, Z_normalization=None, equal_scale=True, lensoffset=0.001, show_contours=False, grey_surf=False, **kwargs)¶
-
next
(input_name, legend=False, boundaries=None, twinx=None, **kwargs)¶ Switch to the figure given by input_name, which is used not only as a string-based name for the figure, but also as a default title and as a base name for resulting figure files.
In the future, we actually want this to track the appropriate axis object!
Parameters: - legend (bool) – If this is set, a legend is created outside the figure.
- twinx ({0,1}) –
1: plots on an overlayed axis (the matplotlib twinx) whose y axis is labeled on the right when you set this for the first time, you can also set a color kwarg that controls the coloring of the right axis. 0: used to switch back to the left (default) axis - boundaries – need to add description
- kwargs (dict) – Any other keyword arguments are passed to the matplotlib (mayavi) figure() function that’s used to switch (create) figures.
-
phaseplot_finalize
()¶ Performs plot decorations that are typically desired for a manual phasing plot. This assumes that the
y
-axis is given in units of half-cycles ($pi$ radians).
-
plot
(*args, **kwargs)¶ Parameters: - linestyle ({':','--','.','etc.'}) – the style of the line
- plottype ({'semilogy','semilogx','loglog'}) – Select a logarithmic plotting style.
- nosemilog (True) – Typically, if you supply a log-spaced axis, a semilogx plot will be automatically selected. This overrides that behavior. Defaults to False.
-
pop_marker
()¶
-
push_marker
()¶
-
setprops
(**kwargs)¶
-
show
(*args, **kwargs)¶
-
show_prep
()¶
-
text
(mytext)¶
-
twinx
(autopad=False, orig=False, color=None)¶
-
use_autolegend
(value=None)¶ No argument sets to true if it’s not already set
-
pyspecdata.fornotebook module¶
This provides figlistl
, the Latex figure list.
Any other functions here are helper functions for the class.
figlist
is generally not chosen manually,
but figlist_var
will be assigned to figlistl
when
python code is embedded in a python environment inside latex.
-
pyspecdata.fornotebook.
calcdistance
(freq, optdistance)¶
-
pyspecdata.fornotebook.
calcfield
(elfreq, elratio=28.113, nmrelratio=1.5167)¶
-
pyspecdata.fornotebook.
calcfielddata
(freq, substance, spec='')¶
-
pyspecdata.fornotebook.
clear_local
(inputdata=[])¶
-
pyspecdata.fornotebook.
cpmgs
(exp, number, tau=None, alpha=None, alphaselect=None, first=False)¶
-
pyspecdata.fornotebook.
cpmgseries
(filename, plotlabel, tau=None, alpha=None, alphaselect=None)¶
-
pyspecdata.fornotebook.
dict_to_txt
(mydict, file='data.txt')¶
-
pyspecdata.fornotebook.
dprint
(*stuff)¶
-
pyspecdata.fornotebook.
ernstangle
(pulsetime=None, pulsetip=None, Tr=None, T1=None)¶
-
pyspecdata.fornotebook.
esr_saturation
(file, powerseries, smoothing=0.2, threshold=0.8, figname=None, hn23adjustment=1.0, show_avg=False)¶
-
pyspecdata.fornotebook.
figlist
¶ alias of
pyspecdata.fornotebook.figlistl
-
pyspecdata.fornotebook.
figlisterr
(figurelist, *args, **kwargs)¶
-
class
pyspecdata.fornotebook.
figlistl
(*args, **kwargs)¶ Bases:
pyspecdata.core.figlist
-
obs
(*args)¶
-
obsn
(*args)¶
-
show
(string, line_spacing=True, **kwargs)¶ latexify the series of figures, where “string” gives the base file name
Parameters: line_spacing (bool) – if false, suppress empty lines between output
-
-
pyspecdata.fornotebook.
lplot
(fname, width=0.33, figure=False, dpi=72, grid=False, alsosave=None, gensvg=False, print_string=None, centered=False, equal_aspect=False, autopad=True, bytextwidth=None, showbox=True, boundaries=True, genpng=False, mlab=False, fig=None, verbose=False)¶ used with python.sty instead of savefig
by default, if width is less than 1, it’s interpreted as bytextwidth = True (i.e. width given as a fraction of the linewidth) if it’s greater than, the width is interpreted in inches.
-
pyspecdata.fornotebook.
lplotfigures
(figurelist, string, **kwargs)¶ obsolete, use the class!
-
pyspecdata.fornotebook.
lrecordarray
(recordlist, columnformat=True, smoosh=True, multi=True, resizebox=False, showpipe=True, return_only=False, format='%0.3f', std_sf=2, scientific_notation=True)¶ generate latex representation of a structured array if set to True, resizebox will automatically scale down the table so it fits on the page (but it will also scale up a small table to fit the width of the page) resizebox can also be a fractional number, so that it is resized to a fraction of the page
-
pyspecdata.fornotebook.
lrecordarray_broken
(recordlist, rows=30, numwide=5)¶
-
pyspecdata.fornotebook.
obs
(*arg)¶
-
pyspecdata.fornotebook.
obs_repr
(*arg)¶
-
pyspecdata.fornotebook.
obsn
(*arg)¶
-
pyspecdata.fornotebook.
ordplot
(x, y, labels, formatstring)¶
-
pyspecdata.fornotebook.
qcalc
(freq1, freq2)¶
-
pyspecdata.fornotebook.
save_data
(inputdata={}, file='data.txt')¶
-
pyspecdata.fornotebook.
save_local
(inputdata={})¶
-
pyspecdata.fornotebook.
save_variable
(variable, content, disp=True, file='data.txt')¶
-
pyspecdata.fornotebook.
see_if_math
(recnames)¶ return latex formatted strings –> ultimately, have this actually check to see if the individual strings are math or not, rather than just assuming that they are. Do this simply by seeing whether or not the thing starts with a full word (more than two characters) or not.
-
pyspecdata.fornotebook.
show_matrix
(B, plot_name, first_figure=None)¶
-
pyspecdata.fornotebook.
standard_noise_comparison
(name, path='franck_cnsi/nmr/', data_subdir='reference_data', expnos=[3])¶
-
pyspecdata.fornotebook.
thisjobname
()¶
-
pyspecdata.fornotebook.
txt_to_dict
(file='data.txt')¶
pyspecdata.general_functions module¶
These are general functions that need to be accessible to everything inside pyspecdata.core. I can’t just put these inside pyspecdata.core, because that would lead to cyclic imports, and e.g. submodules of pyspecdata can’t find them.
-
pyspecdata.general_functions.
autostringconvert
(arg)¶
-
pyspecdata.general_functions.
check_ascending_axis
(u, tolerance=1e-07, additional_message=[])¶ Check that the array u is ascending and equally spaced, and return the spacing, du. This is a common check needed for FT functions, shears, etc.
Parameters: - tolerance (double) – The relative variation in du that is allowed. Defaults to 1e-7.
- additional_message (str) – So that the user can easily figure out where the assertion error is coming from, supply some extra text for the respective message.
Returns: du – the spacing between the elements of u
Return type: double
-
pyspecdata.general_functions.
complex_str
(arg, fancy_format=False, format_code='%.4g')¶ render a complex string – leaving out imaginary if it’s real
-
pyspecdata.general_functions.
exp
(*args, **kwargs)¶
-
pyspecdata.general_functions.
init_logging
(level=20, filename='pyspecdata.log')¶ Initialize logging on pyspecdata.log – do NOT log if run from within a notebook (it’s fair to assume that you will run first before embedding)
-
pyspecdata.general_functions.
inside_sphinx
()¶
-
pyspecdata.general_functions.
log_fname
(logname, fname, dirname)¶
-
pyspecdata.general_functions.
plot
(*args, **kwargs)¶
-
pyspecdata.general_functions.
process_kwargs
(listoftuples, kwargs, pass_through=False, as_attr=False)¶ This function allows dynamically processed (i.e. function definitions with **kwargs) kwargs (keyword arguments) to be dealt with in a fashion more like standard kwargs. The defaults set in listoftuples are used to process kwargs, which are then returned as a set of values (that are set to defaults as needed).
Note that having kwargs as an explicit argument avoids errors where the user forgets to pass the kwargs.
Parameters: - kwargs (**dictionary) – The keyword arguments that you want to process.
- listoftuples (list of tuple pairs) – Tuple pairs, consisting of
('param_name',param_value)
, that give the default values for the various parameters. - pass_through (bool) – Defaults to False. If it’s true, then it’s OK not to process all the kwargs here. In that case, the used kwargs are popped out of the dictionary, and you are expected to pass the unprocessed values (in the dictionary after the call) on to subsequent processing. Importantly, you should always end with a pass_through`=`False call of this function, or by passing **kwargs to a standard function in the standard way. Otherwise it’s possible for the user to pass kwargs that are never processed!
- as_attr (bool, object) – Defaults to False. If not False, it must be an object whose attributes are set to the value of the respective kwargs.
- return (tuple) – It’s expected that the output is assigned to variables with the exact same names as the string in the first half of the tuples, in the exact same order. These parameters will then be set to the appropriate values.
-
pyspecdata.general_functions.
rc
(*args, **kwargs)¶
-
class
pyspecdata.general_functions.
rclass
¶ Bases:
object
-
pyspecdata.general_functions.
redim_C_to_F
(a)¶ see redim_F_to_C
-
pyspecdata.general_functions.
redim_F_to_C
(a)¶ the following creates a C array, reversing the apparent order of dimensions, while preserving the order in memory
-
pyspecdata.general_functions.
reformat_exp
(arg)¶ reformat scientific notation in a nice latex format – used in both pdf and jupyter notebooks
-
pyspecdata.general_functions.
render_matrix
(arg, format_code='%.4g')¶ return latex string representing 2D matrix
-
pyspecdata.general_functions.
strm
(*args)¶
pyspecdata.ipy module¶
pyspecdata.latexscripts module¶
Provides the pdflatex_notebook_wrapper
shell/dos command, which you run
instead of your normal Latex command to build a lab notebook.
The results of python environments are cached and only re-run if the code changes,
even if the python environments are moved around.
This makes the compilation of a Latex lab notebook extremely efficient.
-
pyspecdata.latexscripts.
cache_output_if_needed
(scriptnum_as_str, hashstring, showcode=False, show_error=True)¶ if needed, run the python script numbered by scriptnum_as_str that hashes to hashstring, and output the result to the cache ONLY
-
pyspecdata.latexscripts.
cached_filename
(hashstring, returndir=False)¶ this sets the format for where the cached file is stored we use the first two characters as directory names (so there are just 16 of them
-
pyspecdata.latexscripts.
check_image_path
()¶
-
pyspecdata.latexscripts.
det_new_pdf_name
(thisargv)¶ based on an original tex or pdf name, determine the original basename (i.e., no extension), as well as one with the final word after the underscore removed
-
pyspecdata.latexscripts.
flush_script
(number)¶
-
pyspecdata.latexscripts.
genconfig
()¶ creates a template configuration directory
-
pyspecdata.latexscripts.
get_scripts_dir
()¶
-
pyspecdata.latexscripts.
grab_script_string
(scriptnum_as_str)¶
-
pyspecdata.latexscripts.
main
()¶ This looks for scripts/scriptsUsed.csv inside the notebook directory, and checks whether or not it should be run if a command line argument of “flush” is passed, it flushes that script number from the cache
-
pyspecdata.latexscripts.
script_filename
(scriptnum_as_str)¶
-
pyspecdata.latexscripts.
sha_string
(script)¶ convert the sha hash to a string
-
pyspecdata.latexscripts.
wraplatex
()¶ runs the python scripts after running latex also creates a copy of latex without the final portion under the underscore This prevents the viewer from hanging while it’s waiting for a refresh. This can be used in combination with wrapviewer() and latexmk by using a
~/.latexmkrc
file that looks like this:If you pass the
--xelatex
argument, xelatex is used instead of pdflatex (note that if you’re using latexmk, you need to add this in the latexmkrc file).$pdflatex=q/pdflatex_notebook_wrapper %O -synctex=1 %S/;# calls this function $pdf_previewer=q/pdflatex_notebook_view_wrapper/;# calls the wrapviewer function
pyspecdata.ndshape module¶
The ndshape
class allows you to allocate arrays and determine the shape of existing arrays.
-
class
pyspecdata.ndshape.
ndshape_base
(*args)¶ The base ndshape class, which doesn’t include an allocation method.
-
axn
(axis)¶ return the number for the axis with the name “axis”
-
copy
()¶
-
matchdims
(arg)¶ returns shape with [not in self, len 1] + [overlapping dims between arg + self] + [not in arg] –> this is better accomplished by using sets as I do in the matchdims below
-
pop
(label)¶ remove a dimension
-
rename
(before, after)¶ rename a dimension
-
pyspecdata.nnls module¶
-
pyspecdata.nnls.
nnls_regularized
(A, b, l=0, maxiter=None)¶ Solve math:argmin_x || Ax - b ||_2^2 + lambda^2 ||x||_2^2 for
x>=0
. This is a wrapper for a FORTRAN non-negative least squares solver, with regularization (added by stacking $A$ on top an identity matrix times $lambda$ and $b$ on top of a matching array of zero.Parameters: - A (ndarray) – Matrix
A
as shown above. - b (ndarray) – Right-hand side vector.
- l (double (default 0)) – \(lambda\) – if this is set to 0, the algorithm reverts to standard nnls (rather than stacking on top of two zero matrices for no reason)
- maxiter (int, optional) – Maximum number of iterations, optional.
Default is
3 * A.shape[1]
.
Returns: - x (ndarray) – Solution vector.
- rnorm (float) – The residual,
|| Ax-b ||_2
.
Notes
The FORTRAN code was published in the book below. The algorithm is an active set method. It solves the KKT (Karush-Kuhn-Tucker) conditions for the non-negative least squares problem.
This was adapted from the source distributed with scipy – see scipy for relevant licensing.
References
Lawson C., Hanson R.J., (1987) Solving Least Squares Problems, SIAM
- A (ndarray) – Matrix
pyspecdata.units module¶
Not yet implemented – a preliminary idea for how to handle actual unit conversion. (Currently, we only do s to Hz during FT and order of magnitude prefixes when plotting.)
-
class
pyspecdata.units.
units
(*args)¶ Bases:
object
Each instance of this object stores a numerical representation of a single set of units, and there are routines to set units by (i.e.
parsing
) strings to units and routines to convert units to anstr
representation.At its core, the units are represented by three numpy structured arrays per instance: one for the coefficients in terms of base units, one for the order of magnitude used to determine any prefixes added to the base units, and one for any scaling factors needed to convert to base units.
An array of these structured arrays can be converted into a row vector with
.view((float16,len(base_dtype.names)))
. “Base Units” here are the same as SI base units except that it uses g instead of kg (so we can do the prefixes correctly), and we have added rad.-
load_derived
()¶ Parses and loads a series of definitions for derived units.
It uses definition list to determine a derived dtype vector, which is larger than the base dtype vector.
Then, (not yet done), based on the dictionary that this generates, it will generate a matrix that converts from the derived dtype vector to the base dtype vector.
-
parse
(in_str, verbose=True)¶ Take in_str and parse it as a unit or series of units, and set the units associated with the current instance to the result.
Addition, subtraction, and parentheses are not allowed, and we define a non-standard order of operations, as follows:
\\mu
(including a trailing space) andu
are converted to the utf-8 equivalent (μ)...^-0.125
any number is assumed to be part of the exponent, and only numbers are allowed.*
multiplication- a space also represents multiplication
.../...
comes after all other operations, as is typical for single-line textsqrt(...)
comes “last” in the sense that we take care of everything both inside and outside the sqrt first, before applying the sqrt.
At this point, I use split to break up according to the order of operations, assign powers to each, and determine the prefix. However, I’m currently only using base units, and I will eventually want to use derived units as well.
-
str
(number, using_unicode=False)¶ Give a string that prints number, which has the units given by the current instance of the class. Choose the simplest possible expression for the units.
When printing, we have a matrix that give all our “representation” units, and we use a pseudoinverse to give us the simplest possible expression of our units. (This is assuming that all derived units are defined in terms of powers greater than or equal to 1 of the base units, because powers of magnitude less than 1 would complicate things by allowing us to reduce the norm by spreading across several derived units – in that case, we might want to add a threshold before taking the pinv.)
Currently, I am only giving a method for printing in the base units.
Also, I will want to use number in order to adjust the prefix(es) of the units.
-
pyspecdata.version module¶
Module contents¶
-
exception
pyspecdata.
CustomError
(*value, **kwargs)¶ Bases:
exceptions.Exception
-
class
pyspecdata.
LightSource
(azdeg=315, altdeg=45, hsv_min_val=0, hsv_max_val=1, hsv_min_sat=1, hsv_max_sat=0)¶ Bases:
object
Create a light source coming from the specified azimuth and elevation. Angles are in degrees, with the azimuth measured clockwise from north and elevation up from the zero plane of the surface.
The
shade()
is used to produce “shaded” rgb values for a data array.shade_rgb()
can be used to combine an rgb image with Theshade_rgb()
Thehillshade()
produces an illumination map of a surface.-
blend_hsv
(rgb, intensity, hsv_max_sat=None, hsv_max_val=None, hsv_min_val=None, hsv_min_sat=None)¶ Take the input data array, convert to HSV values in the given colormap, then adjust those color values to give the impression of a shaded relief map with a specified light source. RGBA values are returned, which can then be used to plot the shaded image with imshow.
The color of the resulting image will be darkened by moving the (s,v) values (in hsv colorspace) toward (hsv_min_sat, hsv_min_val) in the shaded regions, or lightened by sliding (s,v) toward (hsv_max_sat hsv_max_val) in regions that are illuminated. The default extremes are chose so that completely shaded points are nearly black (s = 1, v = 0) and completely illuminated points are nearly white (s = 0, v = 1).
Parameters: - rgb (ndarray) – An MxNx3 RGB array of floats ranging from 0 to 1 (color image).
- intensity (ndarray) – An MxNx1 array of floats ranging from 0 to 1 (grayscale image).
- hsv_max_sat (number, optional) – The maximum saturation value that the intensity map can shift the output image to. Defaults to 1.
- hsv_min_sat (number, optional) – The minimum saturation value that the intensity map can shift the output image to. Defaults to 0.
- hsv_max_val (number, optional) – The maximum value (“v” in “hsv”) that the intensity map can shift the output image to. Defaults to 1.
- hsv_min_val (number, optional) – The minimum value (“v” in “hsv”) that the intensity map can shift the output image to. Defaults to 0.
Returns: rgb – An MxNx3 RGB array representing the combined images.
Return type: ndarray
-
blend_overlay
(rgb, intensity)¶ Combines an rgb image with an intensity map using “overlay” blending.
Parameters: - rgb (ndarray) – An MxNx3 RGB array of floats ranging from 0 to 1 (color image).
- intensity (ndarray) – An MxNx1 array of floats ranging from 0 to 1 (grayscale image).
Returns: rgb – An MxNx3 RGB array representing the combined images.
Return type: ndarray
-
blend_soft_light
(rgb, intensity)¶ Combines an rgb image with an intensity map using “soft light” blending. Uses the “pegtop” formula.
Parameters: - rgb (ndarray) – An MxNx3 RGB array of floats ranging from 0 to 1 (color image).
- intensity (ndarray) – An MxNx1 array of floats ranging from 0 to 1 (grayscale image).
Returns: rgb – An MxNx3 RGB array representing the combined images.
Return type: ndarray
-
direction
¶ The unit vector direction towards the light source
-
hillshade
(elevation, vert_exag=1, dx=1, dy=1, fraction=1.0)¶ Calculates the illumination intensity for a surface using the defined azimuth and elevation for the light source.
This computes the normal vectors for the surface, and then passes them on to shade_normals
Parameters: - elevation (array-like) – A 2d array (or equivalent) of the height values used to generate an illumination map
- vert_exag (number, optional) – The amount to exaggerate the elevation values by when calculating illumination. This can be used either to correct for differences in units between the x-y coordinate system and the elevation coordinate system (e.g. decimal degrees vs meters) or to exaggerate or de-emphasize topographic effects.
- dx (number, optional) – The x-spacing (columns) of the input elevation grid.
- dy (number, optional) – The y-spacing (rows) of the input elevation grid.
- fraction (number, optional) – Increases or decreases the contrast of the hillshade. Values greater than one will cause intermediate values to move closer to full illumination or shadow (and clipping any values that move beyond 0 or 1). Note that this is not visually or mathematically the same as vertical exaggeration.
Returns: intensity – A 2d array of illumination values between 0-1, where 0 is completely in shadow and 1 is completely illuminated.
Return type: ndarray
-
shade
(data, cmap, norm=None, blend_mode=u'overlay', vmin=None, vmax=None, vert_exag=1, dx=1, dy=1, fraction=1, **kwargs)¶ Combine colormapped data values with an illumination intensity map (a.k.a. “hillshade”) of the values.
Parameters: - data (array-like) – A 2d array (or equivalent) of the height values used to generate a shaded map.
- cmap (~matplotlib.colors.Colormap instance) – The colormap used to color the data array. Note that this must be a ~matplotlib.colors.Colormap instance. For example, rather than passing in cmap=’gist_earth’, use cmap=plt.get_cmap(‘gist_earth’) instead.
- norm (~matplotlib.colors.Normalize instance, optional) – The normalization used to scale values before colormapping. If None, the input will be linearly scaled between its min and max.
- blend_mode ({'hsv', 'overlay', 'soft'} or callable, optional) – The type of blending used to combine the colormapped data values with the illumination intensity. Default is “overlay”. Note that for most topographic surfaces, “overlay” or “soft” appear more visually realistic. If a user-defined function is supplied, it is expected to combine an MxNx3 RGB array of floats (ranging 0 to 1) with an MxNx1 hillshade array (also 0 to 1). (Call signature func(rgb, illum, **kwargs)) Additional kwargs supplied to this function will be passed on to the blend_mode function.
- vmin (scalar or None, optional) – The minimum value used in colormapping data. If None the minimum value in data is used. If norm is specified, then this argument will be ignored.
- vmax (scalar or None, optional) – The maximum value used in colormapping data. If None the maximum value in data is used. If norm is specified, then this argument will be ignored.
- vert_exag (number, optional) – The amount to exaggerate the elevation values by when calculating illumination. This can be used either to correct for differences in units between the x-y coordinate system and the elevation coordinate system (e.g. decimal degrees vs meters) or to exaggerate or de-emphasize topography.
- dx (number, optional) – The x-spacing (columns) of the input elevation grid.
- dy (number, optional) – The y-spacing (rows) of the input elevation grid.
- fraction (number, optional) – Increases or decreases the contrast of the hillshade. Values greater than one will cause intermediate values to move closer to full illumination or shadow (and clipping any values that move beyond 0 or 1). Note that this is not visually or mathematically the same as vertical exaggeration.
- kwargs are passed on to the blend_mode function. (Additional) –
Returns: rgba – An MxNx4 array of floats ranging between 0-1.
Return type: ndarray
-
shade_normals
(normals, fraction=1.0)¶ Calculates the illumination intensity for the normal vectors of a surface using the defined azimuth and elevation for the light source.
Imagine an artificial sun placed at infinity in some azimuth and elevation position illuminating our surface. The parts of the surface that slope toward the sun should brighten while those sides facing away should become darker.
Parameters: fraction (number, optional) – Increases or decreases the contrast of the hillshade. Values greater than one will cause intermediate values to move closer to full illumination or shadow (and clipping any values that move beyond 0 or 1). Note that this is not visually or mathematically the same as vertical exaggeration. Returns: intensity – A 2d array of illumination values between 0-1, where 0 is completely in shadow and 1 is completely illuminated. Return type: ndarray
-
shade_rgb
(rgb, elevation, fraction=1.0, blend_mode=u'hsv', vert_exag=1, dx=1, dy=1, **kwargs)¶ Take the input RGB array (ny*nx*3) adjust their color values to given the impression of a shaded relief map with a specified light source using the elevation (ny*nx). A new RGB array ((ny*nx*3)) is returned.
Parameters: - rgb (array-like) – An MxNx3 RGB array, assumed to be in the range of 0 to 1.
- elevation (array-like) – A 2d array (or equivalent) of the height values used to generate a shaded map.
- fraction (number) – Increases or decreases the contrast of the hillshade. Values greater than one will cause intermediate values to move closer to full illumination or shadow (and clipping any values that move beyond 0 or 1). Note that this is not visually or mathematically the same as vertical exaggeration.
- blend_mode ({'hsv', 'overlay', 'soft'} or callable, optional) – The type of blending used to combine the colormapped data values with the illumination intensity. For backwards compatibility, this defaults to “hsv”. Note that for most topographic surfaces, “overlay” or “soft” appear more visually realistic. If a user-defined function is supplied, it is expected to combine an MxNx3 RGB array of floats (ranging 0 to 1) with an MxNx1 hillshade array (also 0 to 1). (Call signature func(rgb, illum, **kwargs)) Additional kwargs supplied to this function will be passed on to the blend_mode function.
- vert_exag (number, optional) – The amount to exaggerate the elevation values by when calculating illumination. This can be used either to correct for differences in units between the x-y coordinate system and the elevation coordinate system (e.g. decimal degrees vs meters) or to exaggerate or de-emphasize topography.
- dx (number, optional) – The x-spacing (columns) of the input elevation grid.
- dy (number, optional) – The y-spacing (rows) of the input elevation grid.
- kwargs are passed on to the blend_mode function. (Additional) –
Returns: shaded_rgb – An MxNx3 array of floats ranging between 0-1.
Return type: ndarray
-
-
class
pyspecdata.
Line2D
(xdata, ydata, linewidth=None, linestyle=None, color=None, marker=None, markersize=None, markeredgewidth=None, markeredgecolor=None, markerfacecolor=None, markerfacecoloralt=u'none', fillstyle=None, antialiased=None, dash_capstyle=None, solid_capstyle=None, dash_joinstyle=None, solid_joinstyle=None, pickradius=5, drawstyle=None, markevery=None, **kwargs)¶ Bases:
matplotlib.artist.Artist
A line - the line can have both a solid linestyle connecting all the vertices, and a marker at each vertex. Additionally, the drawing of the solid line is influenced by the drawstyle, e.g., one can create “stepped” lines in various styles.
-
axes
¶ The
Axes
instance the artist resides in, or None.
-
contains
(mouseevent)¶ Test whether the mouse event occurred on the line. The pick radius determines the precision of the location test (usually within five points of the value). Use
get_pickradius()
orset_pickradius()
to view or modify it.Returns True if any values are within the radius along with
{'ind': pointlist}
, where pointlist is the set of points within the radius.TODO: sort returned indices by distance
-
draw
(renderer, *args, **kwargs)¶ draw the Line with renderer unless visibility is False
-
drawStyleKeys
= [u'default', u'steps-mid', u'steps-pre', u'steps-post', u'steps']¶
-
drawStyles
= {u'default': u'_draw_lines', u'steps': u'_draw_steps_pre', u'steps-mid': u'_draw_steps_mid', u'steps-post': u'_draw_steps_post', u'steps-pre': u'_draw_steps_pre'}¶
-
fillStyles
= (u'full', u'left', u'right', u'bottom', u'top', u'none')¶
-
filled_markers
= (u'o', u'v', u'^', u'<', u'>', u'8', u's', u'p', u'*', u'h', u'H', u'D', u'd', u'P', u'X')¶
-
get_aa
()¶ alias for get_antialiased
-
get_antialiased
()¶
-
get_c
()¶ alias for get_color
-
get_color
()¶
-
get_dash_capstyle
()¶ Get the cap style for dashed linestyles
-
get_dash_joinstyle
()¶ Get the join style for dashed linestyles
-
get_data
(orig=True)¶ Return the xdata, ydata.
If orig is True, return the original data.
-
get_drawstyle
()¶
-
get_fillstyle
()¶ return the marker fillstyle
-
get_linestyle
()¶
-
get_linewidth
()¶
-
get_ls
()¶ alias for get_linestyle
-
get_lw
()¶ alias for get_linewidth
-
get_marker
()¶
-
get_markeredgecolor
()¶
-
get_markeredgewidth
()¶
-
get_markerfacecolor
()¶
-
get_markerfacecoloralt
()¶
-
get_markersize
()¶
-
get_markevery
()¶ return the markevery setting
-
get_mec
()¶ alias for get_markeredgecolor
-
get_mew
()¶ alias for get_markeredgewidth
-
get_mfc
()¶ alias for get_markerfacecolor
-
get_mfcalt
(alt=False)¶ alias for get_markerfacecoloralt
-
get_ms
()¶ alias for get_markersize
-
get_path
()¶ Return the
Path
object associated with this line.
-
get_pickradius
()¶ return the pick radius used for containment tests
-
get_solid_capstyle
()¶ Get the cap style for solid linestyles
-
get_solid_joinstyle
()¶ Get the join style for solid linestyles
-
get_window_extent
(renderer)¶
-
get_xdata
(orig=True)¶ Return the xdata.
If orig is True, return the original data, else the processed data.
-
get_xydata
()¶ Return the xy data as a Nx2 numpy array.
-
get_ydata
(orig=True)¶ Return the ydata.
If orig is True, return the original data, else the processed data.
-
is_dashed
()¶ return True if line is dashstyle
-
lineStyles
= {u'': u'_draw_nothing', u' ': u'_draw_nothing', u'-': u'_draw_solid', u'--': u'_draw_dashed', u'-.': u'_draw_dash_dot', u':': u'_draw_dotted', u'None': u'_draw_nothing'}¶
-
markers
= {None: u'nothing', 0: u'tickleft', 1: u'tickright', 2: u'tickup', 3: u'tickdown', 4: u'caretleft', 5: u'caretright', 6: u'caretup', 7: u'caretdown', 8: u'caretleftbase', 9: u'caretrightbase', 10: u'caretupbase', 11: u'caretdownbase', u'': u'nothing', u' ': u'nothing', u'*': u'star', u'+': u'plus', u',': u'pixel', u'.': u'point', u'1': u'tri_down', u'2': u'tri_up', u'3': u'tri_left', u'4': u'tri_right', u'8': u'octagon', u'<': u'triangle_left', u'>': u'triangle_right', u'D': u'diamond', u'H': u'hexagon2', u'None': u'nothing', u'P': u'plus_filled', u'X': u'x_filled', u'^': u'triangle_up', u'_': u'hline', u'd': u'thin_diamond', u'h': u'hexagon1', u'o': u'circle', u'p': u'pentagon', u's': u'square', u'v': u'triangle_down', u'x': u'x', u'|': u'vline'}¶
-
recache
(always=False)¶
-
recache_always
()¶
-
set_aa
(val)¶ alias for set_antialiased
-
set_antialiased
(b)¶ Set whether to use antialiased rendering.
Parameters: b (bool) –
-
set_c
(val)¶ alias for set_color
-
set_color
(color)¶ Set the color of the line
ACCEPTS: any matplotlib color
-
set_dash_capstyle
(s)¶ Set the cap style for dashed linestyles
ACCEPTS: [‘butt’ | ‘round’ | ‘projecting’]
-
set_dash_joinstyle
(s)¶ Set the join style for dashed linestyles ACCEPTS: [‘miter’ | ‘round’ | ‘bevel’]
-
set_dashes
(seq)¶ Set the dash sequence, sequence of dashes with on off ink in points. If seq is empty or if seq = (None, None), the linestyle will be set to solid.
ACCEPTS: sequence of on/off ink in points
-
set_data
(*args)¶ Set the x and y data
ACCEPTS: 2D array (rows are x, y) or two 1D arrays
-
set_drawstyle
(drawstyle)¶ Set the drawstyle of the plot
‘default’ connects the points with lines. The steps variants produce step-plots. ‘steps’ is equivalent to ‘steps-pre’ and is maintained for backward-compatibility.
- ACCEPTS: [‘default’ | ‘steps’ | ‘steps-pre’ | ‘steps-mid’ |
- ‘steps-post’]
-
set_fillstyle
(fs)¶ Set the marker fill style; ‘full’ means fill the whole marker. ‘none’ means no filling; other options are for half-filled markers.
ACCEPTS: [‘full’ | ‘left’ | ‘right’ | ‘bottom’ | ‘top’ | ‘none’]
-
set_linestyle
(ls)¶ Set the linestyle of the line (also accepts drawstyles, e.g.,
'steps--'
)linestyle description '-'
or'solid'
solid line '--'
or'dashed'
dashed line '-.'
or'dashdot'
dash-dotted line ':'
or'dotted'
dotted line 'None'
draw nothing ' '
draw nothing ''
draw nothing ‘steps’ is equivalent to ‘steps-pre’ and is maintained for backward-compatibility.
Alternatively a dash tuple of the following form can be provided:
(offset, onoffseq),
where
onoffseq
is an even length tuple of on and off ink in points.- ACCEPTS: [‘solid’ | ‘dashed’, ‘dashdot’, ‘dotted’ |
- (offset, on-off-dash-seq) |
'-'
|'--'
|'-.'
|':'
|'None'
|' '
|''
]
See also
set_drawstyle()
- To set the drawing style (stepping) of the plot.
Parameters: ls ({ '-'
,'--'
,'-.'
,':'
} and more see description) – The line style.
-
set_linewidth
(w)¶ Set the line width in points
ACCEPTS: float value in points
-
set_ls
(val)¶ alias for set_linestyle
-
set_lw
(val)¶ alias for set_linewidth
-
set_marker
(marker)¶ Set the line marker
ACCEPTS:
A valid marker style
Parameters: marker (marker style) – See ~matplotlib.markers for full description of possible argument
-
set_markeredgecolor
(ec)¶ Set the marker edge color
ACCEPTS: any matplotlib color
-
set_markeredgewidth
(ew)¶ Set the marker edge width in points
ACCEPTS: float value in points
-
set_markerfacecolor
(fc)¶ Set the marker face color.
ACCEPTS: any matplotlib color
-
set_markerfacecoloralt
(fc)¶ Set the alternate marker face color.
ACCEPTS: any matplotlib color
-
set_markersize
(sz)¶ Set the marker size in points
ACCEPTS: float
-
set_markevery
(every)¶ Set the markevery property to subsample the plot when using markers.
e.g., if every=5, every 5-th marker will be plotted.
ACCEPTS: [None | int | length-2 tuple of int | slice | list/array of int | float | length-2 tuple of float]
Parameters: every (None | int | length-2 tuple of int | slice | list/array of int | float | length-2 tuple of float) – Which markers to plot.
- every=None, every point will be plotted.
- every=N, every N-th marker will be plotted starting with marker 0.
- every=(start, N), every N-th marker, starting at point start, will be plotted.
- every=slice(start, end, N), every N-th marker, starting at point start, upto but not including point end, will be plotted.
- every=[i, j, m, n], only markers at points i, j, m, and n will be plotted.
- every=0.1, (i.e. a float) then markers will be spaced at approximately equal distances along the line; the distance along the line between markers is determined by multiplying the display-coordinate distance of the axes bounding-box diagonal by the value of every.
- every=(0.5, 0.1) (i.e. a length-2 tuple of float), the same functionality as every=0.1 is exhibited but the first marker will be 0.5 multiplied by the display-cordinate-diagonal-distance along the line.
Notes
Setting the markevery property will only show markers at actual data points. When using float arguments to set the markevery property on irregularly spaced data, the markers will likely not appear evenly spaced because the actual data points do not coincide with the theoretical spacing between markers.
When using a start offset to specify the first marker, the offset will be from the first data point which may be different from the first the visible data point if the plot is zoomed in.
If zooming in on a plot when using float arguments then the actual data points that have markers will change because the distance between markers is always determined from the display-coordinates axes-bounding-box-diagonal regardless of the actual axes data limits.
-
set_mec
(val)¶ alias for set_markeredgecolor
-
set_mew
(val)¶ alias for set_markeredgewidth
-
set_mfc
(val)¶ alias for set_markerfacecolor
-
set_mfcalt
(val)¶ alias for set_markerfacecoloralt
-
set_ms
(val)¶ alias for set_markersize
-
set_picker
(p)¶ Sets the event picker details for the line.
ACCEPTS: float distance in points or callable pick function
fn(artist, event)
-
set_pickradius
(d)¶ Set the pick radius used for containment tests.
Parameters: d (float) – Pick radius, in points.
-
set_solid_capstyle
(s)¶ Set the cap style for solid linestyles
ACCEPTS: [‘butt’ | ‘round’ | ‘projecting’]
-
set_solid_joinstyle
(s)¶ Set the join style for solid linestyles ACCEPTS: [‘miter’ | ‘round’ | ‘bevel’]
-
set_transform
(t)¶ set the Transformation instance used by this artist
ACCEPTS: a
matplotlib.transforms.Transform
instance
-
set_xdata
(x)¶ Set the data np.array for x
ACCEPTS: 1D array
-
set_ydata
(y)¶ Set the data np.array for y
ACCEPTS: 1D array
-
update_from
(other)¶ copy properties from other to self
-
validCap
= (u'butt', u'round', u'projecting')¶
-
validJoin
= (u'miter', u'round', u'bevel')¶
-
zorder
= 2¶
-
-
class
pyspecdata.
LooseVersion
(vstring=None)¶ Bases:
distutils.version.Version
Version numbering for anarchists and software realists. Implements the standard interface for version number classes as described above. A version number consists of a series of numbers, separated by either periods or strings of letters. When comparing version numbers, the numeric components will be compared numerically, and the alphabetic components lexically. The following are all valid version numbers, in no particular order:
1.5.1 1.5.2b2 161 3.10a 8.02 3.4j 1996.07.12 3.2.pl0 <IP_ADDRESS> 2g6 11g 0.960923 2.2beta29 1.13++ 5.5.kw 2.0b1pl0In fact, there is no such thing as an invalid version number under this scheme; the rules for comparison are simple and predictable, but may not always give the results you want (for some definition of “want”).
-
component_re
= <_sre.SRE_Pattern object>¶
-
parse
(vstring)¶
-
-
pyspecdata.
OLDplot
(*args, **kwargs)¶
-
class
pyspecdata.
PolyCollection
(verts, sizes=None, closed=True, **kwargs)¶ Bases:
matplotlib.collections._CollectionWithSizes
-
set_paths
(verts, closed=True)¶ This allows one to delay initialization of the vertices.
-
set_verts
(verts, closed=True)¶ This allows one to delay initialization of the vertices.
-
set_verts_and_codes
(verts, codes)¶ This allows one to initialize vertices with path codes.
-
-
class
pyspecdata.
UnivariateSpline
(x, y, w=None, bbox=[None, None], k=3, s=None, ext=0, check_finite=False)¶ Bases:
object
One-dimensional smoothing spline fit to a given set of data points.
Fits a spline y = spl(x) of degree k to the provided x, y data. s specifies the number of knots by specifying a smoothing condition.
Parameters: - x ((N,) array_like) – 1-D array of independent input data. Must be increasing.
- y ((N,) array_like) – 1-D array of dependent input data, of the same length as x.
- w ((N,) array_like, optional) – Weights for spline fitting. Must be positive. If None (default), weights are all equal.
- bbox ((2,) array_like, optional) – 2-sequence specifying the boundary of the approximation interval. If
None (default),
bbox=[x[0], x[-1]]
. - k (int, optional) – Degree of the smoothing spline. Must be <= 5. Default is k=3, a cubic spline.
- s (float or None, optional) –
Positive smoothing factor used to choose the number of knots. Number of knots will be increased until the smoothing condition is satisfied:
sum((w[i] * (y[i]-spl(x[i])))**2, axis=0) <= s
If None (default),
s = len(w)
which should be a good value if1/w[i]
is an estimate of the standard deviation ofy[i]
. If 0, spline will interpolate through all data points. - ext (int or str, optional) –
Controls the extrapolation mode for elements not in the interval defined by the knot sequence.
- if ext=0 or ‘extrapolate’, return the extrapolated value.
- if ext=1 or ‘zeros’, return 0
- if ext=2 or ‘raise’, raise a ValueError
- if ext=3 of ‘const’, return the boundary value.
The default value is 0.
- check_finite (bool, optional) – Whether to check that the input arrays contain only finite numbers. Disabling may give a performance gain, but may result in problems (crashes, non-termination or non-sensical results) if the inputs do contain infinities or NaNs. Default is False.
See also
InterpolatedUnivariateSpline
- Subclass with smoothing forced to 0
LSQUnivariateSpline
- Subclass in which knots are user-selected instead of being set by smoothing condition
splrep
- An older, non object-oriented wrapping of FITPACK
splev
,sproot
,splint
,spalde
BivariateSpline
- A similar class for two-dimensional spline interpolation
Notes
The number of data points must be larger than the spline degree k.
NaN handling: If the input arrays contain
nan
values, the result is not useful, since the underlying spline fitting routines cannot deal withnan
. A workaround is to use zero weights for not-a-number data points:>>> from scipy.interpolate import UnivariateSpline >>> x, y = np.array([1, 2, 3, 4]), np.array([1, np.nan, 3, 4]) >>> w = np.isnan(y) >>> y[w] = 0. >>> spl = UnivariateSpline(x, y, w=~w)
Notice the need to replace a
nan
by a numerical value (precise value does not matter as long as the corresponding weight is zero.)Examples
>>> import matplotlib.pyplot as plt >>> from scipy.interpolate import UnivariateSpline >>> x = np.linspace(-3, 3, 50) >>> y = np.exp(-x**2) + 0.1 * np.random.randn(50) >>> plt.plot(x, y, 'ro', ms=5)
Use the default value for the smoothing parameter:
>>> spl = UnivariateSpline(x, y) >>> xs = np.linspace(-3, 3, 1000) >>> plt.plot(xs, spl(xs), 'g', lw=3)
Manually change the amount of smoothing:
>>> spl.set_smoothing_factor(0.5) >>> plt.plot(xs, spl(xs), 'b', lw=3) >>> plt.show()
-
antiderivative
(n=1)¶ Construct a new spline representing the antiderivative of this spline.
Parameters: n (int, optional) – Order of antiderivative to evaluate. Default: 1 Returns: spline – Spline of order k2=k+n representing the antiderivative of this spline. Return type: UnivariateSpline Notes
New in version 0.13.0.
See also
splantider()
,derivative()
Examples
>>> from scipy.interpolate import UnivariateSpline >>> x = np.linspace(0, np.pi/2, 70) >>> y = 1 / np.sqrt(1 - 0.8*np.sin(x)**2) >>> spl = UnivariateSpline(x, y, s=0)
The derivative is the inverse operation of the antiderivative, although some floating point error accumulates:
>>> spl(1.7), spl.antiderivative().derivative()(1.7) (array(2.1565429877197317), array(2.1565429877201865))
Antiderivative can be used to evaluate definite integrals:
>>> ispl = spl.antiderivative() >>> ispl(np.pi/2) - ispl(0) 2.2572053588768486
This is indeed an approximation to the complete elliptic integral \(K(m) = \int_0^{\pi/2} [1 - m\sin^2 x]^{-1/2} dx\):
>>> from scipy.special import ellipk >>> ellipk(0.8) 2.2572053268208538
-
derivative
(n=1)¶ Construct a new spline representing the derivative of this spline.
Parameters: n (int, optional) – Order of derivative to evaluate. Default: 1 Returns: spline – Spline of order k2=k-n representing the derivative of this spline. Return type: UnivariateSpline See also
splder()
,antiderivative()
Notes
New in version 0.13.0.
Examples
This can be used for finding maxima of a curve:
>>> from scipy.interpolate import UnivariateSpline >>> x = np.linspace(0, 10, 70) >>> y = np.sin(x) >>> spl = UnivariateSpline(x, y, k=4, s=0)
Now, differentiate the spline and find the zeros of the derivative. (NB: sproot only works for order 3 splines, so we fit an order 4 spline):
>>> spl.derivative().roots() / np.pi array([ 0.50000001, 1.5 , 2.49999998])
This agrees well with roots \(\pi/2 + n\pi\) of \(\cos(x) = \sin'(x)\).
-
derivatives
(x)¶ Return all derivatives of the spline at the point x.
Parameters: x (float) – The point to evaluate the derivatives at. Returns: der – Derivatives of the orders 0 to k. Return type: ndarray, shape(k+1,) Examples
>>> from scipy.interpolate import UnivariateSpline >>> x = np.linspace(0, 3, 11) >>> y = x**2 >>> spl = UnivariateSpline(x, y) >>> spl.derivatives(1.5) array([2.25, 3.0, 2.0, 0])
-
get_coeffs
()¶ Return spline coefficients.
-
get_knots
()¶ Return positions of interior knots of the spline.
Internally, the knot vector contains
2*k
additional boundary knots.
-
get_residual
()¶ Return weighted sum of squared residuals of the spline approximation.
This is equivalent to:
sum((w[i] * (y[i]-spl(x[i])))**2, axis=0)
-
integral
(a, b)¶ Return definite integral of the spline between two given points.
Parameters: - a (float) – Lower limit of integration.
- b (float) – Upper limit of integration.
Returns: integral – The value of the definite integral of the spline between limits.
Return type: float
Examples
>>> from scipy.interpolate import UnivariateSpline >>> x = np.linspace(0, 3, 11) >>> y = x**2 >>> spl = UnivariateSpline(x, y) >>> spl.integral(0, 3) 9.0
which agrees with \(\int x^2 dx = x^3 / 3\) between the limits of 0 and 3.
A caveat is that this routine assumes the spline to be zero outside of the data limits:
>>> spl.integral(-1, 4) 9.0 >>> spl.integral(-1, 0) 0.0
-
roots
()¶ Return the zeros of the spline.
Restriction: only cubic splines are supported by fitpack.
-
set_smoothing_factor
(s)¶ Continue spline computation with the given smoothing factor s and with the knots found at the last call.
This routine modifies the spline in place.
-
pyspecdata.
addlabels
(labelstring, x, y, labels)¶ obsolete – use plot_label_points
-
pyspecdata.
apply_oom
(average_oom, numbers, prev_label='')¶ scale numbers by the order of magnitude average_oom and change the label prev_label by adding the appropriate SI prefix
-
pyspecdata.
applyto_rec
(myfunc, myarray, mylist)¶ apply myfunc to myarray with the intention of collapsing it to a smaller number of values
-
pyspecdata.
autolegend
(*args, **kwargs)¶
-
pyspecdata.
autopad_figure
(pad=0.2, centered=False, figname='unknown')¶
-
pyspecdata.
autostringconvert
(arg)¶
-
pyspecdata.
box_muller
(length, return_complex=True)¶ algorithm to generate normally distributed noise
-
pyspecdata.
bruker_load_t1_axis
(file)¶
-
pyspecdata.
bruker_load_title
(file)¶
-
pyspecdata.
check_ascending_axis
(u, tolerance=1e-07, additional_message=[])¶ Check that the array u is ascending and equally spaced, and return the spacing, du. This is a common check needed for FT functions, shears, etc.
Parameters: - tolerance (double) – The relative variation in du that is allowed. Defaults to 1e-7.
- additional_message (str) – So that the user can easily figure out where the assertion error is coming from, supply some extra text for the respective message.
Returns: du – the spacing between the elements of u
Return type: double
-
pyspecdata.
colormap
(points, colors, n=256)¶
-
pyspecdata.
complex_str
(arg, fancy_format=False, format_code='%.4g')¶ render a complex string – leaving out imaginary if it’s real
-
pyspecdata.
concat
(datalist, dimname, chop=False)¶
-
pyspecdata.
contour_plot
(xvals, yvals, zvals, color='k', alpha=1.0, npts=300, **kwargs)¶
-
pyspecdata.
copy_maybe_none
(input)¶
-
pyspecdata.
cw
(file, **kwargs)¶
-
pyspecdata.
decorate_rec
()¶ Decorate the rows in A with information in B –> if names overlap, keep the ones in A b_ind and a_ind can be either a single key, or a list of keys; if more than one element in B matches that in A, include both options!!
-
pyspecdata.
deepcopy
(x, memo=None, _nil=[])¶ Deep copy operation on arbitrary Python objects.
See the module’s __doc__ string for more info.
-
pyspecdata.
dp
(number, decimalplaces, scientific=False)¶
-
pyspecdata.
drop_fields
(base, drop_names, usemask=True, asrecarray=False)¶ Return a new array with fields in drop_names dropped.
Nested fields are supported.
Parameters: - base (array) – Input array
- drop_names (string or sequence) – String or sequence of strings corresponding to the names of the fields to drop.
- usemask ({False, True}, optional) – Whether to return a masked array or not.
- asrecarray (string or sequence, optional) – Whether to return a recarray or a mrecarray (asrecarray=True) or a plain ndarray or masked array with flexible dtype. The default is False.
Examples
>>> from numpy.lib import recfunctions as rfn >>> a = np.array([(1, (2, 3.0)), (4, (5, 6.0))], ... dtype=[('a', int), ('b', [('ba', float), ('bb', int)])]) >>> rfn.drop_fields(a, 'a') array([((2.0, 3),), ((5.0, 6),)], dtype=[('b', [('ba', '<f8'), ('bb', '<i4')])]) >>> rfn.drop_fields(a, 'ba') array([(1, (3,)), (4, (6,))], dtype=[('a', '<i4'), ('b', [('bb', '<i4')])]) >>> rfn.drop_fields(a, ['ba', 'bb']) array([(1,), (4,)], dtype=[('a', '<i4')])
-
pyspecdata.
emptyfunction
()¶
-
pyspecdata.
emptytest
(x)¶
-
pyspecdata.
exc_info
() -> (type, value, traceback)¶ Return information about the most recent exception caught by an except clause in the current stack frame or in an older stack frame.
-
pyspecdata.
exp
(*args, **kwargs)¶
-
pyspecdata.
expand_x
(*args)¶ expand the axes. If an argument is passed, then it refers to the position relative to the current coordinates. Values can be: :0: set this side of the axis to 0 :None: leave this side of the axis alone :a double: rescale the distance from the center of the axis to this side by this number
-
pyspecdata.
expand_y
(*args)¶ expand the axes. If an argument is passed, then it refers to the position relative to the current coordinates. Values can be: :0: set this side of the axis to 0 :None: leave this side of the axis alone :a double: rescale the distance from the center of the axis to this side by this number
-
pyspecdata.
explain_error
(e)¶ Allows you to wrap existing errors with more explanation
For example:
> except BaseException as e: > raise IndexError(“I can’t find the node “+pathstring+explain_error(e)) > + ‘
‘.join([‘> ‘+j for j in str(e).split(‘ ‘)]))# this indents
-
pyspecdata.
fa
(input, dtype='complex128')¶
-
pyspecdata.
fftconvolve
(in1, in2, mode='full', axes=None)¶ Convolve two N-dimensional arrays using FFT.
Convolve in1 and in2 using the fast Fourier transform method, with the output size determined by the mode argument.
This is generally much faster than convolve for large arrays (n > ~500), but can be slower when only a few output values are needed, and can only output float arrays (int or object array inputs will be cast to float).
As of v0.19, convolve automatically chooses this method or the direct method based on an estimation of which is faster.
Parameters: - in1 (array_like) – First input.
- in2 (array_like) – Second input. Should have the same number of dimensions as in1.
- mode (str {'full', 'valid', 'same'}, optional) –
A string indicating the size of the output:
full
- The output is the full discrete linear convolution of the inputs. (Default)
valid
- The output consists only of those elements that do not rely on the zero-padding. In ‘valid’ mode, either in1 or in2 must be at least as large as the other in every dimension.
same
- The output is the same size as in1, centered with respect to the ‘full’ output. axis : tuple, optional
- axes (int or array_like of ints or None, optional) – Axes over which to compute the convolution. The default is over all axes.
Returns: out – An N-dimensional array containing a subset of the discrete linear convolution of in1 with in2.
Return type: array
Examples
Autocorrelation of white noise is an impulse.
>>> from scipy import signal >>> sig = np.random.randn(1000) >>> autocorr = signal.fftconvolve(sig, sig[::-1], mode='full')
>>> import matplotlib.pyplot as plt >>> fig, (ax_orig, ax_mag) = plt.subplots(2, 1) >>> ax_orig.plot(sig) >>> ax_orig.set_title('White noise') >>> ax_mag.plot(np.arange(-len(sig)+1,len(sig)), autocorr) >>> ax_mag.set_title('Autocorrelation') >>> fig.tight_layout() >>> fig.show()
Gaussian blur implemented using FFT convolution. Notice the dark borders around the image, due to the zero-padding beyond its boundaries. The convolve2d function allows for other types of image boundaries, but is far slower.
>>> from scipy import misc >>> face = misc.face(gray=True) >>> kernel = np.outer(signal.gaussian(70, 8), signal.gaussian(70, 8)) >>> blurred = signal.fftconvolve(face, kernel, mode='same')
>>> fig, (ax_orig, ax_kernel, ax_blurred) = plt.subplots(3, 1, ... figsize=(6, 15)) >>> ax_orig.imshow(face, cmap='gray') >>> ax_orig.set_title('Original') >>> ax_orig.set_axis_off() >>> ax_kernel.imshow(kernel, cmap='gray') >>> ax_kernel.set_title('Gaussian kernel') >>> ax_kernel.set_axis_off() >>> ax_blurred.imshow(blurred, cmap='gray') >>> ax_blurred.set_title('Blurred') >>> ax_blurred.set_axis_off() >>> fig.show()
-
class
pyspecdata.
figlist
(*arg, **kwargs)¶ Bases:
object
-
basename
¶ A basename that can be changed to generate different sets of figures with different basenames. For example, this is useful if you are looping over different sets of data, and generating the same set of figures for each set of data (which would correspond to a basename).
Type: str
-
figurelist
¶ A list of the figure names
Type: list
-
figdict
¶ A dictionary containing the figurelist and the figure numbers or objects that they correspond to. Keys of this dictionary must be elements of figurelist.
Type: dict
-
propdict
¶ Maintains various properties for each element in figurelist. Keys of this dictionary must be elements of figurelist.
Type: dict
-
adjust_spines
(spines)¶
-
check_units
(testdata, x_index, y_index, verbose=False)¶
-
generate_ticks
(plotdata, axes, rescale, z_norm=None, y_rescale=1, text_scale=0.05, verbose=False, follow_surface=False, lensoffset=0.005, line_width=0.001, tube_radius=0.001, fine_grid=False)¶ generate 3d ticks and grid for mayavi
-
get_fig_number
(name)¶
-
grid
()¶
-
header
(number_above, input_string)¶
-
image
(A, **kwargs)¶ Called as fl.image() where fl is the figlist_var object
Note that this code just wraps the figlist properties, and the heavy lifting is done by the image( function. Together, the effect is as follows:
check_units converts to human-readable units, and makes sure they match the units already used in the plot.
if A has more than two dimensions, the final dimension in A.dimlabels is used as the column dimension, and a direct-product of all non-column dimensions (a Kronecker product, such that the innermost index comes the latest in the list A.dimlabels) is used as the row dimension. A white/black line is drawn after the innermost index used to create the direct product is finished iterating.
If A consists of complex data, then an HSV plot (misnomer, actually an HV plot) is used: - convert to polar form: \(z=\rho \exp(i \phi)\) - \(\phi\) determines the color (Hue)
- Color wheel is cyclical, like \(\exp(i \phi)\)
- red is taken as \(\phi=0\), purely real and positive
- green-blue is \(pi\) radians out of phase with red and therefore negative real
\(\rho\) determines the intensity (value) - Depending on whether or not black is set (either as a
keyword argument, or fl.black, the background will be black with high \(\rho\) values “lit up” (intended for screen plotting) or the background will be white with the high \(\rho\) values “colored in” (intended for printing)
If the data type (dtype) of the data in A is real (typically achieved by calling abs(A) or A.runcopy(real)), then A is plotted with a colormap and corresponding colorbar.
If no title has been given, it’s set to the name of the current plot in the figurelist
-
x
¶ If A is a numpy array, then this gives the values along the x axis (columns). Defaults to the size of the array. Not used if A is nddata.
Type: Optional[double] or Optional[scalar]
-
y
¶ If A is a numpy array, then this gives the values along the y axis (columns). Defaults to the size of the array. Not used if A is nddata.
Type: Optional[double] or Optional[scalar]
-
x_first
¶ Since it’s designed to represent matrices, an image plot by defaults is “transposed” relative to all other plots. If you want the first dimension on the x-axis (e.g., if you are plotting a contour plot on top of an image), then set x_first to True.
Type: boolean
-
spacing
¶ Determines the size of the white/black line drawn Defaults to 1
Type: integer
-
ax
¶ the Axis object where the plot should go.
Type: matplotlib Axes
-
all remaning
are passed through to matplotlib imshow
-
origin
¶ upper and lower are passed to matplotlib. Flip is for 2D nmr, and flips the data manually.
Type: {‘upper’, ‘lower’, ‘flip’}
-
.. code-block:: python
from pyspecdata import * fl = figlist_var()
t = r_[-1:1:300j] x = nddata(t,[-1],[‘x’]).labels(‘x’,t) y = nddata(t,[-1],[‘y’]).labels(‘y’,t)
z = x**2 + 2*y**2 print “dimlabels of z:”,z.dimlabels
fl.next(‘image with contours’) fl.image(z,x_first = True) # x_first is needed to align # with the contour plot z.contour(colors = ‘w’,alpha = 0.75)
fl.next(‘simple plot’) # just to show that x is the same # here as well fl.plot(z[‘y’:(0,0.01)])
fl.show(‘compare_image_contour_150911.pdf’)
-
label_point
(data, axis, value, thislabel, show_point=True, xscale=1, **new_kwargs)¶ only works for 1D data: assume you’ve passed a single-point nddata, and label it
xscale gives the unit scaling
..todo:
Improve the unit scaling, so that this would also work. Allow it to include a format string that would use the value.
Parameters: show_point (bool) – Defaults to True. Actually generate a point (circle), vs. just the label.
-
marked_text
(marker, input_text='', sep='\n')¶ Creates a named marker where we can place text. If marker has been used, goes back and places text there.
-
mesh
(plotdata, Z_normalization=None, equal_scale=True, lensoffset=0.001, show_contours=False, grey_surf=False, **kwargs)¶
-
next
(input_name, legend=False, boundaries=None, twinx=None, **kwargs)¶ Switch to the figure given by input_name, which is used not only as a string-based name for the figure, but also as a default title and as a base name for resulting figure files.
In the future, we actually want this to track the appropriate axis object!
Parameters: - legend (bool) – If this is set, a legend is created outside the figure.
- twinx ({0,1}) –
1: plots on an overlayed axis (the matplotlib twinx) whose y axis is labeled on the right when you set this for the first time, you can also set a color kwarg that controls the coloring of the right axis. 0: used to switch back to the left (default) axis - boundaries – need to add description
- kwargs (dict) – Any other keyword arguments are passed to the matplotlib (mayavi) figure() function that’s used to switch (create) figures.
-
phaseplot_finalize
()¶ Performs plot decorations that are typically desired for a manual phasing plot. This assumes that the
y
-axis is given in units of half-cycles ($pi$ radians).
-
plot