Unit class

(Shortest import: from brian2 import Unit)

class brian2.units.fundamentalunits.Unit(arr, dim=None, scale=0, name=None, dispname=None, latexname=None, iscompound=False, dtype=None, copy=False)[source]

Bases: 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 import *
>>> from brian2.units.allunits import newton
>>> Nm = newton * metre

You can then do

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

New “compound units”, i.e. units that are composed of other units will be automatically registered and from then on used for display. For example, imagine you define total conductance for a membrane, and the total area of that membrane:

>>> conductance = 10.*nS
>>> area = 20000*um**2

If you now ask for the conductance density, you will get an “ugly” display in basic SI dimensions, as Brian does not know of a corresponding unit:

>>> conductance/area
0.5 * metre ** -4 * kilogram ** -1 * second ** 3 * amp ** 2

By using an appropriate unit once, it will be registered and from then on used for display when appropriate:

>>> usiemens/cm**2
usiemens / (cmetre ** 2)
>>> conductance/area  # same as before, but now Brian knows about uS/cm^2
50. * usiemens / (cmetre ** 2)

Note that user-defined units cannot override the standard units (volt, second, etc.) that are predefined by Brian. For example, the unit Nm has the dimensions “length²·mass/time²”, and therefore the same dimensions as the standard unit joule. The latter will be used for display purposes:

>>> 3*joule
3. * joule
>>> 3*Nm
3. * joule

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 (as the integer exponent of 10), i.e. a scale of 3 means 10^3, e.g. for a "k" prefix.

Methods

create(dim, name, dispname[, latexname, scale])

Create a new named unit.

create_scaled_unit(baseunit, scalefactor)

Create a scaled unit from a base unit.

set_display_name(name)

Sets the display name for the unit.

set_latex_name(name)

Sets the LaTeX name for the unit.

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 (as the integer exponent of 10), i.e. a scale of 3 means 10^3, e.g. for a “k” prefix.

static create(dim, name, dispname, latexname=None, scale=0)[source]

Create a new named unit.

static create_scaled_unit(baseunit, scalefactor)[source]

Create a scaled unit from a base unit.

set_display_name(name)[source]

Sets the display name for the unit.

Deprecated since version 2.1: Create a new unit with Unit.create instead.

set_latex_name(name)[source]

Sets the LaTeX name for the unit.

Deprecated since version 2.1: Create a new unit with Unit.create instead.

set_name(name)[source]

Sets the name for the unit.

Deprecated since version 2.1: Create a new unit with Unit.create instead.