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 hashes)

Uploaded Source

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