Reading a .dat File¶
The DatFile
class reads and stores data from a .dat file from a Quantum Design MPMPS magnetometer. Create the DatFile
object by passing the path to the .dat file to the constructor.
from pathlib import Path
import magnetopy as mp
DATA_PATH = Path("../../tests/data")
mvsh1 = mp.DatFile(DATA_PATH / "mvsh1.dat")
The data
¶
DatFile
creation executes some low-level parsing of the .dat file. For example, the file header and data sections are separated into header
and data
attributes. The data
attribute is a Pandas DataFrame
and gives access to the original data in a convenient form.
mvsh1.data
Comment | Time Stamp (sec) | Temperature (K) | Magnetic Field (Oe) | Moment (emu) | M. Std. Err. (emu) | Transport Action | Averaging Time (sec) | Frequency (Hz) | Peak Amplitude (mm) | ... | Map 07 | Map 08 | Map 09 | Map 10 | Map 11 | Map 12 | Map 13 | Map 14 | Map 15 | Map 16 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | NaN | 3803627317 | 2.000165 | 70000.37500 | 0.736924 | 0.000996 | 1 | 1 | 13.006381 | 0.999015 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1 | NaN | 3803627320 | 2.000241 | 69995.39844 | 0.736522 | 0.001055 | 1 | 1 | 13.006381 | 0.999028 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
2 | NaN | 3803627325 | 1.999892 | 69746.85938 | 0.737400 | 0.001470 | 1 | 1 | 13.006381 | 0.999024 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
3 | NaN | 3803627334 | 2.000141 | 69286.15625 | 0.736039 | 0.000992 | 1 | 1 | 13.006381 | 0.999066 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
4 | NaN | 3803627335 | 1.999827 | 69246.48438 | 0.737444 | 0.001020 | 1 | 1 | 13.006381 | 0.999066 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
7300 | NaN | 3814233888 | 300.000000 | -69003.28906 | -0.067261 | 0.000070 | 1 | 1 | 13.006381 | 0.999186 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
7301 | NaN | 3814233893 | 299.999634 | -69254.71094 | -0.067489 | 0.000071 | 1 | 1 | 13.006381 | 0.999208 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
7302 | NaN | 3814233898 | 300.000000 | -69508.75000 | -0.067781 | 0.000076 | 1 | 1 | 13.006381 | 0.999197 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
7303 | NaN | 3814233902 | 299.999451 | -69750.06250 | -0.067944 | 0.000072 | 1 | 1 | 13.006381 | 0.999192 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
7304 | NaN | 3814233911 | 300.000183 | -70000.16406 | -0.068273 | 0.000078 | 1 | 1 | 13.006381 | 0.999175 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
7305 rows × 89 columns
Serialization¶
Serializing the DatFile
object via the as_dict()
method shows some of the other class attributes. Most of them are important solely for record keeping.
mvsh1.as_dict()
{'experiment_type': 'magnetometry', 'local_path': '../../tests/data/mvsh1.dat', 'length': 2604806, 'date_created': '2020-07-11T11:07:00', 'sha512': 'f9f8ac5d5b84cc00a57073d330c0f8b005e90201eadc588649388a35666f427bdc6beaa9c8ef5d58e70099a54ced7d9006f9800b8014639e790a2f0423acd6a5', 'experiments_in_file': ['mvsh']}
Determining the Types of Experiments in the File¶
The experiments_in_file
attribute is useful for higher level classes (particularly the Dataset
class) to determine how to further handle DatFile
objects. In this case, mvsh1.dat
contains the mvsh
(aka M vs. H, variable field magnetization, or hysteresis) experiment. Actually it contains several mvsh
experiments at different temperatures, but the separation of that data is dealt with by the MvsH
class. For now it's just helpful to have a list of the types of experiments in the file.
mvsh1.experiments_in_file
['mvsh']
Let's look at some other files and see what's inside of them.
zfcfc1 = mp.DatFile(DATA_PATH / "zfcfc1.dat")
zfcfc1.experiments_in_file
['zfcfc']
zfc4 = mp.DatFile(DATA_PATH / 'zfc4a.dat')
zfc4.experiments_in_file
['zfc']
fc4 = mp.DatFile(DATA_PATH / "fc4a.dat")
fc4.experiments_in_file
['fc']
In these cases MagnetoPy is getting information from the file names, but it can also determine the experiments from the file content. The following cell creates a copy of "zfcfc1.dat" named "unlabeled_file.dat" and determines the experiments from the actual data.
import shutil
import os
shutil.copy(DATA_PATH / "zfcfc1.dat", DATA_PATH / "unlabeled_file.dat")
unlabeled = mp.DatFile(DATA_PATH / "unlabeled_file.dat")
os.remove(DATA_PATH / "unlabeled_file.dat")
unlabeled.experiments_in_file
['zfcfc']
Here's a file with several experiments inside
dataset4 = mp.DatFile(DATA_PATH / "dataset4.dat")
dataset4.experiments_in_file
['zfc', 'fc', 'zfc', 'fc', 'mvsh']
Experiment Determination Method¶
The experiment determination is done by a class method _get_experiments_in_file
shown here:
def _get_experiments_in_file(self) -> list[str]:
experiments = []
if self.comments:
for comments in self.comments.values():
for comment in comments:
if comment.lower() in ["mvsh", "zfc", "fc", "zfcfc"]:
experiments.append(comment.lower())
elif (filename := filename_label(self.local_path.name, "", True)) != "unknown":
experiments.append(filename)
else:
if len(self.data["Magnetic Field (Oe)"].unique()) == 1:
experiments.append("zfcfc")
else:
experiments.append("mvsh")
return experiments
So the order of precedence is:
- Comments in the file. Note that these are comments in the "[DATA]" section of the file, which are inserted using the "Add comment" option in MultiVu used when creating the sequence. This allows for multiple experiments to be easily read from a single file, as was done in "dataset4.dat" in the example above.
- Filename. This is particularly useful for
"fc"
and"zfc"
experiments in files without comments, as the method used in #3 below will not be able to distinguish between the two. - Reading the data. With knowledge of what typical experiments and files look like, we can make some assumptions about the data. For example, distinguishing between a "zfcfc" file and an "mvsh" file relies on the fact that typically files containing "zfcfc" experiments will only have one unique magnetic field value.
As is discussed in the File Formatting page, commented files are strongly encouraged, as it removes any ambiguity in the experiment determination.