Skip to main content

crunch-convert - Conversion module for the CrunchDAO Platform

Project description

Crunch Convert Tool

PyTest

This Python library is designed for the CrunchDAO Platform, exposing the conversion tools in a very small CLI.

Features

Automatic line commenting

Only includes the functions, imports, and classes will be kept.

Everything else is commented out to prevent side effects when your code is loaded into the cloud environment. (e.g. when you're exploring the data, debugging your algorithm, or doing visualizating using Matplotlib, etc.)

You can prevent this behavior by using special comments to tell the system to keep part of your code:

  • To start a section that you want to keep, write: @crunch/keep:on
  • To end the section, write: @crunch/keep:off
# @crunch/keep:on

# keep global initialization
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# keep constants
TRAIN_DEPTH = 42
IMPORTANT_FEATURES = [ "a", "b", "c" ]

# @crunch/keep:off

# this will be ignored
x, y = crunch.load_data()

def train(...):
    ...

The result will be:

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

TRAIN_DEPTH = 42
IMPORTANT_FEATURES = [ "a", "b", "c" ]

#x, y = crunch.load_data()

def train(...):
    ...

[!TIP] You can put a @crunch/keep:on at the top of the cell and never close it to keep everything.

Specifying package versions

Since submitting a notebook does not include a requirements.txt, users can instead specify the version of a package using import-level requirement specifiers in a comment on the same line.

# Valid statements
import pandas # == 1.3
import sklearn # >= 1.2, < 2.0
import tqdm # [foo, bar]
import scikit # ~= 1.4.2
from requests import Session # == 1.5

Specifying multiple times will cause the submission to be rejected if they are different.

# Inconsistant versions will be rejected
import pandas # == 1.3
import pandas # == 1.5

Specifying versions on standard libraries does nothing (but they will still be rejected if there is an inconsistent version).

# Will be ignored
import os # == 1.3
import sys # == 1.5

If an optional dependency is required for the code to work properly, an import statement must be added, even if the code does not use it directly.

import castle.algorithms

# Keep me, I am needed by castle
import torch

It is possible for multiple import names to resolve to different libraries on PyPI. If this happens, you must specify which one you want. If you do not want a specific version, you can use @latest, as without this, we cannot distinguish between commented code and version specifiers.

# Prefer https://pypi.org/project/EMD-signal/
import pyemd # EMD-signal @latest

# Prefer https://pypi.org/project/pyemd/
import pyemd # pyemd @latest

R imports via rpy2

For notebook users, the packages are automatically extracted from the importr("<name>") calls, which is provided by rpy2.

# Import the `importr` function
from rpy2.robjects.packages import importr

# Import the "base" R package
base = importr("base")

The following format must be followed:

  • The import must be declared at the root level.
  • The result must be assigned to a variable; the variable's name will not matter.
  • The function name must be importr, and it must be imported as shown in the example above.
  • The first argument must be a string constant, variables or other will be ignored.
  • The other arguments are ignored; this allows for custom import mapping if necessary.

The line will not be commented, read more about line commenting here.

Embedded Files

Additional files can be embedded in cells to be submitted with the Notebook. In order for the system to recognize a cell as an Embed File, the following syntax must be followed:

---
file: <file_name>.md
---

<!-- File content goes here -->
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Aenean rutrum condimentum ornare.

Submitting multiple cells with the same file name will be rejected.

While the focus is on Markdown files, any text file will be accepted. Including but not limited to: .txt, .yaml, .json, ...

Installation

Use pip to install the crunch-convert.

pip install --upgrade crunch-convert

Usage

Via the CLI

crunch-convert notebook my-notebook.ipynb

Via the Code

from crunch_convert.notebook import extract_from_file

flatten = extract_from_file("notebook.ipynb")

with open("main.py", "w") as fd:
  fd.write(flatten.source_code)

with open("requirements.txt", "w") as fd:
  for requirement in flatten.requirements:
    fd.write(str(requirement) + "\n")

for embedded_file in flatten.embedded_files:
  with open(embedded_file.normalized_path, "w") as fd:
    fd.write(embedded_file.content)

Contributing

Pull requests are always welcome! If you find any issues or have suggestions for improvements, please feel free to submit a pull request or open an issue in the GitHub repository.

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

crunch_convert-0.1.0.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

crunch_convert-0.1.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file crunch_convert-0.1.0.tar.gz.

File metadata

  • Download URL: crunch_convert-0.1.0.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for crunch_convert-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0fb58407e63af946199d790463ac91f5df1995d3545b051431687297d6249e4c
MD5 281bcda3f90c111bfa562171b1989989
BLAKE2b-256 e4a11ad4b8d0469b61431f4e351df03be2717dc260228c7cd11fed682df84dc1

See more details on using hashes here.

File details

Details for the file crunch_convert-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: crunch_convert-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for crunch_convert-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dfecddf27e0378041b73c07a9d3177f31c275e3dcaf6c176f73cb5037f7a028b
MD5 bea711a2511ce8447ba0e0b49190b1fa
BLAKE2b-256 1b9ff896c7eda3416d874841d6d3047b6a49529499a5daf40c0b977a4bdf050d

See more details on using hashes here.

Supported by

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