Units

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)

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 an str 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:

  1. \\mu (including a trailing space) and u are converted to the utf-8 equivalent (μ)

  2. ...^-0.125 any number is assumed to be part of the exponent, and only numbers are allowed.

  3. * multiplication

  4. a space also represents multiplication

  5. .../... comes after all other operations, as is typical for single-line text

  6. sqrt(...) 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.