Skip to content

Commit

Permalink
Move the pickle download out of setup.py into decomposer_base.py
Browse files Browse the repository at this point in the history
  • Loading branch information
PAHdb committed Feb 15, 2024
1 parent 3178a71 commit c62d96e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 37 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.9
- name: Install dependencies
Expand All @@ -26,7 +26,7 @@ jobs:
coverage report -m
coverage xml
- name: Upload coverage report to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: coverage.xml
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pyPAHdb can be directly installed from the

`pip install git+https://github.com/PAHdb/pyPAHdb.git`

Note that upon first run a pre-computed matrix will be automatically downloaded.

## Supported data formats

pyPAHdb supports reading IPAC tables, _Spitzer_ FITS files and _JWST_ FITS files.
Expand Down
46 changes: 35 additions & 11 deletions pypahdb/decomposer_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import os
import pickle
from functools import partial
from urllib.request import urlretrieve

import importlib_resources
import numpy as np
import pkg_resources
from tqdm import tqdm
from astropy import units as u
from scipy import optimize
from specutils import Spectrum1D
Expand Down Expand Up @@ -115,16 +117,38 @@ def __init__(self, spectrum):
print("spectral data is all zeros.")
return None

# Retrieve the precomputed data and raise error if file is
# not found.
file_name = "resources/precomputed.pkl"
file_path = pkg_resources.resource_filename("pypahdb", file_name)
with open(file_path, "rb") as f:
try:
self._precomputed = pickle.load(f, encoding="latin1")
except Exception as e:
print("Python 3 is required for pypahdb.")
raise (e)
# Download the precomputed data if not present
remote_pkl = "https://www.astrochemistry.org/pahdb/pypahdb/pickle.php"
if os.getenv("GITHUB_ACTIONS") == "true":
remote_pkl += "?github_actions=true"
local_pkl = (
importlib_resources.files("pypahdb") / "resources/precomputed.pkl"
)
if not os.path.isfile(local_pkl):

def hook(t):
last_b = [0]

def inner(b=1, bsize=1, tsize=None):
if tsize is not None:
t.total = tsize
t.update((b - last_b[0]) * bsize)
last_b[0] = b
return inner

print("downloading pre-computed matrix")
with tqdm(
unit="B",
unit_scale=True,
leave=True,
miniters=1,
) as t:
urlretrieve(
remote_pkl, filename=local_pkl, reporthook=hook(t), data=None
)

with open(local_pkl, "rb") as f:
self._precomputed = pickle.load(f, encoding="latin1")

# Linearly interpolate the precomputed spectra onto the
# frequency grid of the input spectrum.
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ scipy
astropy
specutils
matplotlib
importlib_resources
tqdm
24 changes: 1 addition & 23 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
from os import path
from os import getenv
from setuptools import setup, find_packages
from setuptools.command.build_py import build_py
import versioneer

try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
from os import path

"""
NOTE: This file must remain Python 2 compatible for the foreseeable future,
Expand Down Expand Up @@ -39,21 +33,6 @@
requirements = [line for line in requirements_file.read().splitlines()
if not line.startswith('#')]


class BuildPyCommand(build_py):
def run(self):
remote_pkl = 'https://www.astrochemistry.org/pahdb/pypahdb/pickle.php'
if getenv('GITHUB_ACTIONS') == 'true':
remote_pkl += '?github_actions=true'
local_pkl = 'pypahdb/resources/precomputed.pkl'
# honor the --dry-run flag
if not self.dry_run and not path.isfile(local_pkl):
response = urlopen(remote_pkl)
with open(path.join(here, local_pkl), 'wb') as f:
f.write(response.read())
versioneer.get_cmdclass()['build_py'].run(self)
build_py.run(self)

# Arguments marked as "Required" below must be included for upload to PyPI.
# Fields marked as "Optional" may be commented out.

Expand Down Expand Up @@ -222,6 +201,5 @@ def run(self):

# Run custom commands
cmdclass={ # Optional
'build_py': BuildPyCommand,
},
)

0 comments on commit c62d96e

Please sign in to comment.