Skip to main content

A packaging tool for storing and exchanging data & code bound in a single file.

Project description

PltPack

Bind your matplotlib functions with data

Test

PyPI version fury.io PyPI pyversions PyPI license PyPI status

PltPack is a lightweight packaging tool for storing and exchanging data & code bound in a single file. It is mainly created to accelerate scientific work and is focused on supporting matplotlib package for exchanging scientific figures. Integrated with Jupyter Notebook.

Install from PyPi with pip:

pip install plt_pack

Usage

PltPack saves minimal atomic part of your code and the data required to run a function and plot a figure:

  • arguments
  • imports
  • function code
  • called sub-functions
  • global variables
  • used module versions
  • non-default matplotlib rcParams

The most convenient way to use PltPack is to define a project with a folder where all the .plt files will be saved, and register the entry functions you would like to save. The alternative is to decorate a function with 'auto_save' method to save it with new arguments on every call.

Below is a self-explanatory example:

# imports
from matplotlib import pyplot as plt
import numpy as np

from plt_pack import PltProject

# define your project with directory for figures
plt_project = PltProject('FiguresDir')

# define your functions
# decorate them with 'register' or 'auto_save' method

@plt_project.register
def plot(x, y_list, label_list):
    for y, label in zip(y_list, label_list):
        plot_line(x, y, label)
    add_legend()


@plt_project.auto_save(rewrite=False, datefmt='%H-%M-%S')
def plot_hist(y, bins: int = 10):
    plt.hist(y, bins=bins)


# some util functions & globals defined somewhere in your
# file or Jupyter Notebook

COLOR = 'red'

def plot_line(x, y, label: str = None, ls: str = '--'):
    plt.plot(x, y, ls=ls, lw=2, c=COLOR, label=label)

def add_legend():
    plt.legend()
    
    
# Registered function can be saved with context parameters:
x = np.arange(10)
y_list = [np.arange(10) * i for i in range(5)]
label_list = [f'Curve #{i}' for i in range(5)]

with plt_project(rewrite=True, name='my_function'):
    plot(x, y_list, label_list)
    
# function will be executed but also saved to my_function.plt
# file to your project folder:

assert plt_project.list_files() == ['my_function']

Now you can upload this file later (non necessarily on the same machine) and reproduce the result with one call.

from plt_pack import read_plt_file

file = read_plt_file('my_function')
# or using plt_project: 
# file = plt_project.load_file('path/to/file')

file.exec()  # that will re-run the function

print(file.get_code_str())  # that will show all the code to reproduce it

Expected output:

# Imports:
import matplotlib.pyplot as plt

# Global variables:
COLOR = 'red'

# Main function:

def plot(x, y_list, label_list):
    for y, label in zip(y_list, label_list):
        plot_line(x, y, label)
    add_legend()


# Sub-functions:

def add_legend():
    plt.legend()


def plot_line(x, y, label: str = None, ls: str = '--'):
    plt.plot(x, y, ls=ls, lw=2, c=COLOR, label=label)

LICENSE

MIT

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

plt_pack-0.0.3.2.tar.gz (16.6 kB view details)

Uploaded Source

File details

Details for the file plt_pack-0.0.3.2.tar.gz.

File metadata

  • Download URL: plt_pack-0.0.3.2.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.3

File hashes

Hashes for plt_pack-0.0.3.2.tar.gz
Algorithm Hash digest
SHA256 1bd8ee66c3bc6f8af98c38335fbb4b23ee963a5ef10ad4e153c11870fd732fef
MD5 bafe25912557787f55c433f48b087303
BLAKE2b-256 a12ec76d5cc28426619a5669d5de0c90f96363282b5066e2e3b08b80310875ce

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