Skip to main content

Utility functions that are often useful

Project description

pelutils

pytest Coverage Status

Various utilities useful for Python projects. Features include

  • A simple and powerful logger with colourful printing and stacktrace logging
  • Parsing for combining config files and command-line arguments - especially useful for algorithms with several parameters
  • A timer inspired by Matlab's tic and toc
  • Simple code profiler
  • An extension to the built-in dataclass for saving and loading data
  • Table formatting
  • Miscellaneous standalone functions - see pelutils/__init__.py
  • Data-science submodule with extra utilities for statistics, plotting with matplotlib, and machine learning using PyTorch
  • Linear time unique function in the style of numpy.unique

pelutils supports Python 3.9+.

To install, simply run pip install pelutils. A small subset of the functionality requires PyTorch, which has to be installed separately.

Timing and Code Profiling

Simple time taker inspired by Matlab Tic, Toc, which also has profiling tooling.

# Time a task
TT.tick()
<some task>
seconds_used = TT.tock()

# Profile a for loop
for i in range(100):
    TT.profile("Repeated code")
    <some task>
    TT.profile("Subtask")
    <some subtask>
    TT.end_profile()
    TT.end_profile()
print(TT)  # Prints a table view of profiled code sections

# Alternative syntax using with statement
with TT.profile("The best task"):
    <some task>

# When using multiprocessing, it can be useful to simulate multiple hits of the same profile
with mp.Pool() as p, TT.profile("Processing 100 items on multiple threads", hits=100):
    p.map(100 items)
# Similar for very quick loops
a = 0
with TT.profile("Adding 1 to a", hits=100):
    for _ in range(100):
        a += 1

# Examples so far use a global TickTock instance, which is convenient,
# but it can also be desirable to use for multiple different timers, e.g.
tt1 = TickTock()
tt2 = TickTock()
t1_interval = 1  # Do task 1 every second
t2_interval = 2  # Do task 2 every other second
tt1.tick()
tt2.tick()
while True:
    if tt1.tock() > t1_interval:
        <task 1>
        tt1.tick()
    if tt2.tock() > t2_interval:
        <task 2>
        tt2.tick()
    time.sleep(0.01)

Data Storage

The DataStorage class is an augmentation of the dataclass that incluces save and load functionality. This simplifies saving data, as only save command has to be issued for all data, and it keeps type hinting when loading data compared to e.g. a dictionary.

Data is in general preserved exactly as-is when saved data is loaded into memory with few exceptions. Notably, tuples are considered json-serializble, and so will be saved to the json file and will be loaded as lists.

Usage example:

@dataclass
class ResultData(DataStorage):
    shots: int
    goalscorers: list
    dists: np.ndarray

rdata = ResultData(shots=1, goalscorers=["Max Fenger"], dists=np.ones(22)*10)
rdata.save("max")
# Now shots and goalscorers are saved in <pwd>/max/ResultData.json and dists in <pwd>/max/ResultData.pkl

# Then to load
rdata = ResultData.load("max")
print(rdata.goalscorers)  # ["Max Fenger"]

Parsing

A parsing tool for combining command-line and config file arguments. Useful for parametric methods such as machine learning. The first argument must always be a path. This can for instance be used to put log files, results, plots etc.

Consider the execution of a file main.py with the command line call

python main.py path/to/put/results -c path/to/config/file.ini --data-path path/to/data

The config file could contain

[DEFAULT]
fp16
learning-rate=1e-4

[LOWLR]
learning-rate=1e-5

[NOFP16]
fp16=False

where main.py contains

options = [
    # Mandatory argument with set abbreviation -p
    Argument("data-path", help="Path to where data is located", abbrv"-p"),
    # Optional argument with auto-generated abbreviation -l
    Option("learning-rate", default=1e-5, help="Learning rate to use for gradient descent steps"),
    # Boolean flag with auto-generated abbreviation -f
    Flag("fp16", help="Use mixed precision for training"),
]
parser = Parser(*options, multiple_jobs=True)  # Two jobs are specified in the config file, so multiple_jobs=True
location = parser.location  # Experiments are stored here. In this case path/to/put/results
job_descriptions = parser.parse_args()
# Run each experiment
for job in job_descriptions:
    # Get the job as a dictionary
    job_dict = job.todict()
    # Clear directory where job is located and put a documentation file there
    job.prepare_directory()
    # Get location of this job as job.location
    run_experiment(job)

This could then by run by python main.py data/my-big-experiment --learning-rate 1e-5 or by python main.py data/my-big-experiment --config cfg.ini or using a combination where CLI args takes precedence: python main.py data/my-big-experiment --config cfg.ini --learning-rate 1e-5 where cfg.ini could contain

Logging

The logging submodule contains a simple yet feature-rich logger which fits common needs. Can be imported from pelutils directly, e.g. from pelutils import log.

# Configure logger for the script
log.configure("path/to/save/log.log")

# Start logging
for i in range(70):  # Nice
    log("Execution %i" % i)

# Sections
log.section("New section in the logfile")

# Adjust logging levels
log.warning("Will be logged")
with log.level(LogLevels.ERROR):  # Only log at ERROR level or above
    log.warning("Will not be logged")
with log.no_log:
    log.section("I will not be logged")

# Error handling
# The zero-division error and stacktrace is logged
with log.log_errors:
    0 / 0
# Entire chained stacktrace is logged
with log.log_errors:
    try:
        0 / 0
    except ZeroDivisionError as e:
        raise ValueError("Denominator must be non-zero") from e

# User input - acts like built-in input but logs both prompt and user input
inp = log.input("Continue [Y/n]? ")
# Parse yes/no user input
cont = log.parse_bool_input(inp, default=True)

# Log all logs from a function at the same time
# This is especially useful when using multiple threads so logging does not get mixed up
def fun():
    with log.collect:
        log("Hello there")
        log("General Kenobi!")
with mp.Pool() as p:
    p.map(fun, args)

# It is also possible to create multiple loggers by importing the Logger class, e.g.
log2 = Logger()
log2.configure("path/to/save/log2.log")

Data Science

This submodule contains various utility functions for data science, statistics, plotting, and machine learning.

Deep Learning

Statistics

Includes various commonly used statistical functions.

# Get one sided z value for exponential(lambda=2) distribution with a significance level of 1 %
zval = z(alpha=0.01, two_sided=False, distribution=scipy.stats.expon(loc=1/2))

# Get correlation, confidence interval, and p value for two vectors
a, b = np.random.randn(100), np.random.randn(100)
r, lower_r, upper_r, p = corr_ci(a, b, alpha=0.01)

Plotting

pelutils provides plotting utilities based on matplotlib. Most notable is the Figure context class, which attempts to remedy some of the common grievances with matplotlib, e.g. having to remember the correct kwargs and rcParams for setting font sizes, legend edge colour etc.

from pelutils.ds.plots import Figure

# The following makes a plot and saves it to `plot.png`.
# The seaborn is style is used for demonstration, but if the `style` argument
# is not given, the default matplotlib style is used.
# The figure and font size are also given for demonstration, but their default
# values are increased compared to matplotlib's default, as these are generally
# too small for finished plots.
with Figure("plot.png", figsize=(20, 10), style="seaborn", fontsize=20):
    plt.scatter(x, y, label="Data")
    plt.grid()
    plt.title("Very nice plot")
# The figure is automatically saved to `plot.png` and closed, such that
# plt.plot can be used again from here.
# Figure changes `matplotlib.rcParams`, but these changes are also undone
# after the end of the `with statement`.

The plotting utilies also include binning functions for creating nice histograms. The histogram function produces bins based on a binning function, of which three are provided:

  • linear_binning: Bins are spaced evenly from the lowest to the largest value of the data.
  • log_binning: Bins are log-spaced from the lowest to the largest value of the data, which is assumed to be positive.
  • normal_binning: Bins are distributed according to the distribution of the data, such there are more bins closer to the center of the data. This is useful if the data somewhat resembles a normal distribution, as the resolution will be the greatest where there is the most data.

It is also possible to provide custom binning functions.

histogram provide both x and y coordinates, making it simple to use with argument unpacking:

import matplotlib.pyplot as plt
import numpy as np
from pelutils.ds.plots import histogram, normal_binning

# Generate normally distributed data
x = np.random.randn(100)
# Plot distribution
plt.plot(*histogram(x, binning_fn=normal_binning))

Finally, different smoothing functions are provided. The two most common are moving_avg and exponential_avg which smooth the data using a moving average and exponential smoothing, respectively.

The double_moving_avg is special in that the number of smoothed data points do not depend on the number of given data points but is instead based on a given number of samples, which allows the resulting smoothed curve to not by jagged as happens with the other smoothing functions. It also has two smoothness parameters, which allows a large degree of smoothness control.

Apart from smoothness parameters, all smoothness functions have the same call signature:

from pelutils.ds.plots import double_moving_avg

# Generate noisy data
n = 100
x = np.linspace(-1, 1, n)
y = np.random.randn(n)

# Plot data along with smoothed curve
plt.plot(*double_moving_avg(x, y))
# If x is not given, it is assumed to go from 0 to n-1 in steps of 1
plt.plot(*double_moving_avg(y))

Examples of all the plotting utilities are shown in the examples directory.

Supported platforms

Precompiled wheels are provided for most common platforms. Notably, they are not provided for 32-bit systems. If no wheel is provided, pip should attempt a source install. If all else fails, it is possible to install from source by pointing pip to Github directly:

pip install git+https://github.com/peleiden/pelutils.git@release#egg=pelutils

It is also possible to install from source using pip's --no-binary option.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pelutils-3.1.0.tar.gz (48.4 kB view details)

Uploaded Source

Built Distributions

pelutils-3.1.0-cp312-cp312-win_amd64.whl (49.0 kB view details)

Uploaded CPython 3.12 Windows x86-64

pelutils-3.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (66.0 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

pelutils-3.1.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (66.8 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

pelutils-3.1.0-cp312-cp312-macosx_10_9_x86_64.whl (47.4 kB view details)

Uploaded CPython 3.12 macOS 10.9+ x86-64

pelutils-3.1.0-cp311-cp311-win_amd64.whl (49.0 kB view details)

Uploaded CPython 3.11 Windows x86-64

pelutils-3.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (65.9 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

pelutils-3.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (66.6 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

pelutils-3.1.0-cp311-cp311-macosx_10_9_x86_64.whl (47.4 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

pelutils-3.1.0-cp310-cp310-win_amd64.whl (49.0 kB view details)

Uploaded CPython 3.10 Windows x86-64

pelutils-3.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (65.9 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pelutils-3.1.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (66.6 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

pelutils-3.1.0-cp310-cp310-macosx_10_9_x86_64.whl (47.4 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

pelutils-3.1.0-cp39-cp39-win_amd64.whl (49.0 kB view details)

Uploaded CPython 3.9 Windows x86-64

pelutils-3.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (65.7 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pelutils-3.1.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (66.4 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

pelutils-3.1.0-cp39-cp39-macosx_10_9_x86_64.whl (47.4 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

File details

Details for the file pelutils-3.1.0.tar.gz.

File metadata

  • Download URL: pelutils-3.1.0.tar.gz
  • Upload date:
  • Size: 48.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for pelutils-3.1.0.tar.gz
Algorithm Hash digest
SHA256 1c9c72858b8b5fc0a4ea4191bf6d637c4e8341259d709ac0253dd3d9d393fcef
MD5 b8241df4755317b32d3427d3eb8b1195
BLAKE2b-256 4186183c996a1be13bf37a5807ae5d8fa9a193abc393f40ed4cc6e3fb188446e

See more details on using hashes here.

File details

Details for the file pelutils-3.1.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: pelutils-3.1.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 49.0 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for pelutils-3.1.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8b0cf2ff5d877f06cfc5a9d46cf494b8642660c54891d0dd8f50d3fca112879c
MD5 1640e8fd7d04efb7c48c8cb7a1ec7f88
BLAKE2b-256 acbeddc975908e0557d149da79cd4e86516d5d67d6511ea719da490a56779d97

See more details on using hashes here.

File details

Details for the file pelutils-3.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pelutils-3.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9f09401411b7581c31bda415a7c85202b2c7ca387a8c268ee3877b4b21eff5ba
MD5 7ad05d505e7b85eb6cd0fdd58bf1385b
BLAKE2b-256 59d3b98795d3d81fea67e7332657ac82c497a89145549522f758495dc08722cd

See more details on using hashes here.

File details

Details for the file pelutils-3.1.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pelutils-3.1.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 1b78ff398ee6eb90b6725402ac09816d09f1e37947589d7f18c82d46c4532e27
MD5 eb26fe735c4393fa44e17d0e8cb6750e
BLAKE2b-256 60c8552f1f0b92072717661774f6d3202fc9362bc91e31adf33af46fbd027662

See more details on using hashes here.

File details

Details for the file pelutils-3.1.0-cp312-cp312-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pelutils-3.1.0-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 af3a2d0335f3a1b95834fd73861a9432f68362967c01f4e2065690b186d6ce2a
MD5 1679555300c3634cfeb2c4b0d1839aec
BLAKE2b-256 c93d007750f1c07fa811514d297a52aa7e0a18afe366a6b6319be5a7faef7b11

See more details on using hashes here.

File details

Details for the file pelutils-3.1.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: pelutils-3.1.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 49.0 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for pelutils-3.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 31c90bedd607fa589a2fd7e16eff78d1593443bf2a312184a24a891e4d8cf1f5
MD5 e61ae7babbb2b9308b30f8b069576ba7
BLAKE2b-256 dec8858ee23241843643412e4585b1754523410c97d8f47f3f82773baf8c892a

See more details on using hashes here.

File details

Details for the file pelutils-3.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pelutils-3.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 32e9fa69b3427a9d9963c37e54ab96d82a946053fb672cea725e8975e58045f4
MD5 356842a818c7de8db18fd344e2ef43fa
BLAKE2b-256 a9b91bfb50189abd79f142511d146f5937ee0b3fe558e945dcc8f19102ee73e7

See more details on using hashes here.

File details

Details for the file pelutils-3.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pelutils-3.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c48a49828248361d85835ef5ff46ce5d4f6014d8e5e373e25adbcdaf139989d8
MD5 a091fc04194b1f9d2fcf65b1b1068d34
BLAKE2b-256 e3f4af1959ece956039a604080b142499002ac865eb490cd1a929423181c5ea9

See more details on using hashes here.

File details

Details for the file pelutils-3.1.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pelutils-3.1.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6b089d48120347e5c965235c55fe427ecb5b8eeac648494279ed17f3e3aa037d
MD5 643cb6953163982f33ba8ce51602ed68
BLAKE2b-256 fc00d76a85f7842c160e85e6b21b7bd7044898b72443452370898672cab27356

See more details on using hashes here.

File details

Details for the file pelutils-3.1.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: pelutils-3.1.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 49.0 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for pelutils-3.1.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 9432358535ce3e287fe3e6fed261ed71f82cf2621cbe775f7b0caa01ce841f51
MD5 c9a214e318a1f00dcc7a9c964ed80a74
BLAKE2b-256 a256fdfb3be8bf21753869e32feb59b83440c84b9f16078c7696b10ce47fa616

See more details on using hashes here.

File details

Details for the file pelutils-3.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pelutils-3.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1b98326444adb23de36930a8c7b947bc1e9d2baa3c4c4ab9da70d39bc851b033
MD5 3d36bc7cff64744f932ddaa675ef97b7
BLAKE2b-256 e8d6fee8aa71a00c8e61a7e86d4acf9670216c9b419f5c9f8b383f222118a581

See more details on using hashes here.

File details

Details for the file pelutils-3.1.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pelutils-3.1.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 cd4b4a29c724b1a9c0a718df318dddef9ce20234cd359e8b81182230ecc53859
MD5 811e65de2eb677f59c155a3e1c9d7e88
BLAKE2b-256 ecaf3331688e43288d581c8a2db2c678e1296ed48b07b57a104219ce0ec40196

See more details on using hashes here.

File details

Details for the file pelutils-3.1.0-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pelutils-3.1.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8547d07fd582873e40f55c2b486cba9ed9e31044791582a4d8cc7b38dc5c5c73
MD5 7fc2dcbfaa405c86b5c5997100404162
BLAKE2b-256 31772f4f75aa615be1e25ea441981169561de52a3aaf7bcc61e25bf859067f00

See more details on using hashes here.

File details

Details for the file pelutils-3.1.0-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: pelutils-3.1.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 49.0 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for pelutils-3.1.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 8021d2222e8a6e23cc7ea1a299c4e1e8dcfd3b724846828e20955ca6b6791637
MD5 a1f56f588206d0e51d547a8f1d9102dd
BLAKE2b-256 e6d497131a30785c6e879671e6aac97bf25a3cbdc502ef89316c2cea29a7720b

See more details on using hashes here.

File details

Details for the file pelutils-3.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pelutils-3.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 339103fdf579e0976ce2eb50242d136f0273baf365a63a980124a8eff9085f0e
MD5 231e73c45b6d9ff4aaf2bffe0830adf6
BLAKE2b-256 86226885a4a5cce4d2563a2765ba64a4adcfa2fbeed7fd81ca0f9861d92e01c4

See more details on using hashes here.

File details

Details for the file pelutils-3.1.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pelutils-3.1.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c1123e63d19ae45dc9e8b854e19904c154e9230a0fc9ffd0b4b50fe64b190a7f
MD5 57c4bb6e133bef9a1d8ac69be5ecede5
BLAKE2b-256 053321b8fbc5802dcc548c027230628648272c5658dcf2cd1c3638960c01dfdc

See more details on using hashes here.

File details

Details for the file pelutils-3.1.0-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pelutils-3.1.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3bd15c7aa16e32a6f522558423d33e79f7193ca23350ba6f1c52def89129854e
MD5 7913e8ae7dfdfc56f66216e96b81d568
BLAKE2b-256 f6594d56a5c90456acb0ffd09521831d5f6899b43ca69e9e70b65308faf8e1c0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page