Created
October 10, 2024 15:39
-
-
Save rien333/ba7087cc7dbb36a048be7961f6ea41d4 to your computer and use it in GitHub Desktop.
sensors_graph.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from collections import defaultdict | |
import matplotlib.pyplot as plt | |
import matplotlib as mpl | |
import matplotlib.cm as cm | |
import numpy as np | |
import math | |
# Map sensors to their unit of measurement. | |
sensor_measurement_mapping = {'amdgpu-pci-c100.vddgfx.in0_input' : 'V', | |
'amdgpu-pci-c100.vddnb.in1_input' : 'mV', | |
'amdgpu-pci-c100.edge.temp1_input' : '℃', | |
'amdgpu-pci-c100.PPT.power1_input' : 'W', | |
'cros_ec-isa-0000.fan1.fan1_input' : 'RPM', | |
'[email protected]_input' : '℃', | |
'[email protected]_input' : '℃', | |
'[email protected]_input' : '℃', | |
'[email protected]_input' : '℃', | |
'ucsi_source_psy_USBC000:004-isa-0000.in0.in0_input' : 'V', | |
'ucsi_source_psy_USBC000:004-isa-0000.curr1.curr1_input' : 'A', | |
'spd5118-i2c-2-50.temp1.temp1_input' : '℃', | |
'ucsi_source_psy_USBC000:002-isa-0000.in0.in0_input' : 'V', | |
'ucsi_source_psy_USBC000:002-isa-0000.curr1.curr1_input' : 'A', | |
'k10temp-pci-00c3.Tctl.temp1_input' : '℃', | |
'BAT1-acpi-0.in0.in0_input' : 'V', | |
'BAT1-acpi-0.curr1.curr1_input' : 'A', | |
'ucsi_source_psy_USBC000:001-isa-0000.in0.in0_input' : 'V', | |
'ucsi_source_psy_USBC000:001-isa-0000.curr1.curr1_input' : 'A', | |
'mt7921_phy0-pci-0100.temp1.temp1_input' : '℃', | |
'spd5118-i2c-2-51.temp1.temp1_input' : '℃', | |
'ucsi_source_psy_USBC000:003-isa-0000.in0.in0_input' : 'V', | |
'ucsi_source_psy_USBC000:003-isa-0000.curr1.curr1_input' : 'A', | |
'framework_laptop-isa-0000.fan1.fan1_input' : 'RPM', | |
'nvme-pci-0200.Composite.temp1_input' : '℃', | |
'nvme-pci-0200.Sensor 1.temp2_input' : '℃', | |
'nvme-pci-0200.Sensor 2.temp3_input' : '℃', | |
'acpitz-acpi-0.temp1.temp1_input' : '℃', | |
'acpitz-acpi-0.temp2.temp2_input' : '℃', | |
'acpitz-acpi-0.temp3.temp3_input' : '℃', | |
'acpitz-acpi-0.temp4.temp4_input' : '℃'} | |
# do not produce plots of these sensors | |
disabled_sensors = ['ucsi_source_psy_USBC000:004-isa-0000.in0.in0_input', 'ucsi_source_psy_USBC000:002-isa-0000.curr1.curr1_input', 'ucsi_source_psy_USBC000:001-isa-0000.in0.in0_input', 'ucsi_source_psy_USBC000:003-isa-0000.in0.in0_input', 'ucsi_source_psy_USBC000:002-isa-0000.in0.in0_input', 'BAT1-acpi-0.curr1.curr1_input', 'ucsi_source_psy_USBC000:004-isa-0000.curr1.curr1_input', 'ucsi_source_psy_USBC000:001-isa-0000.curr1.curr1_input', 'ucsi_source_psy_USBC000:003-isa-0000.curr1.curr1_input'] | |
font = {'size' : 19} | |
mpl.rc('font', **font) | |
readings_by_sensor = defaultdict(list) | |
with open('readings.json', 'r') as f: | |
readings = json.load(f) | |
# populate readings by sensor | |
for r in readings: | |
for k, v in r.items(): | |
if k == 'date' or k in disabled_sensors: | |
continue | |
readings_by_sensor[k].append(v) | |
# Determine the number of subplots needed | |
num_plots = len(readings_by_sensor) | |
cols = 3 # Number of columns | |
rows = math.ceil(num_plots / cols) # Determine the required number of rows | |
# Create the subplots | |
fig, axs = plt.subplots(rows, cols, figsize=(50, 4.6 * rows)) | |
axs = axs.flatten() | |
# Iterate through the dictionary and subplots | |
for idx, (sensor, values) in enumerate(readings_by_sensor.items()): | |
# 3 == time steps (i.e. poll every three seconds) | |
axs[idx].plot(range(0, len(values)*3, 3), values,) | |
axs[idx].set_title(sensor, pad=20, fontsize=23, weight='bold') # Set the title to the dictionary key | |
axs[idx].set_xlabel('time (s)', fontsize=24) | |
axs[idx].set_ylabel(sensor_measurement_mapping[sensor], rotation=0, fontsize=24, labelpad=20) | |
# Remove empty subplots, if any | |
for i in range(num_plots, len(axs)): | |
fig.delaxes(axs[i]) | |
plt.tight_layout() # Adjust layout to prevent overlap | |
plt.subplots_adjust(hspace=0.5, wspace=0.15) # increase padding between subplts | |
plt.savefig('plot.pdf') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment