liberata_metrics.utils package#

Overview#

The utils module provides essential utilities for working with the Liberata Scientometrics library. It includes:

  • Data Loading: Fetch data from Supabase or local files

  • Data Wrangling: Transform and prepare data for analysis

  • Matrix Operations: Efficient utilities for sparse matrix manipulation

  • Helper Functions: Common operations used across the package

Submodules#

Performance Considerations#

Working with Large Matrices

The utils module is optimized for large sparse matrices:

  • Sparse matrix format (COO, CSR, CSC) to minimize memory usage

  • Lazy operations that avoid creating dense copies

  • Vectorized NumPy operations for speed

Memory Profiling

from liberata_metrics.utils import utils
import sys

# Check size of matrix in memory
size_mb = sys.getsizeof(matrix) / 1e6
nnz = matrix.nnz  # Number of non-zero elements
density = nnz / (matrix.shape[0] * matrix.shape[1])

Module Contents#

liberata_metrics.utils.coo_to_binned_array(coo: coo_matrix, max_side: int, agg_fn: Callable = <function sum>) → ndarray[source]#

utility function to convert COO to binned array

liberata_metrics.utils.deserialize_upload_dates(upload_dates_json: Dict[str, str]) → Dict[str, date][source]#

convert ISO strings YYYY-MM-DD to date objects

liberata_metrics.utils.make_date_grid(start_date: date, end_date: date, time_step: timedelta) → List[date][source]#

build a grid of dates from start_date to end_date with given time_step increments for time series data extraction

liberata_metrics.utils.matrix_to_plot_array(mat: spmatrix, max_side: int = 500, agg_fn: Callable = <function sum>) → ndarray[source]#

convert any COO matrix into appropriate array form for plotting (note: this is all ChatGPT)

liberata_metrics.utils.random_date(rng: RandomState, start: date, end: date) → date[source]#

draw a random date in given range

liberata_metrics.utils.read_yaml_config(config_path: str)[source]#

reads YAML config file and returns dictionary

liberata_metrics.utils.save_sparse_npz(path: str | Path, matrix: spmatrix, log: Logger | None = None) → None[source]#

save scipy.sparse matrix to .npz

liberata_metrics.utils.serialize_upload_dates(upload_dates: Dict[str, date]) → Dict[str, str][source]#

convert date objects to ISO strings YYYY-MM-DD