Skip to main content

Collection of Python utilities intended to be useful for machine learning research and experiments.

Project description

jutility

Collection of Python utilities intended to be useful for machine learning research and experiments.

Contents

Install with pip

The jutility package is available as a Python package on PyPI, and can be installed with pip using the following commands:

python -m pip install -U pip
python -m pip install -U jutility

Alternatively, jutility can be installed in "editable mode" from the GitHub repository:

git clone https://github.com/jakelevi1996/jutility.git
cd jutility
python -m pip install -U pip
python -m pip install -e .

Usage examples

plotting

Making a simple plot

import numpy as np
from jutility import plotting

x = np.linspace(-10, 10, 100, dtype=np.float32)
plotting.plot(
    plotting.Line(x, np.log(1 + np.exp(x)), label="Softplus", c="b"),
    plotting.Line(x, np.maximum(x, 0),      label="ReLU",     c="r", ls="--"),
    legend=True,
    plot_name="Softplus vs ReLU",
    dir_name="images",
)

import numpy as np
from jutility import plotting

rng = np.random.default_rng(0)
x = np.linspace(0, 2)
f = lambda x: x + 0.1 * rng.normal(size=x.shape)

plotting.plot(
    plotting.Line(x, f(x), c="b", marker="o", label="Blue data"),
    plotting.Line(x, np.exp(-f(x)), c="r", marker="o", label="Red data"),
    axis_properties=plotting.AxisProperties("x label", "y label"),
    legend=True,
    plot_name="Simple plot",
    dir_name="images",
)

Multiple lines in a single Line

import numpy as np
from jutility import plotting, util

n = 100
batch_size = 20
rng = np.random.default_rng(0)
noise = rng.normal(size=[n, batch_size])

x = np.linspace(-1, 4, n).reshape(n, 1)
y = np.exp(-x) + 0.1 * noise

plotting.plot(
    plotting.Line(x, y, alpha=0.2,       c="b", zorder=10),
    plotting.Line(x, np.mean(y, axis=1), c="r", zorder=20),
    plot_name="Multiple lines in a single Line",
    dir_name="images",
)

Vector fields (with optional normalisation)

import numpy as np
from jutility import plotting, util

n = 25
x = np.linspace(-2, 2, n).reshape(1, n)
y = np.linspace(-2, 2, n).reshape(n, 1)
dx = y - x
dy = x + y
mp = plotting.MultiPlot(
    plotting.Subplot(
        plotting.Quiver(x, y, dx, dy, zorder=10, normalise=False),
        axis_properties=plotting.AxisProperties(title="normalise=False")
    ),
    plotting.Subplot(
        plotting.Quiver(x, y, dx, dy, zorder=10, normalise=True),
        axis_properties=plotting.AxisProperties(title="normalise=True")
    ),
    figure_properties=plotting.FigureProperties(figsize=[10, 4])
)
mp.save(plot_name="Vector field", dir_name="images")

Shared colour bar

import numpy as np
from jutility import plotting

rng = np.random.default_rng(0)
z1 = rng.random((100, 200)) + 5
z2 = rng.random((100, 200)) + 2
v_min = min(z1.min(), z2.min())
v_max = max(z1.max(), z2.max())

colour_bar = plotting.ColourBar(v_min, v_max)

mp = plotting.MultiPlot(
    plotting.ImShow(c=z1, vmin=v_min, vmax=v_max),
    colour_bar,
    plotting.ImShow(c=z2, vmin=v_min, vmax=v_max),
    colour_bar,
    figure_properties=plotting.FigureProperties(
        num_rows=2,
        num_cols=2,
        width_ratios=[1, 0.2],
        tight_layout=False,
        title="Shared colour bar",
    ),
)
mp.save("Shared colour bar", dir_name="images")

More complex examples coming soon

util

Coming soon

sweep

Coming soon

(in the meantime, see scripts/make_logo.py which made the logo above, and unit tests for util, plotting, and sweep)

Unit tests

To run all unit tests, install pytest (these tests have previously been run with pytest version 5.4.1), and run the following command (at the time of writing, this takes about 17 seconds to run 42 unit tests, because several unit tests involve saving images or GIFs to disk):

pytest

Build package locally

jutility can be installed in "editable mode" using pip using the following command:

python -m pip install -e .

Alternatively, jutility can be built and installed locally using the following commands, replacing $WHEEL_NAME with the name of the wheel built by the python -m build command (for example, jutility-0.0.5-py3-none-any.whl):

python -m build
python -m pip install --force-reinstall --no-deps dist/$WHEEL_NAME

Or alternatively, run the script build_local.py, which automatically extracts the version number to infer the name of the wheel and then builds it:

python build_local.py

Updating package on PyPI

This package was uploaded to PyPI following the Packaging Python Projects tutorial in the official Python documentation.

To update PyPI with a newer version, update the version tag in setup.cfg, and then use the following commands:

rm -rf dist/*
python -m build
python -m twine upload dist/*

When prompted by twine, enter __token__ as the username, and paste an API token from the PyPI account management webpage as the password (including the pypi- prefix).

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

jutility-0.0.22.tar.gz (50.8 kB view details)

Uploaded Source

Built Distribution

jutility-0.0.22-py3-none-any.whl (29.9 kB view details)

Uploaded Python 3

File details

Details for the file jutility-0.0.22.tar.gz.

File metadata

  • Download URL: jutility-0.0.22.tar.gz
  • Upload date:
  • Size: 50.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for jutility-0.0.22.tar.gz
Algorithm Hash digest
SHA256 de3019c24284c3d094697d70f73724aada0ded4ab4aa8a0ac164a4a776ae1e94
MD5 b5e8e77f0e33c7c59ce34cf06cce1180
BLAKE2b-256 62dcb50e66ca5a75084620fe0e4e63f85ba287de67bd012361350a1b637c4995

See more details on using hashes here.

File details

Details for the file jutility-0.0.22-py3-none-any.whl.

File metadata

  • Download URL: jutility-0.0.22-py3-none-any.whl
  • Upload date:
  • Size: 29.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for jutility-0.0.22-py3-none-any.whl
Algorithm Hash digest
SHA256 26f05e5b630fa6be62cd994febe033bed4b73ce5054485223add4b365ae4736f
MD5 71abe9ee038ae5b70fa39fdf0937f424
BLAKE2b-256 825c87806367d5139b0892c835c19252694bfa9abfef4e98df298bfe13545696

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