Units and Conversions#

Note

MagentroPy uses the pint package internally for unit conversions, as well as the pint_pandas package to extend functionality to DataFrames. If you happen to be using pint_pandas as well, be aware that MagentroData’s pint.UnitRegistry is set to pint_pandas.PintType.ureg, and it is reset with the necessary definitions and conversion contexts each time conversions are performed.

Tip

Raw data units can be set during instantiation:

magdata = MagentroData(..., raw_data_units={...})
from IPython.display import display, HTML
from magentropy import MagentroData

magdata = MagentroData('magdata.csv', qd_dat=False, comment_col=None, T='T', H='H', M='M', M_err='M_err')
magdata.process_data()

def data_heads():
    display(HTML('<p>Raw data:</p>'))
    display(magdata.raw_df_with_units.head(3))
    display(HTML('<p>Converted data:</p>'))
    display(magdata.converted_df_with_units.head(3))
    display(HTML('<p>Processed data:</p>'))
    display(magdata.processed_df_with_units.head(3))
Hide code cell output
The data contains the following 5 magnetic field strengths and observations per field:
20.0     100
40.0     100
60.0     100
80.0     100
100.0    100
Name: T, dtype: int64

Processing data using the following settings:
{
	npoints: 1000,
	temp_range: [-inf  inf],
	fields: [],
	decimals: 5,
	max_diff: inf,
	min_sweep_len: 10,
	d_order: 2,
	lmbds: [nan],
	lmbd_guess: 0.0001,
	weight_err: True,
	match_err: False,
	min_kwargs: {'method': 'Nelder-Mead', 'bounds': ((-inf, inf),), 'options': {'maxfev': 50, 'xatol': 0.01, 'fatol': 1e-06}},
	add_zeros: False
}

scipy.optimize.minimize: Optimization terminated successfully.
Processed M(T) at field: 20.0
scipy.optimize.minimize: Optimization terminated successfully.
Processed M(T) at field: 40.0
scipy.optimize.minimize: Optimization terminated successfully.
Processed M(T) at field: 60.0
scipy.optimize.minimize: Optimization terminated successfully.
Processed M(T) at field: 80.0
scipy.optimize.minimize: Optimization terminated successfully.
Processed M(T) at field: 100.0
Calculated raw derivative and entropy.

last_presets set to:
{
	npoints: 1000,
	temp_range: [  0.99999934 100.00000083],
	fields: [ 20.  40.  60.  80. 100.],
	decimals: 5,
	max_diff: inf,
	min_sweep_len: 10,
	d_order: 2,
	lmbds: [0.00091728 0.00054639 0.00072862 0.00091728 0.00095775],
	lmbd_guess: 0.0001,
	weight_err: True,
	match_err: False,
	min_kwargs: {'method': 'Nelder-Mead', 'bounds': ((-inf, inf),), 'options': {'maxfev': 50, 'xatol': 0.01, 'fatol': 1e-06}},
	add_zeros: False
}

Finished.

Above, we’ve read a .csv file and processed the data. The default sample mass of 1.0 mg was used. Luckily, the sample mass and all of the raw data units can be set at any time, before or after processing, with the appropriate conversions applied automatically.

Right now, the data looks like this:

data_heads()

Raw data:

T H M M_err M_per_mass M_per_mass_err dM_dT Delta_SM
unit K Oe emu emu emu/g emu/g cal/K/Oe/g cal/K/g
0 1.000000 20.000001 0.002023 0.00005 2.023238 0.05 4.245595e-23 8.491189e-22
1 2.000000 20.000000 0.001977 0.00005 1.977035 0.05 -1.692885e-09 -1.692885e-08
2 3.000001 19.999998 0.001969 0.00005 1.969118 0.05 -9.980543e-11 -9.980543e-10

Converted data:

T H M M_err M_per_mass M_per_mass_err dM_dT Delta_SM
unit K T A·m² A·m² A·m²/kg A·m²/kg J/K/T/kg J/K/kg
0 1.000000 0.002 0.000002 5.000000e-08 2.023238 0.05 1.776357e-15 3.552714e-18
1 2.000000 0.002 0.000002 5.000000e-08 1.977035 0.05 -7.083031e-02 -7.083031e-05
2 3.000001 0.002 0.000002 5.000000e-08 1.969118 0.05 -4.175859e-03 -4.175859e-06

Processed data:

T H M M_err M_per_mass M_per_mass_err dM_dT Delta_SM
unit K T A·m² A·m² A·m²/kg A·m²/kg J/K/T/kg J/K/kg
0 0.999999 0.002 0.000002 NaN 1.984700 NaN -0.009827 -0.00001
1 1.099098 0.002 0.000002 NaN 1.983727 NaN -0.009824 -0.00001
2 1.198198 0.002 0.000002 NaN 1.982753 NaN -0.009820 -0.00001

Sample mass#

To change the sample mass, use the sample_mass attribute:

magdata.sample_mass = 0.1
data_heads()

Raw data:

T H M M_err M_per_mass M_per_mass_err dM_dT Delta_SM
unit K Oe emu emu emu/g emu/g cal/K/Oe/g cal/K/g
0 1.000000 20.000001 0.002023 0.00005 20.232376 0.5 4.245595e-22 8.491189e-21
1 2.000000 20.000000 0.001977 0.00005 19.770351 0.5 -1.692885e-08 -1.692885e-07
2 3.000001 19.999998 0.001969 0.00005 19.691176 0.5 -9.980543e-10 -9.980543e-09

Converted data:

T H M M_err M_per_mass M_per_mass_err dM_dT Delta_SM
unit K T A·m² A·m² A·m²/kg A·m²/kg J/K/T/kg J/K/kg
0 1.000000 0.002 0.000002 5.000000e-08 20.232376 0.5 1.776357e-14 3.552714e-17
1 2.000000 0.002 0.000002 5.000000e-08 19.770351 0.5 -7.083031e-01 -7.083031e-04
2 3.000001 0.002 0.000002 5.000000e-08 19.691176 0.5 -4.175859e-02 -4.175859e-05

Processed data:

T H M M_err M_per_mass M_per_mass_err dM_dT Delta_SM
unit K T A·m² A·m² A·m²/kg A·m²/kg J/K/T/kg J/K/kg
0 0.999999 0.002 0.000002 NaN 19.847003 NaN -0.098265 -0.000098
1 1.099098 0.002 0.000002 NaN 19.837267 NaN -0.098240 -0.000098
2 1.198198 0.002 0.000002 NaN 19.827533 NaN -0.098202 -0.000098

All of the per-mass columns ('M_per_mass', 'M_per_mass_err', 'dM_dT', and 'Delta_SM') have increased by a factor of 10.

Similarly, one can change the units:

magdata.set_raw_data_units(sample_mass='g')
data_heads()
sample_mass will have units of g

Raw data:

T H M M_err M_per_mass M_per_mass_err dM_dT Delta_SM
unit K Oe emu emu emu/g emu/g cal/K/Oe/g cal/K/g
0 1.000000 20.000001 0.002023 0.00005 0.020232 0.0005 4.245595e-25 8.491189e-24
1 2.000000 20.000000 0.001977 0.00005 0.019770 0.0005 -1.692885e-11 -1.692885e-10
2 3.000001 19.999998 0.001969 0.00005 0.019691 0.0005 -9.980543e-13 -9.980543e-12

Converted data:

T H M M_err M_per_mass M_per_mass_err dM_dT Delta_SM
unit K T A·m² A·m² A·m²/kg A·m²/kg J/K/T/kg J/K/kg
0 1.000000 0.002 0.000002 5.000000e-08 0.020232 0.0005 1.776357e-17 3.552714e-20
1 2.000000 0.002 0.000002 5.000000e-08 0.019770 0.0005 -7.083031e-04 -7.083031e-07
2 3.000001 0.002 0.000002 5.000000e-08 0.019691 0.0005 -4.175859e-05 -4.175859e-08

Processed data:

T H M M_err M_per_mass M_per_mass_err dM_dT Delta_SM
unit K T A·m² A·m² A·m²/kg A·m²/kg J/K/T/kg J/K/kg
0 0.999999 0.002 0.000002 NaN 0.019847 NaN -0.000098 -9.826538e-08
1 1.099098 0.002 0.000002 NaN 0.019837 NaN -0.000098 -9.823999e-08
2 1.198198 0.002 0.000002 NaN 0.019828 NaN -0.000098 -9.820192e-08

The per-mass columns have now decreased by a factor of 1000.

pint’s unit string parsing is used to change the units. The logged output confirms that the unit change is as expected.

magdata.set_raw_data_units(sample_mass='gram')
magdata.set_raw_data_units(sample_mass='grams')
sample_mass will have units of g
sample_mass will have units of g

Both the magnitude and the units can be set at once using sample_mass_with_units:

magdata.sample_mass_with_units
(0.1, 'g')
magdata.sample_mass_with_units = (0.5, 'milligrams')
data_heads()
sample_mass will have units of mg

Raw data:

T H M M_err M_per_mass M_per_mass_err dM_dT Delta_SM
unit K Oe emu emu emu/g emu/g cal/K/Oe/g cal/K/g
0 1.000000 20.000001 0.002023 0.00005 4.046475 0.1 8.491189e-23 1.698238e-21
1 2.000000 20.000000 0.001977 0.00005 3.954070 0.1 -3.385770e-09 -3.385770e-08
2 3.000001 19.999998 0.001969 0.00005 3.938235 0.1 -1.996109e-10 -1.996109e-09

Converted data:

T H M M_err M_per_mass M_per_mass_err dM_dT Delta_SM
unit K T A·m² A·m² A·m²/kg A·m²/kg J/K/T/kg J/K/kg
0 1.000000 0.002 0.000002 5.000000e-08 4.046475 0.1 3.552714e-15 7.105427e-18
1 2.000000 0.002 0.000002 5.000000e-08 3.954070 0.1 -1.416606e-01 -1.416606e-04
2 3.000001 0.002 0.000002 5.000000e-08 3.938235 0.1 -8.351719e-03 -8.351719e-06

Processed data:

T H M M_err M_per_mass M_per_mass_err dM_dT Delta_SM
unit K T A·m² A·m² A·m²/kg A·m²/kg J/K/T/kg J/K/kg
0 0.999999 0.002 0.000002 NaN 3.969401 NaN -0.019653 -0.00002
1 1.099098 0.002 0.000002 NaN 3.967453 NaN -0.019648 -0.00002
2 1.198198 0.002 0.000002 NaN 3.965507 NaN -0.019640 -0.00002
magdata.sample_mass_with_units
(0.5, 'mg')

Column units#

The input temperature, magnetic field, and magnetic moment units can be changed using set_raw_data_units(). The moment error column will get the same units as the moment column. The last four columns are fixed in cgs units.

magdata.set_raw_data_units(T='degC', H='G', M='erg/G')
data_heads()
T will have units of °C
H will have units of G
M will have units of erg/G

Raw data:

T H M M_err M_per_mass M_per_mass_err dM_dT Delta_SM
unit °C G erg/G erg/G emu/g emu/g cal/K/Oe/g cal/K/g
0 1.000000 20.000001 0.002023 0.00005 4.046475 0.1 8.491189e-23 1.698238e-21
1 2.000000 20.000000 0.001977 0.00005 3.954070 0.1 -3.385770e-09 -3.385770e-08
2 3.000001 19.999998 0.001969 0.00005 3.938235 0.1 -1.996109e-10 -1.996109e-09

Converted data:

T H M M_err M_per_mass M_per_mass_err dM_dT Delta_SM
unit K T A·m² A·m² A·m²/kg A·m²/kg J/K/T/kg J/K/kg
0 274.150000 0.002 0.000002 5.000000e-08 4.046475 0.1 3.552714e-15 7.105427e-18
1 275.150000 0.002 0.000002 5.000000e-08 3.954070 0.1 -1.416606e-01 -1.416606e-04
2 276.150001 0.002 0.000002 5.000000e-08 3.938235 0.1 -8.351719e-03 -8.351719e-06

Processed data:

T H M M_err M_per_mass M_per_mass_err dM_dT Delta_SM
unit K T A·m² A·m² A·m²/kg A·m²/kg J/K/T/kg J/K/kg
0 274.149999 0.002 0.000002 NaN 3.969401 NaN -0.019653 -0.00002
1 274.249098 0.002 0.000002 NaN 3.967453 NaN -0.019648 -0.00002
2 274.348198 0.002 0.000002 NaN 3.965507 NaN -0.019640 -0.00002

A few things to note:

  1. The short string for “degrees Celcius” is 'degC'; 'C' stands for “Coulombs”.

  2. Changing the input units does not convert the input values themselves. Rather, it determines how they are converted to cgs and SI units. For example, the converted and processed data now begin at about 274.15 K. (Compare to the previous data output.)

  3. In a vacuum, the conversion from Gauss to Tesla is the same as that from Oersted to Tesla (divide by 10000), so the magnetic field column is unchanged.

  4. Similarly, 1 emu is defined to be 1 erg/Gauss in the context of measuring magnetic moments, so the magnetic moment column is unchanged.

  5. Concluding the perhaps underwhelming lack of changes in the data, the unit scale is the same for Celcius as it is for Kelvin, so the 'dM_dT' and 'Delta_SM' columns, which depend only on the derivative with respect to temperature, are unchanged.

Plotting#

Temperature and field units will be reflected in the default plot labels and titles when plotting the raw data.

import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, 2, figsize=(12, 5))

magdata.plot_lines(data_prop='M_per_mass', data_version='raw', ax=ax[0], legend=True)

magdata.plot_map(
    data_prop='M_per_mass', data_version='raw', ax=ax[1],
    colorbar_kwargs={'ax': ax, 'fraction': 0.05, 'pad': 0.01}
);
unit conversions plotting example

A note about regularization parameters#

In general, regularization parameters are dependent on the scale of the data. One might think it necessary, then, to re-process the data after changing the sample mass or raw data units in order to appropriately choose regularization parameters. Internally, however, smoothing is done using the raw values themselves, and conversions happen afterwards. This removes the need to re-process after conversions.