Utilities

This module provides various utility functions used across the CompSensePack library.

utils.py - Utility functions for formatted printing, saving to CSV, and loading ECG signals.

This module provides utility functions for: 1. Printing NumPy matrices in a formatted way. 2. Saving matrices as CSV files to the project root’s Outputs directory. 3. Loading ECG signals from the MIT-BIH Arrhythmia Database using the wfdb package.

All output files from this module will be stored in the Outputs directory located in the root of the project:

(root)/Outputs/py_test_csv/ for saving CSV files.

Note:

  • Requires the following third-party libraries: numpy, wfdb.

  • Make sure wfdb is properly installed and configured to access PhysioNet databases.

Example usage:

>>> import numpy as np
>>> from utils import printFormatted, py_test_csv, load_signal_from_wfdb
>>> matrix = np.array([[1.234567, 123.456789], [0.0001234, 1.2345]])
>>> printFormatted(matrix, decimals=4)
    1.2346  123.4568
    0.0001    1.2345
>>> signal, record_name = load_signal_from_wfdb('100', duration_minutes=1)
>>> print(f"ECG signal for record {record_name}: {signal[:10]}")  # Display first 10 samples
CompSensePack.utils.load_signal_from_wfdb(record_name, duration_minutes=None)[source]

Load an ECG signal from the MIT-BIH Arrhythmia Database using the wfdb package.

Parameters

record_namestr

The record name from the MIT-BIH Arrhythmia Database (e.g., ‘100’ for record 100).

duration_minutesint, optional

The duration of the signal to load in minutes. If None, loads the entire signal.

Returns

signalnp.ndarray

The ECG signal as a numpy array (units in microvolts).

record_namestr

The name of the record loaded.

Example

>>> signal, record_name = load_signal_from_wfdb('100', duration_minutes=1)
>>> print(f"ECG signal for record {record_name}: {signal[:10]}")  # Display first 10 samples

Notes

  • The function uses the PhysioNet’s MIT-BIH Arrhythmia Database to load ECG signals.

  • It multiplies the signal values by 1000 to convert from millivolts to microvolts.

CompSensePack.utils.printFormatted(matrix, decimals=4)[source]

Prints the matrix with formatted elements aligned in columns for improved readability.

Parameters

matrixnp.ndarray

The matrix to be printed. Should be a 2D numpy array.

decimalsint, optional

The number of decimal places to display for each element (default is 4).

Returns

None

This function does not return any value; it prints the formatted matrix directly to the console.

Example

>>> import numpy as np
>>> matrix = np.array([[1.234567, 123.456789], [0.0001234, 1.2345]])
>>> printFormatted(matrix, decimals=4)
     1.2346  123.4568
     0.0001    1.2345

Notes

  • This function is useful for visual inspection of numerical matrices, especially when the values have varying magnitudes.

CompSensePack.utils.py_test_csv(array, output_dir)[source]

Save a numpy array as a CSV file in the specified output directory.

Parameters

arraynp.ndarray

The input array to be saved as a CSV file.

output_dirstr or Path

The path to the directory where the CSV file should be saved. If the directory does not exist, it will be created.

Returns

None

This function saves the array to a CSV file but does not return any value.

Example

>>> import numpy as np
>>> array = np.random.rand(5, 3)
>>> py_test_csv(array, './Outputs/py_test_csv')

Notes

  • The output file will be saved in the specified directory as ‘py_test.csv’.

  • The directory will be created if it doesn’t exist.