Skip to content

Commit

Permalink
Minor tweaks to the documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Kundert authored and Ken Kundert committed Jul 15, 2017
1 parent 40057db commit 4f161d0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 39 deletions.
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ You use *Quantity* to convert numbers and units in various forms to quantities:
>>> print(Fhy)
1.4204 GHz
>>> Rsense = Quantity('1e-4 Ohms')
>>> Rsense = Quantity('1e-4 Ω')
>>> print(Rsense)
100 uOhms
100
>>> cost = Quantity('$11_200_000')
>>> print(cost)
Expand All @@ -128,7 +128,7 @@ the quantity:
22400000.0
>>> Rsense.units
'Ohms'
'Ω'
>>> str(Tboil)
'100 °C'
Expand Down
45 changes: 20 additions & 25 deletions doc/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ included a width specification, but in the second the desired unit of measure
was specified (*B*), which caused the underlying value to be converted from bits
to bytes.

Also it is important to recognize that *QuantiPhy* is using decimal rather than
binary scale factors. So 5 GB is 5 gigabyte and not 5 gibibyte. In otherwords
5 GB represents 5×10⁹ B and not 5×2³⁰ B.


.. _thermal voltage example:

Expand Down Expand Up @@ -399,32 +403,19 @@ produces an SVG version of the results using MatPlotLib.
from matplotlib.ticker import FuncFormatter
import matplotlib.pyplot as pl
from quantiphy import Quantity
Quantity.set_prefs(prec=2)
# define the axis formatting routines
def freq_fmt(val, pos):
return Quantity(val, 'Hz').render()
freq_formatter = FuncFormatter(freq_fmt)
def volt_fmt(val, pos):
return Quantity(val, 'V').render()
volt_formatter = FuncFormatter(volt_fmt)
# read the data from delta-sigma.smpl
data = np.fromfile('delta-sigma.smpl', sep=' ')
time, wave = data.reshape((2, len(data)//2), order='F')
# print out basic information about the data
timestep = Quantity(time[1] - time[0], 's')
nonperiodicity = Quantity(wave[-1] - wave[0], 'V')
points = len(time)
period = Quantity(timestep * len(time), 's')
freq_res = Quantity(1/period, 'Hz')
print('timestep:', timestep)
print('nonperiodicity:', nonperiodicity)
print('timepoints:', points)
print('period:', period)
print('freq resolution:', freq_res)
timestep = Quantity(time[1] - time[0], name='Time step', units='s')
nonperiodicity = Quantity(wave[-1] - wave[0], name='Nonperiodicity', units='V')
points = Quantity(len(time), name='Time points')
period = Quantity(timestep * len(time), name='Period', units='s')
freq_res = Quantity(1/period, name='Frequency resolution', units='Hz')
with Quantity.prefs(show_label=True, prec=2):
print(timestep, nonperiodicity, points, period, freq_res, sep='\n')
# create the window
window = np.kaiser(len(time), 11)/0.37
Expand All @@ -436,6 +427,10 @@ produces an SVG version of the results using MatPlotLib.
spectrum = 2*fftshift(fft(windowed))/len(time)
freq = fftshift(fftfreq(len(wave), timestep))
# define the axis formatting routines
freq_formatter = FuncFormatter(lambda v, p: str(Quantity(v, 'Hz')))
volt_formatter = FuncFormatter(lambda v, p: str(Quantity(v, 'V')))
# generate graphs of the resulting spectrum
fig = pl.figure()
ax = fig.add_subplot(111)
Expand All @@ -450,11 +445,11 @@ produces an SVG version of the results using MatPlotLib.
This script produces the following textual output::

timestep: 20 ns
nonperiodicity: 2.3 pV
timepoints: 28k
period: 560 us
freq resolution: 1.79 kHz
Time step = 20 ns
Nonperiodicity = 2.3 pV
Time points = 28k
Period = 560 us
Frequency resolution = 1.79 kHz

And the following is one of the two graphs produced:

Expand Down
26 changes: 15 additions & 11 deletions doc/user.rst
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,15 @@ If given as a string, the value may also be the name of a known constant:
160.22e-21 C
If you only specify a real number for the value, then the units, name, and
description do not get values. But even if given as a string or quantity the
value may not contain these extra attributes. This is where the second argument,
the model, helps. It may be another quantity or it may be a string. Any
attributes that are not provided by the first argument are taken from the second
if available. If the second argument is a string, it is split. If it contains
one value, that value is taken to be the units, if it contains two, those values
are taken to be the name and units, and it it contains more than two, the
remaining values are taken to be the description. If the model is a quantity,
only the units are inherited. For example:
description do not get values. Even if given as a string or quantity the value
may not contain these extra attributes. This is where the second argument, the
model, helps. It may be another quantity or it may be a string. Any attributes
that are not provided by the first argument are taken from the second if
available. If the second argument is a string, it is split. If it contains one
value, that value is taken to be the units, if it contains two, those values are
taken to be the name and units, and it it contains more than two, the remaining
values are taken to be the description. If the model is a quantity, only the
units are inherited. For example:

.. code-block:: python
Expand Down Expand Up @@ -361,13 +361,17 @@ You can also create your own converters using :class:`quantiphy.UnitConversion`:
>>> UnitConversion('m', 'pc parsec', 3.0857e16)
<...>
>>> d = Quantity('5 μpc', scale='m')
>>> print(d)
>>> d_sol = Quantity('5 μpc', scale='m')
>>> print(d_sol)
154.28 Gm
This unit conversion says, when converting units of 'm' to either 'pc' or
'parsec' multiply by 3.0857e16, when going the other way, divide by 3.0857e16.

>>> d_sol = Quantity('154.285 Gm', scale='pc')
>>> print(d_sol)
5 upc

When using unit conversions it is important to only convert to units without
scale factors (such as those in the first column above) when creating
a quantity. For example, it is better to convert to 'm' rather than 'cm'. If
Expand Down

0 comments on commit 4f161d0

Please sign in to comment.