JaminMartin / spcs_instruments

An experiment manager and instrument control library written in Python and Rust
0 stars 1 forks source link

Plotting Results #2

Closed davidrrwalker closed 3 weeks ago

davidrrwalker commented 1 month ago

Hi Jamin,

Perhaps there could be an easy way to extract the measurement data from the logfile and subsequently plot the data.

Cheers, David

JaminMartin commented 1 month ago

Hi David,

I have just made a major change to the data logging process. It is now entirely in TOML format.

[experiment]
start_time = "18-07-2024 21:10:05.415"
end_time = "18-07-2024 21:10:05.421"

[experiment.info]
name = "John Doe"
email = "test@canterbury.ac.nz"
experiment_name = "Test Experiment"
experiment_description = "This is a test experiment"

[Test_DAQ]
averages = 40
gate_time = 1000

[Test_DAQ.data]
counts = [8983.487839935988, 860.9849948780346, 1049.4660716379633, 8972.287989438386, 1118.2829760283619, 5075.440946215393, 9789.348249900506, 4471.92143692627, 7997.331511318171, 2872.420489781416]
current = [8983.487839935988, 860.9849948780346, 1049.4660716379633, 8972.287989438386, 1118.2829760283619, 5075.440946215393, 9789.348249900506, 4471.92143692627, 7997.331511318171, 2872.420489781416]

Which can be read in Python using the tool library as a Python dictionary.

import toml
import matplotlib.pyplot as plt
# Load the TOML file
with open('path/to/your_file.toml', 'r') as file:
    toml_data = toml.load(file)

# Extract the data from the [Test_DAQ.data] section
test_daq_data = toml_data.get('Test_DAQ', {}).get('data', {})
plt.plot(data['counts'], data["current")
plt.show()

However, I am sure we can make a function that reads through all the experimental data and returns it from the experimental results file. It might be useful if multiple things are logging data.

JaminMartin commented 3 weeks ago

Hi @davidrrwalker ,

I have added a new feature to v0.2.2 of spcs_instruments (4c6ed44). There is now a function that takes the path to the experiment file and returns all the keys (say the name of the instrument) and their corresponding data.

from spcs_instruments import pyfex #this is the main library function used for data handling in spcs_instruments) 
data = pyfex.load_experimental_data("path/to/experimnetal/logfile")
print(data) 
# this will print a dictionary of all the keys (instruments and their associated data)

instrument1 = data.get("Instrument_1") # will create a dictionary of all the keys and value data associated with that instrument, e.g. counts and voltage. 

Should make importing data from the logs much easier. :)

You should be able to then plot:

plt.plot(instrument1["counts"], instrument1["voltage"])