Unit class

(Shortest import: from brian2 import Unit)

class brian2.units.fundamentalunits.Unit(value, dim=None, scale=None)[source]

Bases: brian2.units.fundamentalunits.Quantity

A physical unit.

Normally, you do not need to worry about the implementation of units. They are derived from the Quantity object with some additional information (name and string representation).

Basically, a unit is just a number with given dimensions, e.g. mvolt = 0.001 with the dimensions of voltage. The units module defines a large number of standard units, and you can also define your own (see below).

The unit class also keeps track of various things that were used to define it so as to generate a nice string representation of it. See below.

When creating scaled units, you can use the following prefixes:

Factor Name Prefix
10^24 yotta Y
10^21 zetta Z
10^18 exa E
10^15 peta P
10^12 tera T
10^9 giga G
10^6 mega M
10^3 kilo k
10^2 hecto h
10^1 deka da
1    
10^-1 deci d
10^-2 centi c
10^-3 milli m
10^-6 micro u (mu in SI)
10^-9 nano n
10^-12 pico p
10^-15 femto f
10^-18 atto a
10^-21 zepto z
10^-24 yocto y

Defining your own

It can be useful to define your own units for printing purposes. So for example, to define the newton metre, you write >>> from brian2.units.allunits import metre, newton >>> Nm = newton * metre

You can then do

>>> (1*Nm).in_unit(Nm)
'1. N m'

which returns "1 N m" because the Unit class generates a new display name of "N m" from the display names "N" and "m" for newtons and metres automatically.

To register this unit for use in the automatic printing of the Quantity.in_best_unit() method, see the documentation for the UnitRegistry class.

Construction

The best way to construct a new unit is to use standard units already defined and arithmetic operations, e.g. newton*metre. See the documentation for the static methods Unit.create and Unit.create_scaled_units for more details.

If you don’t like the automatically generated display name for the unit, use the Unit.set_display_name() method.

Representation

A new unit defined by multiplication, division or taking powers generates a name for the unit automatically, so that for example the name for pfarad/mmetre**2 is "pF/mm^2", etc. If you don’t like the automatically generated name, use the Unit.set_display_name() method.

Attributes

_dispname The display name of this unit.
_latexname A LaTeX expression for the name of this unit.
_name The full name of this unit.
dim The Dimensions of this unit
dispname The display name of the unit
iscompound Whether this unit is a combination of other units.
latexname The LaTeX name of the unit
name The name of the unit
scale The scale for this unit (a 7-tuple)
scalefactor The scalefactor for this unit, e.g.

Methods

create(dim[, name, dispname, latexname, ...]) Create a new named unit.
create_scaled_unit(baseunit, scalefactor) Create a scaled unit from a base unit.
get_display_name()
get_latex_name()
get_name()
set_display_name(name) Sets the display name for the unit
set_latex_name(name)
set_name(name) Sets the name for the unit

Details

_dispname

The display name of this unit.

_latexname

A LaTeX expression for the name of this unit.

_name

The full name of this unit.

dim

The Dimensions of this unit

dispname

The display name of the unit

iscompound

Whether this unit is a combination of other units.

latexname

The LaTeX name of the unit

name

The name of the unit

scale

The scale for this unit (a 7-tuple)

scalefactor

The scalefactor for this unit, e.g. ‘m’ for milli

static create(dim, name='', dispname='', latexname=None, scalefactor='', **keywords)[source]

Create a new named unit.

Parameters:

dim : Dimension

The dimensions of the unit.

name : str, optional

The full name of the unit, e.g. 'volt'

dispname : str, optional

The display name, e.g. 'V'

latexname : str, optional

The name as a LaTeX expression (math mode is assumed, do not add $ signs or similar), e.g. '\omega'. If no latexname is specified, dispname will be used.

scalefactor : str, optional

The scaling factor, e.g. 'm' for mvolt

keywords :

The scaling for each SI dimension, e.g. length="m", mass="-1", etc.

Returns:

u : Unit

The new unit.

static create_scaled_unit(baseunit, scalefactor)[source]

Create a scaled unit from a base unit.

Parameters:

baseunit : Unit

The unit of which to create a scaled version, e.g. volt, amp.

scalefactor : str

The scaling factor, e.g. "m" for mvolt, mamp

Returns:

u : Unit

The new unit.

get_display_name()[source]
get_latex_name()[source]
get_name()[source]
set_display_name(name)[source]

Sets the display name for the unit

set_latex_name(name)[source]
set_name(name)[source]

Sets the name for the unit