calamari.sample.Sample#

class calamari.sample.Sample(name, mass_mg=None, mass_source=None, molar_mass_g_per_mol=None, magnetic_ions_per_fu=None, shape=None, orientation=None, comment=None)#

Bases: object

The physical-sample facts needed to convert raw readings to physics units.

Frozen (immutable) per the house rule that nothing mutates a Measurement or Sample in place – derive a new Sample (e.g. via dataclasses.replace()) rather than editing one.

Every field defaults to None except name – there are no project-specific numeric defaults (no assumed mass, no assumed molar mass) because those facts are specific to whatever sample and compound the caller is actually measuring. Conversions that need a missing field raise ValueError naming it, rather than silently propagating None into arithmetic.

name#

Sample identifier (material name, crystal label, …).

Type:

str

mass_mg#

Sample mass in milligrams. See from_measurement() for how it is derived from a parsed file, and mass_source for how to tell afterwards where it came from.

Type:

float or None

mass_source#

Provenance of mass_mg: 'user' when the caller supplied it, 'file' when it was read from the measurement header, or None when there is no mass (or when the Sample was constructed directly rather than via from_measurement(), which is the only thing that records provenance – a directly built Sample leaves this None rather than inferring 'user', so None means “not recorded”, never “trust me”).

This exists because a mass that silently came from the wrong place is the most damaging thing that can happen to a magnetometry conversion – every muB-per-ion and molar-susceptibility number scales inversely with it. Recording the provenance turns a silent substitution into a checkable one:

if sample.mass_source == "file" and name in logbook:
    ratio = sample.mass_mg / logbook[name]
    if not 0.5 < ratio < 2.0:
        warn(f"header mass is {ratio:.0f}x the logbook value")
Type:

str or None

molar_mass_g_per_mol#

Molar mass of the compound, g/mol.

Type:

float or None

magnetic_ions_per_fu#

Number, or average number, of magnetic ions per formula unit (e.g. the number of rare-earth/transition-metal ions carrying the moment). Used by the “per ion” conversions and properties. Fractional occupancy is supported; the caller defines the counted species. Contributions from different magnetic species are not separated automatically.

Type:

float or None

shape#

Free-text sample shape/geometry note (e.g. 'plate', 'sphere').

Type:

str or None

orientation#

Free-text field/crystal-axis orientation note (e.g. 'H // c').

Type:

str or None

comment#

Free-text note, e.g. carried over from the measurement header.

Type:

str or None

property mass_g: float#

Sample mass in grams.

Raises:

ValueError – If mass_mg is None.

property moles: float#

Amount of substance, in moles of formula units.

Raises:

ValueError – If mass_mg or molar_mass_g_per_mol is None.

property formula_units: float#

Number of formula units in the sample (moles * N_A).

Raises:

ValueError – If mass_mg or molar_mass_g_per_mol is None (propagated from moles).

property moles_magnetic_ions: float#

Amount of substance, in moles of magnetic ions.

Raises:

ValueError – If mass_mg, molar_mass_g_per_mol, or magnetic_ions_per_fu is None.

moment_emu_to_muB_per_fu(moment_emu)#

Convert a measured moment (emu) to Bohr magnetons per formula unit.

The number of formula units in the sample is FU = moles * N_A (formula_units), and muB_per_fu = moment_emu * MU_B_PER_EMU / FU, using the exact emu<->Bohr-magneton factor from calamari.constants. Older analyses often round that factor to 1.07828e20, which differs only in the 5th significant figure.

Parameters:

moment_emu (float, numpy.ndarray, or pandas.Series) – Measured moment in emu. Returned in the same shape/type.

Returns:

float, numpy.ndarray, or pandas.Series – Moment in Bohr magnetons per formula unit.

Raises:

ValueError – If mass_mg or molar_mass_g_per_mol is None (propagated from formula_units).

moment_emu_to_muB_per_ion(moment_emu)#

Convert a measured moment (emu) to Bohr magnetons per magnetic ion.

Equivalent to moment_emu_to_muB_per_fu() divided by magnetic_ions_per_fu. The ion species is caller-defined via magnetic_ions_per_fu, never assumed by this library.

Parameters:

moment_emu (float, numpy.ndarray, or pandas.Series) – Measured moment in emu. Returned in the same shape/type.

Returns:

float, numpy.ndarray, or pandas.Series – Moment in Bohr magnetons per magnetic ion.

Raises:

ValueError – If mass_mg, molar_mass_g_per_mol, or magnetic_ions_per_fu is None.

moment_to_chi_molar(moment_emu, field_oe)#

Molar susceptibility, emu/(mol Oe), per mole of formula units.

chi_molar = (moment_emu / field_oe) / moles, i.e. the moment per unit applied field normalised by the amount of substance (moles, formula-unit basis). Field-dependent (non-linear) moments should be evaluated in a regime where moment/field is a meaningful susceptibility (e.g. the low-field limit) – this method performs no linearity check.

Parameters:
Returns:

float, numpy.ndarray, or pandas.Series – Molar susceptibility, emu/(mol Oe), per mole of formula units.

Raises:

ValueError – If mass_mg or molar_mass_g_per_mol is None.

moment_to_chi_molar_per_ion(moment_emu, field_oe)#

Molar susceptibility, emu/(mol Oe), per mole of magnetic ions.

As moment_to_chi_molar() but normalised by moles_magnetic_ions instead of moles, i.e. per mole of magnetic ion rather than per mole of formula unit – the natural basis for comparing a Curie constant to a single-ion effective moment (see mu_eff_from_curie_constant()).

Parameters:
Returns:

float, numpy.ndarray, or pandas.Series – Molar susceptibility, emu/(mol Oe), per mole of magnetic ions.

Raises:

ValueError – If mass_mg, molar_mass_g_per_mol, or magnetic_ions_per_fu is None.

chi_mass(moment_emu, field_oe)#

Mass susceptibility, emu/(g Oe).

chi_mass = (moment_emu / field_oe) / mass_g. Useful when the molar mass is not known (e.g. an unidentified phase) but the sample mass is.

Parameters:
Returns:

float, numpy.ndarray, or pandas.Series – Mass susceptibility, emu/(g Oe).

Raises:

ValueError – If mass_mg is None.

chi_to_chi_molar(chi_emu_per_oe)#

Molar susceptibility, emu/(mol Oe), from a susceptibility in emu/Oe.

The AC-side counterpart of moment_to_chi_molar(): chi_real exposes susceptibility in emu/Oe, so normalising it needs no field – just the amount of substance. chi_molar = chi_emu_per_oe / moles.

Parameters:

chi_emu_per_oe (float, numpy.ndarray, or pandas.Series) – Susceptibility in emu/Oe (the extensive, whole-sample quantity the instrument reports).

Returns:

float, numpy.ndarray, or pandas.Series – Molar susceptibility, emu/(mol Oe), per mole of formula units.

Raises:

ValueError – If mass_mg or molar_mass_g_per_mol is None.

chi_to_chi_molar_per_ion(chi_emu_per_oe)#

Molar susceptibility, emu/(mol Oe), per mole of magnetic ions.

As chi_to_chi_molar() but normalised by moles_magnetic_ions – the AC-side counterpart of moment_to_chi_molar_per_ion().

Raises:

ValueError – If mass_mg, molar_mass_g_per_mol, or magnetic_ions_per_fu is None.

chi_to_chi_mass(chi_emu_per_oe)#

Mass susceptibility, emu/(g Oe), from a susceptibility in emu/Oe.

As chi_to_chi_molar() but normalised by mass_g – the AC-side counterpart of chi_mass(), for when the molar mass is not known but the sample mass is.

Raises:

ValueError – If mass_mg is None.

classmethod from_measurement(m, *, mass_mg=None, **overrides)#

Build a Sample from a parsed file’s header metadata.

The mass is resolved by a single rule – the caller wins, then the file – and where it came from is recorded on mass_source:

mass_mg given?

File records a mass?

Result

yes

either

caller’s value, 'user'

no

yes

file’s value, 'file'

no

no

None, None

The file mass comes from SAMPLE_MASS through sample_mass_mg. This is an operator-entered value, not an independently verified specimen fact. Check it against the weighing record before using mass normalisation.

The sample name defaults to m.sample_name (falling back to 'unknown' if the header carries none); every other Sample field (molar_mass_g_per_mol, magnetic_ions_per_fu, shape, orientation, comment) has no file-derived source and must be supplied via **overrides if needed.

Parameters:
  • m (Measurement) – Parsed measurement (see calamari.io).

  • mass_mg (float | None) – Sample mass in mg, overriding whatever the file records. Passing None (the default) is not an assertion that there is no mass – it simply means “use the file’s, if it has one”, so mass_mg=logbook.get(name) behaves sensibly for a sample you have no logbook entry for. Has no project-specific default; callers own it.

  • **overrides – Additional Sample fields, forwarded to the constructor. These are applied last, so passing mass_source here overrides the derived provenance – don’t, unless you are deliberately re-labelling it.

Return type:

Sample

Returns:

calamari.sample.Sample