Install and import packages¶
import pmagpy.rockmag as rmag
import pmagpy.ipmag as ipmag
import pmagpy.contribution_builder as cb
import pandas as pd
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
output_notebook()Import local data in MagIC format¶
In this demonstration we will be using local data.
The data is from the following publication:
Swanson-Hysell, N. L., Avery, M. S., Zhang, Y., Hodgin, E. B., Sherwood, R. J., Apen, F. E., et al. (2021). The paleogeography of Laurentia in its early years: New constraints from the Paleoproterozoic East-Central Minnesota Batholith. Tectonics, 40, e2021TC006751. Swanson‐Hysell et al. (2021)
# set the dir_path to the directory where the measurements.txt file is located
dir_path = '../example_data/ECMB'
# set the name of the MagIC file
ipmag.unpack_magic('magic_contribution_20213.txt',
dir_path = dir_path,
input_dir_path = dir_path,
print_progress=False)
# create a contribution object from the tables in the directory
contribution = cb.Contribution(dir_path)
measurements = contribution.tables['measurements'].df
specimens = contribution.tables['specimens'].df1 records written to file /private/tmp/claude-501/-Users-hematite-Documents-GitHub-PmagPy/167d67da-2cda-4fca-810e-f539c9908e87/scratchpad/rmnb2/example_data/ECMB/contribution.txt
1 records written to file /private/tmp/claude-501/-Users-hematite-Documents-GitHub-PmagPy/167d67da-2cda-4fca-810e-f539c9908e87/scratchpad/rmnb2/example_data/ECMB/locations.txt
90 records written to file /private/tmp/claude-501/-Users-hematite-Documents-GitHub-PmagPy/167d67da-2cda-4fca-810e-f539c9908e87/scratchpad/rmnb2/example_data/ECMB/sites.txt
312 records written to file /private/tmp/claude-501/-Users-hematite-Documents-GitHub-PmagPy/167d67da-2cda-4fca-810e-f539c9908e87/scratchpad/rmnb2/example_data/ECMB/samples.txt
1574 records written to file /private/tmp/claude-501/-Users-hematite-Documents-GitHub-PmagPy/167d67da-2cda-4fca-810e-f539c9908e87/scratchpad/rmnb2/example_data/ECMB/specimens.txt
17428 records written to file /private/tmp/claude-501/-Users-hematite-Documents-GitHub-PmagPy/167d67da-2cda-4fca-810e-f539c9908e87/scratchpad/rmnb2/example_data/ECMB/measurements.txt
-I- Using online data model
-I- Getting method codes from earthref.org
-I- Using cached vocabularies
-I- Using cached suggested vocabularies
Get hysteresis data using the method code¶
The method codes relevant to hysteresis loops are:
LP-HYSfor regular hysteresis loopsLP-HYS-Ofor hysteresis loops as a function of orientationLP-HYS-Tfor hysteresis loops as a function of temperature
We can filter the data to only have data for LP-HYS.
hyst_measurements = measurements[measurements['method_codes'] == 'LP-HYS']We can see what experimental hysteresis data are available in this dataset:
hyst_experiments = rmag.make_experiment_df(hyst_measurements)
hyst_experimentsProcess hysteresis data for one experiment¶
We can process the data from one of the experiments using the rmag.process_hyst_loop function. The processing steps behind the function are described in the hysteresis
#specify the experiment name and specimen name
experiment_name = 'IRM-VSM3-LP-HYS-218845'
specimen_name = 'NED1-5c'
experiment_hyst = measurements[measurements['experiment'] == experiment_name].reset_index(drop=True)
experiment_results = rmag.process_hyst_loop(experiment_hyst['meas_field_dc'].values, experiment_hyst['magn_mass'].values, specimen_name)Process hysteresis data for all experiments¶
The decision tree handles the full range of loop behavior within a batch: statistically linear loops terminate with only χHF reported (the fit_linear_loop=True argument overrides this exit when full processing is desired), and every other loop is processed in full. Loop closure at high field is tested and reported rather than acted on: each row of the results table carries closure_state ('closed', 'open' or 'indeterminate') and the openness statistic HF_Mrh_fraction (, the high-field Mrh as a fraction of Mr) with its standard error, loops that are open or of unresolved closure are flagged with a printed -W- line, and a summary line reports how many were flagged. The openness_tolerance argument (default 0.05) sets the level of at which a loop is flagged. When results are written to the specimens table, add_hyst_stats_to_specimens_table(..., exclude_open=True) withholds Ms, Bc and χHF for open loops while keeping Mr and Brh.
results = rmag.process_hyst_loops(hyst_experiments,measurements)-I- 0 of 5 loops open at high field, 0 of unresolved closure, 0 statistically linear; see the 'closure_state', 'HF_Mrh_fraction' and 'loop_is_linear' columns
Export hysteresis loop statistics into a MagIC specimens data table¶
The add_hyst_stats_to_specimens_table function adds the summary hysteresis parameters (hyst_ms_mass, hyst_mr_mass, hyst_bc, hyst_xhf) to the MagIC columns of the specimens table, with the additional quality statistics (Q, Qf, FNL values, sigma, etc.) recorded in the description column.
specimens = rmag.add_hyst_stats_to_specimens_table(specimens, results)specimensThe updated table can then be written out as a MagIC-format specimens.txt file:
contribution.tables['specimens'].df = specimens
contribution.tables['specimens'].write_magic_file(dir_path=dir_path)-I- overwriting /private/tmp/claude-501/-Users-hematite-Documents-GitHub-PmagPy/167d67da-2cda-4fca-810e-f539c9908e87/scratchpad/rmnb2/example_data/ECMB/specimens.txt
-I- 1574 records written to specimens file
'/private/tmp/claude-501/-Users-hematite-Documents-GitHub-PmagPy/167d67da-2cda-4fca-810e-f539c9908e87/scratchpad/rmnb2/example_data/ECMB/specimens.txt'Processing hysteresis data that are not in MagIC format¶
While the workflow above is built around MagIC-formatted data — the recommended practice, since it keeps measurement-level data, metadata, and derived parameters together — the processing functions themselves operate on simple sequences of field and magnetization values. This makes these algorithms accessible to data coming from any stream: an instrument export, a spreadsheet, or a colleague’s text file.
A few conventions to be aware of:
Field values are expected in tesla. The high-field susceptibility unit conversions assume tesla, and a warning is printed if the field values appear to be in mT or Oe.
Magnetization can be in any consistent unit. Mass-normalized Am²/kg matches MagIC conventions and makes the resulting Ms and Mr directly comparable to database values.
Inputs are cleaned automatically by
sanitize_hyst_inputs: plain Python lists, numeric strings, and dataframe columns are all accepted; measurement pairs with non-finite values are dropped with a report; and loops measured in either field sweep order (starting from positive or negative saturation) are handled.
Below we process a plain two-column CSV file (the NED18-2c loop exported with field_tesla and moment_Am2_per_kg columns):
plain_data = pd.read_csv('../example_data/plain_text/NED18_2c_hysteresis.csv')
plain_result = rmag.process_hyst_loop(plain_data['field_tesla'],
plain_data['moment_Am2_per_kg'],
'NED18-2c (from csv)')- Swanson‐Hysell, N. L., Avery, M. S., Zhang, Y., Hodgin, E. B., Sherwood, R. J., Apen, F. E., Boerboom, T. J., Keller, C. B., & Cottle, J. M. (2021). The Paleogeography of Laurentia in Its Early Years: New Constraints From the Paleoproterozoic East‐Central Minnesota Batholith. Tectonics, 40(5). 10.1029/2021tc006751