Skip to content

Commit

Permalink
pkg_resources to importlib update
Browse files Browse the repository at this point in the history
  • Loading branch information
karllark committed Nov 27, 2024
1 parent 0ead456 commit ef05ba6
Show file tree
Hide file tree
Showing 21 changed files with 78 additions and 112 deletions.
17 changes: 7 additions & 10 deletions measure_extinction/plotting/plot_ext.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
#!/usr/bin/env python

from __future__ import absolute_import, division, print_function, unicode_literals

import pkg_resources
import argparse
import warnings
import matplotlib.pyplot as plt
Expand All @@ -11,6 +6,7 @@
import pandas as pd
import os

from measure_extinction.utils.helpers import get_datapath
from measure_extinction.extdata import ExtData
from dust_extinction.parameter_averages import CCM89

Expand Down Expand Up @@ -92,8 +88,9 @@ def plot_average(
Plots the average extinction curve
"""
# read in the average extinction curve (if it exists)
if os.path.isfile(path + filename):
average = ExtData(path + filename)
fname = f"{path}/{filename}"
if os.path.isfile(fname):
average = ExtData(fname)
else:
warnings.warn(
"An average extinction curve with the name "
Expand Down Expand Up @@ -331,8 +328,8 @@ def plot_HI(ax, wavenum=False):
Indicates HI-lines on the plot
"""
# read in HI-lines
path = pkg_resources.resource_filename("measure_extinction", "data/")
table = pd.read_table(path + "HI_lines.list", sep=r"\s+", comment="#")
data_path = get_datapath()
table = pd.read_table(f"{data_path}/HI_lines.list", sep=r"\s+", comment="#")
# group lines by series
series_groups = table.groupby("n'")
colors = plt.get_cmap("tab10")
Expand Down Expand Up @@ -805,7 +802,7 @@ def main():
parser.add_argument(
"--path",
help="path to the data files",
default=pkg_resources.resource_filename("measure_extinction", "data/"),
default=get_datapath(),
)
parser.add_argument("--alax", help="plot A(lambda)/A(X)", action="store_true")
parser.add_argument(
Expand Down
10 changes: 3 additions & 7 deletions measure_extinction/plotting/plot_spec.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
#!/usr/bin/env python

from __future__ import absolute_import, division, print_function, unicode_literals

import pkg_resources
import argparse
import matplotlib.pyplot as plt
import numpy as np
import astropy.units as u
import pandas as pd

from measure_extinction.utils.helpers import get_datapath
from measure_extinction.stardata import StarData


Expand All @@ -26,7 +22,7 @@ def plot_HI(path, ax):
Indicates HI-lines on the plot
"""
# read in HI-lines
table = pd.read_table(path + "HI_lines.list", sep=r"\s+", comment="#")
table = pd.read_table(f"{path}/HI_lines.list", sep=r"\s+", comment="#")
# group lines by series
series_groups = table.groupby("n'")
colors = plt.get_cmap("tab10")
Expand Down Expand Up @@ -479,7 +475,7 @@ def main():
parser.add_argument(
"--path",
help="path to the data files",
default=pkg_resources.resource_filename("measure_extinction", "data/"),
default=get_datapath(),
)
parser.add_argument("--mlam4", help="plot lambda^4*F(lambda)", action="store_true")
parser.add_argument("--HI_lines", help="indicate the HI-lines", action="store_true")
Expand Down
4 changes: 2 additions & 2 deletions measure_extinction/stardata.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ def _getspecfilename(line, path):
eqpos = line.find("=")
tfile = line[eqpos + 2 :].rstrip()

return path + tfile
return f"{path}/{tfile}"


class SpecData:
Expand Down Expand Up @@ -1102,7 +1102,7 @@ def read(self, deredden=False, only_bands=None):
"""

# open and read all the lines in the file
f = open(self.path + self.file, "r")
f = open(f"{self.path}/{self.file}", "r")
self.datfile_lines = list(f)
f.close()
# get the photometric band data
Expand Down
14 changes: 7 additions & 7 deletions measure_extinction/tests/test_calc_ext.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import pkg_resources
import os

from measure_extinction.utils.helpers import get_datapath
from measure_extinction.utils.calc_ext import calc_extinction, calc_ave_ext


def test_calc_extinction():
# get the location of the data files
data_path = pkg_resources.resource_filename("measure_extinction", "data/")

redstarname = "HD229238"
compstarname = "HD204172"

# get the location of the data files
data_path = get_datapath()

# calculate the extinction curve
calc_extinction(redstarname, compstarname, data_path, savepath=data_path)

# check if an extinction curve has been calculated and saved to a fits file
assert os.path.isfile(
data_path + "%s_%s_ext.fits" % (redstarname.lower(), compstarname.lower())
f"{data_path}/{redstarname.lower()}_{compstarname.lower()}_ext.fits"
), (
"No FITS file has been created with the extinction curve of reddened star "
+ redstarname
Expand All @@ -27,7 +27,7 @@ def test_calc_extinction():

def test_calc_ave_ext():
# get the location of the data files
data_path = pkg_resources.resource_filename("measure_extinction", "data/")
data_path = get_datapath()

# list the same starpair 3 times so that an average curve will be calculated (it needs at least 3 sightlines)
starpair_list = ["HD229238_HD204172", "HD229238_HD204172", "HD229238_HD204172"]
Expand All @@ -38,5 +38,5 @@ def test_calc_ave_ext():

# check if the average extinction curve has been calculated and saved to a fits file
assert os.path.isfile(
data_path + "average_ext.fits"
f"{data_path}/average_ext.fits"
), "No FITS file has been created with the average extinction curve"
21 changes: 10 additions & 11 deletions measure_extinction/tests/test_extdata.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import pkg_resources

import astropy.units as u
import numpy as np

from measure_extinction.utils.helpers import get_datapath
from measure_extinction.stardata import StarData
from measure_extinction.extdata import ExtData, _hierarch_keywords, _get_column_val


def test_calc_ext():
# get the location of the data files
data_path = pkg_resources.resource_filename("measure_extinction", "data/")
data_path = get_datapath()

# read in the observed data of the stars
redstar = StarData("hd229238.dat", path=data_path)
Expand All @@ -34,7 +33,7 @@ def test_calc_ext():

def test_get_fitdata():

data_path = pkg_resources.resource_filename("measure_extinction", "data/")
data_path = get_datapath()

# read in the observed data of the stars
redstar = StarData("hd229238.dat", path=data_path)
Expand Down Expand Up @@ -62,7 +61,7 @@ def test_get_fitdata():

def test_calc_AV_RV():
# get the location of the data files
data_path = pkg_resources.resource_filename("measure_extinction", "data/")
data_path = get_datapath()

# read in the observed data of the stars
redstar = StarData("hd229238.dat", path=data_path)
Expand Down Expand Up @@ -100,17 +99,17 @@ def test_get_column_val():

def test_fit_band_ext(): # only for alax=False (for now)
# get the location of the data files
data_path = pkg_resources.resource_filename("measure_extinction", "data/")
data_path = get_datapath()

# read in the extinction curve data
extdata = ExtData(data_path + "hd229238_hd204172_ext.fits")
extdata = ExtData(f"{data_path}/hd229238_hd204172_ext.fits")

# fit the extinction curve with a powerlaw based on the band data
extdata.fit_band_ext()

# test the fitting results
waves, exts, res = np.loadtxt(
data_path + "fit_band_ext_result_hd229238_hd204172.txt", unpack=True
f"{data_path}/fit_band_ext_result_hd229238_hd204172.txt", unpack=True
)
np.testing.assert_almost_equal(extdata.model["waves"], waves)
np.testing.assert_almost_equal(extdata.model["exts"], exts)
Expand All @@ -124,17 +123,17 @@ def test_fit_band_ext(): # only for alax=False (for now)

def test_fit_spex_ext(): # only for alax=False (for now)
# get the location of the data files
data_path = pkg_resources.resource_filename("measure_extinction", "data/")
data_path = get_datapath()

# read in the extinction curve data
extdata = ExtData(data_path + "hd229238_hd204172_ext.fits")
extdata = ExtData(f"{data_path}/hd229238_hd204172_ext.fits")

# fit the extinction curve with a powerlaw based on the SpeX data
extdata.fit_spex_ext()

# test the fitting results
waves, exts, res = np.loadtxt(
data_path + "fit_spex_ext_result_hd229238_hd204172.txt", unpack=True
f"{data_path}/fit_spex_ext_result_hd229238_hd204172.txt", unpack=True
)
np.testing.assert_almost_equal(extdata.model["waves"], waves)
np.testing.assert_almost_equal(extdata.model["exts"], exts)
Expand Down
6 changes: 3 additions & 3 deletions measure_extinction/tests/test_plot_ext.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pkg_resources
import os
import warnings
import pytest

from measure_extinction.utils.helpers import get_datapath
from measure_extinction.plotting.plot_ext import (
plot_multi_extinction,
plot_extinction,
Expand All @@ -13,7 +13,7 @@
@pytest.mark.skip(reason="failing due to changes in matplotlib")
def test_plot_extinction():
# get the location of the data files
data_path = pkg_resources.resource_filename("measure_extinction", "data/")
data_path = get_datapath()
starpair = "HD229238_HD204172"

# plot the extinction curve
Expand Down Expand Up @@ -105,7 +105,7 @@ def test_plot_extinction():

def test_plot_average():
# get the location of the data files
data_path = pkg_resources.resource_filename("measure_extinction", "data/")
data_path = get_datapath()

# plot the average extinction curve in a separate figure
# with the default settings
Expand Down
4 changes: 2 additions & 2 deletions measure_extinction/tests/test_plot_spec.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pkg_resources
import os

from measure_extinction.utils.helpers import get_datapath
from measure_extinction.plotting.plot_spec import plot_multi_spectra, plot_spectrum


def test_plot_spectra():
# get the location of the data files
data_path = pkg_resources.resource_filename("measure_extinction", "data/")
data_path = get_datapath()
starlist = ["hd229238", "hd204172"]

# plot the spectra in separate figures
Expand Down
7 changes: 3 additions & 4 deletions measure_extinction/tests/test_stardata.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import pkg_resources

import astropy.units as u

from measure_extinction.utils.helpers import get_datapath
from measure_extinction.stardata import StarData


def test_load_stardata():
# get the location of the data files
data_path = pkg_resources.resource_filename("measure_extinction", "data/")
data_path = get_datapath()

# read in the observed data on the star
star = StarData("hd229238.dat", path=data_path)
Expand All @@ -18,7 +17,7 @@ def test_load_stardata():

def test_units_stardata():
# get the location of the data files
data_path = pkg_resources.resource_filename("measure_extinction", "data/")
data_path = get_datapath()

# read in the observed data of the star
star = StarData("hd229238.dat", path=data_path)
Expand Down
16 changes: 5 additions & 11 deletions measure_extinction/utils/calc_ext.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#!/usr/bin/env python

from __future__ import absolute_import, division, print_function, unicode_literals

import pkg_resources
import argparse

from measure_extinction.utils.helpers import get_datapath
from measure_extinction.stardata import StarData
from measure_extinction.extdata import ExtData, AverageExtData

Expand All @@ -31,9 +27,7 @@ def calc_extinction(
extdata.trans_elv_elvebv()
elif alav:
extdata.trans_elv_alav()
extdata.save(
savepath + "%s_%s_ext.fits" % (redstarname.lower(), compstarname.lower())
)
extdata.save(f"{savepath}/{redstarname.lower()}_{compstarname.lower()}_ext.fits")


def calc_ave_ext(
Expand Down Expand Up @@ -65,10 +59,10 @@ def calc_ave_ext(
"""
extdatas = []
for starpair in starpair_list:
extdata = ExtData("%s%s_ext.fits" % (path, starpair.lower()))
extdata = ExtData(f"{path}/{starpair.lower()}_ext.fits")
extdatas.append(extdata)
average = AverageExtData(extdatas, min_number=min_number, mask=mask)
average.save(path + outname)
average.save(f"{path}/{outname}")


def main():
Expand All @@ -79,7 +73,7 @@ def main():
parser.add_argument(
"--path",
help="path to data files",
default=pkg_resources.resource_filename("measure_extinction", "data/"),
default=get_datapath(),
)
parser.add_argument(
"--deredden",
Expand Down
14 changes: 13 additions & 1 deletion measure_extinction/utils/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import importlib.resources as importlib_resources

__all__ = ["get_full_starfile"]
__all__ = ["get_full_starfile", "get_datapath"]


def get_full_starfile(starname):
Expand All @@ -25,3 +26,14 @@ def get_full_starfile(starname):
def_path = "./"

return fstarname, def_path


def get_datapath():
"""
Determine the location of the data distributed along with the package
"""
# get the location of the data files
ref = importlib_resources.files("measure_extinction") / "data"
with importlib_resources.as_file(ref) as cdata_path:
data_path = str(cdata_path)
return data_path
2 changes: 0 additions & 2 deletions measure_extinction/utils/make_all_tlusty_obsdata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python

import glob
import argparse

Expand Down
9 changes: 2 additions & 7 deletions measure_extinction/utils/make_obsdata_from_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#!/usr/bin/env python

import pkg_resources

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
Expand All @@ -13,6 +9,7 @@
from synphot import SpectralElement
import stsynphot as STS

from measure_extinction.utils.helpers import get_datapath
from measure_extinction.stardata import BandData
from measure_extinction.merge_obsspec import (
merge_iue_obsspec,
Expand Down Expand Up @@ -123,9 +120,7 @@ def get_phot(mwave, mflux, band_names, band_resp_filenames):
bdata = BandData("BAND")

# path for non-HST band response curves
data_path = pkg_resources.resource_filename(
"measure_extinction", "data/Band_RespCurves/"
)
data_path = f"{get_datapath()}/Band_RespCurves/"

Check warning on line 123 in measure_extinction/utils/make_obsdata_from_model.py

View check run for this annotation

Codecov / codecov/patch

measure_extinction/utils/make_obsdata_from_model.py#L123

Added line #L123 was not covered by tests

# compute the fluxes in each band
for k, cband in enumerate(band_names):
Expand Down
Loading

0 comments on commit ef05ba6

Please sign in to comment.