Skip to content

Commit

Permalink
mv save_figure to functions.py, update readme example code
Browse files Browse the repository at this point in the history
  • Loading branch information
gituser789 committed Dec 11, 2024
1 parent d8b6235 commit 89bb6ad
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 24 deletions.
34 changes: 34 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,37 @@ Example usage
-------------
pySignalScope helps to load, edit, display and analyze the signals.
The following application example loads a noisy measurement signal, which is first filtered.


::

import pysignalscope as pss

# Read curves from scope csv file
[voltage_prim, voltage_sec, current_prim, current_sec] = pss.Scope.from_tektronix('scope_example_data_tek.csv')

# Add labels and units to channel: This example considers the Channel 'current_prim' only
current_prim = pss.Scope.modify(current_prim, label='current measured', unit='A', color='red')

# Low pass filter the noisy current signal, modify the Channel attributes label, color and linestyle
current_prim_filtered = pss.Scope.low_pass_filter(current_prim)
current_prim_filtered = pss.Scope.modify(current_prim_filtered, label='current filtered', linestyle='--', color='green')

# Make some modifications on the signal itself: data offset, time offset and factor to the data.
# Short the channel to one period and add label, color and linestyle to the Channel
current_prim_filtered_mod = pss.Scope.modify(current_prim_filtered, data_factor=1.3, data_offset=11, time_shift=2.5e-6)
current_prim_filtered_mod = pss.Scope.short_to_period(current_prim_filtered_mod, f0=200000)
current_prim_filtered_mod = pss.Scope.modify(current_prim_filtered_mod, label='current modified', linestyle='-', color='orange')

# Plot channels, save as pdf
fig1 = pss.Scope.plot_channels([current_prim, current_prim_filtered], [current_prim_filtered_mod], timebase='us')
pss.save_figure(fig1, 'figure.pdf')

# short channels to a single period, perform FFT for current waveforms
current_prim = pss.Scope.short_to_period(current_prim, f0=200000)
current_prim = pss.Scope.modify(current_prim, time_shift=5e-6)
pss.Scope.fft(current_prim)

To simplify the display, colors, linestyle and the label can be attached to the object.
This is shown in the plot above.

Expand All @@ -53,6 +84,9 @@ The signals are then plotted with just one plot command.

.. image:: https://raw.githubusercontent.com/upb-lea/pySignalScope/main/docs/source/figures/function_overview.png




Have a look at the `Scope example <https://github.com/upb-lea/pySignalScope/blob/main/examples/scope_example.py>`__ and at the `Impedance example <https://github.com/upb-lea/pySignalScope/blob/main/examples/impedance_example.py>`__ to see what you can do with this toolbox.

Naming convention
Expand Down
3 changes: 2 additions & 1 deletion examples/impedance_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
print(f"{recalculated_c=}")

# plot multiple channel data
pss.Impedance.plot_impedance([example_data_0mm5, example_data_1mm5, example_data_rlc])
impedance_figure = pss.Impedance.plot_impedance([example_data_0mm5, example_data_1mm5, example_data_rlc])
pss.save_figure(impedance_figure, "impedance_fig.pdf")

# save and load an impedance object
pss.Impedance.save(example_data_rlc, "example")
Expand Down
4 changes: 2 additions & 2 deletions examples/scope_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
current_prim = pss.Scope.modify(current_prim, label='current primary', unit='A')
current_sec = pss.Scope.modify(current_sec, label='current secondary', unit='A')

# Show gain and DC-offset
# Gain and DC-offset for the secondary current
current_sec = pss.Scope.modify(current_sec, data_factor=1.3, data_offset=10)

# Plot channels
fig1 = pss.Scope.plot_channels([voltage_prim, voltage_sec], [current_prim, current_sec], timebase='us')
pss.Scope.save_figure(fig1, 'test')
pss.save_figure(fig1, 'scope_fig.pdf')

# Shift first two channels and plot the shift of these channels
shiftlist = pss.Scope.plot_shiftchannels([voltage_prim, voltage_sec])
Expand Down
17 changes: 17 additions & 0 deletions pysignalscope/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
from typing import Union, Tuple, Optional, List
import numpy as np

def save_figure(figure: plt.figure, fig_name: str):
"""
Save the given figure object as pdf.
:param figure: figure object
:type figure: matplotlib.pyplot.figure
:param fig_name: figure name for pdf file naming
:type fig_name: str
"""
if isinstance(fig_name, str):
if fig_name.endswith(".pdf"):
figure.savefig(f"{fig_name}")
else:
figure.savefig(f"{fig_name}.pdf")
else:
raise TypeError("figure name must be of type str.")


def fft(period_vector_t_i: Union[List[List[float]], np.ndarray], sample_factor: int = 1000, plot: bool = True, mode: str = 'rad',
f0: Optional[float] = None, title: str = 'ffT', filter_type: str = 'factor',
Expand Down
9 changes: 6 additions & 3 deletions pysignalscope/impedance.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,16 @@ def from_kemet_ksim(csv_filename: str) -> 'ImpedanceChannel':
source='https://ksim3.kemet.com/capacitor-simulation')

@staticmethod
def plot_impedance(channel_list: List, figure_size: Tuple = None) -> None:
def plot_impedance(channel_list: List, figure_size: Tuple = None) -> plt.figure:
"""
Plot and compare impedance channels.
:param channel_list: List with impedances
:type channel_list: List
:param figure_size: figure size as tuple in mm, e.g. (80, 80)
:type figure_size: Tuple
:return: matplotlib figure
:rtype: plt.figure
"""
fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, sharex=True, figsize=[x / 25.4 for x in figure_size] if figure_size is not None else None)
for channel in channel_list:
Expand All @@ -286,6 +288,7 @@ def plot_impedance(channel_list: List, figure_size: Tuple = None) -> None:
ax2.legend()
plt.tight_layout()
plt.show()
return fig

@staticmethod
def plot_inductance_and_ac_resistance(channel_list: List) -> None:
Expand Down Expand Up @@ -419,7 +422,7 @@ def calc_rlc(channel: ImpedanceChannel, type_rlc: str, f_calc_c: float, f_calc_l
color=color_calculation, label='recalculated data')
ax1.grid()
ax1.legend()
ax1.set(xlabel='Frequency in Hz', ylabel=r'Impedance in $\Omega$')
ax1.set(xlabel=f'Frequency {Impedance.unit_separator_plot} Hz', ylabel=rf'Impedance {Impedance.unit_separator_plot} $\Omega$')
ax1.plot(f_calc_c, z_calc_c, marker=markerstyle, color=color_measurement)
ax1.plot(f_calc_r, z_calc_r, marker=markerstyle, color=color_measurement)
ax1.plot(f_calc_l, z_calc_l, marker=markerstyle, color=color_measurement)
Expand All @@ -429,7 +432,7 @@ def calc_rlc(channel: ImpedanceChannel, type_rlc: str, f_calc_c: float, f_calc_l
ax2.semilogx(recalculated_curve.frequency, recalculated_curve.phase_deg, linestyle=linestyle_calculation, color=color_calculation,
label='recalculated data')
ax2.grid()
ax2.set(xlabel='Frequency in Hz', ylabel='Phase in degree')
ax2.set(xlabel=f'Frequency {Impedance.unit_separator_plot} Hz', ylabel=f'Phase {Impedance.unit_separator_plot} °')
ax2.legend()
ax2.plot(f_calc_c, phase_calc_c, marker=markerstyle, color=color_measurement)
ax2.plot(f_calc_r, phase_calc_r, marker=markerstyle, color=color_measurement)
Expand Down
18 changes: 0 additions & 18 deletions pysignalscope/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -1707,24 +1707,6 @@ def square(channel: Channel) -> Channel:

return channel_modified

@staticmethod
def save_figure(figure: plt.figure, fig_name: str):
"""
Save the given figure object as pdf.
:param figure: figure object
:type figure: matplotlib.pyplot.figure
:param fig_name: figure name for pdf file naming
:type fig_name: str
"""
if isinstance(fig_name, str):
figure.savefig(f"{fig_name}.pdf")
else:
raise TypeError("figure name must be of type str.")

# Log flow control
logging.debug(f"{class_modulename} :Name of file to save={fig_name}.pdf")

@staticmethod
def save(channel: Channel, filepath: str) -> None:
"""
Expand Down

0 comments on commit 89bb6ad

Please sign in to comment.