Skip to content

Commit

Permalink
changing path handling to pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
temuller committed Jan 25, 2024
1 parent f3a3dbe commit 9c4082d
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 61 deletions.
9 changes: 5 additions & 4 deletions src/hostphot/cutouts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
from pathlib import Path

import glob
import copy
import shutil
Expand Down Expand Up @@ -42,7 +44,7 @@
survey_pixel_scale,
)
import hostphot
hostphot_path = hostphot.__path__[0]
hostphot_path = Path(hostphot.__path__[0])

import warnings
from astropy.utils.exceptions import AstropyWarning
Expand Down Expand Up @@ -578,7 +580,6 @@ def get_WISE_images(ra, dec, size=3, filters=None):
size_arcsec = size.to(u.arcsec)

pixel_scale = survey_pixel_scale(survey)
# size_pixels = int(size_arcsec.value / pixel_scale)

with warnings.catch_warnings():
warnings.simplefilter("ignore", AstropyWarning)
Expand Down Expand Up @@ -682,7 +683,7 @@ def get_unWISE_images(ra, dec, size=3, filters=None, version="allwise"):
master_url = base_url + params_url

response = requests.get(master_url, stream=True)
target_file = f"unWISE_images_{ra}_{dec}.tar.gz" # current directory
target_file = Path(f"unWISE_images_{ra}_{dec}.tar.gz") # current directory
if response.status_code == 200:
with open(target_file, "wb") as f:
f.write(response.raw.read())
Expand Down Expand Up @@ -1558,7 +1559,7 @@ def get_SPLUS_images(ra, dec, size=3, filters=None):

# add zeropoint
# file from https://splus.cloud/documentation/dr2_3
zps_file = os.path.join(hostphot_path, 'filters', 'SPLUS', 'iDR3_zps.cat')
zps_file = hostphot_path.joinpath('filters', 'SPLUS', 'iDR3_zps.cat')
zps_df = pd.read_csv(zps_file, delim_whitespace=True)

field = hdu[0].header['OBJECT'].replace('_', '-')
Expand Down
14 changes: 8 additions & 6 deletions src/hostphot/dust.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
from pathlib import Path

import tarfile
import requests
import numpy as np
Expand All @@ -18,15 +20,15 @@ def _download_dustmaps():
"""Downloads the dust maps for extinction calculation if they are not found
locally.
"""
mapsdir = hostphot.__path__[0]
mapsdir = Path(hostphot.__path__[0])

# check if files already exist locally
dust_files = [
os.path.join(mapsdir, "sfddata-master", f"SFD_dust_4096_{sky}gp.fits")
mapsdir.joinpath("sfddata-master", rf"SFD_dust_4096_{sky}gp.fits")
for sky in ["n", "s"]
]
mask_files = [
os.path.join(mapsdir, "sfddata-master", f"SFD_mask_4096_{sky}gp.fits")
mapsdir.joinpath("sfddata-master", rf"SFD_mask_4096_{sky}gp.fits")
for sky in ["n", "s"]
]
maps_files = dust_files + mask_files
Expand All @@ -39,7 +41,7 @@ def _download_dustmaps():
)
response = requests.get(sfdmaps_url)

master_tar = "master.tar.gz"
master_tar = Path("master.tar.gz")
with open(master_tar, "wb") as file:
file.write(response.content)

Expand Down Expand Up @@ -93,8 +95,8 @@ def deredden(
"""
_download_dustmaps()

hostphot_path = hostphot.__path__[0]
dustmaps_dir = os.path.join(hostphot_path, "sfddata-master")
hostphot_path = Path(hostphot.__path__[0])
dustmaps_dir = hostphot_path / "sfddata-master"

with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
Expand Down
24 changes: 11 additions & 13 deletions src/hostphot/global_photometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
#
# Some parts of this notebook are based on https://github.com/djones1040/PS1_surface_brightness/blob/master/Surface%20Brightness%20Tutorial.ipynb and codes from Lluís Galbany

import os
from pathlib import Path

import pickle
import numpy as np
import pandas as pd
Expand Down Expand Up @@ -239,14 +240,14 @@ def extract_kronparams(
"""
check_survey_validity(survey)
check_work_dir(workdir)
obj_dir = os.path.join(workdir, name)
obj_dir = Path(workdir, name)
if use_mask:
suffix = "masked_"
else:
suffix = ""
if isinstance(filt, list):
filt = "".join(f for f in filt)
fits_file = os.path.join(obj_dir, f"{suffix}{survey}_{filt}.fits")
fits_file = obj_dir / rf"{suffix}{survey}_{filt}.fits"

hdu = fits.open(fits_file)
hdu = remove_nan(hdu)
Expand Down Expand Up @@ -300,7 +301,7 @@ def extract_kronparams(
)

if save_plots is True:
outfile = os.path.join(obj_dir, f"global_{survey}_{filt}.jpg")
outfile = obj_dir / f"global_{survey}_{filt}.jpg"
title = f"{name}: {survey}-${filt}$"
plot_detected_objects(
hdu,
Expand All @@ -320,9 +321,7 @@ def extract_kronparams(
flip = False

if save_aperture_params is True:
outfile = os.path.join(
obj_dir, f"{survey}_{filt}_aperture_parameters.pickle"
)
outfile = obj_dir / rf"{survey}_{filt}_aperture_parameters.pickle"
with open(outfile, "wb") as fp:
aperture_parameters = gal_obj, img_wcs, kronrad, scale, flip
pickle.dump(aperture_parameters, fp, protocol=4)
Expand All @@ -348,8 +347,7 @@ def load_aperture_params(name, filt, survey):
Aperture paremeters with the same format as the output of the
``extract_kronparams`` function.
"""
inputfile = os.path.join(workdir, name,
f'{survey}_{filt}_aperture_parameters.pickle')
inputfile = Path(workdir, name, rf'{survey}_{filt}_aperture_parameters.pickle')

aperture_params = load_pickle(inputfile)

Expand Down Expand Up @@ -449,12 +447,12 @@ def photometry(

check_survey_validity(survey)
check_work_dir(workdir)
obj_dir = os.path.join(workdir, name)
obj_dir = Path(workdir, name)
if use_mask:
suffix = "masked_"
else:
suffix = ""
fits_file = os.path.join(obj_dir, f"{suffix}{survey}_{filt}.fits")
fits_file = obj_dir / f"{suffix}{survey}_{filt}.fits"

hdu = fits.open(fits_file)
hdu = remove_nan(hdu)
Expand Down Expand Up @@ -566,7 +564,7 @@ def photometry(
flux *= 10**(0.4*A_ext)

if save_plots is True:
outfile = os.path.join(obj_dir, f"global_{survey}_{filt}.jpg")
outfile = obj_dir / f"global_{survey}_{filt}.jpg"
title = f"{name}: {survey}-${filt}$"
plot_detected_objects(
hdu,
Expand Down Expand Up @@ -753,7 +751,7 @@ def multi_band_phot(
results_dict[f"{filt}_zeropoint"] = np.nan

if save_results is True:
outfile = os.path.join(workdir, name, f"{survey}_global.csv")
outfile = Path(workdir, name, rf"{survey}_global.csv")
phot_df = pd.DataFrame(
{key: [val] for key, val in results_dict.items()}
)
Expand Down
18 changes: 8 additions & 10 deletions src/hostphot/image_masking.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from pathlib import Path

import pickle
import numpy as np
from copy import deepcopy
Expand Down Expand Up @@ -178,8 +179,8 @@ def create_mask(
if isinstance(filt, list):
filt = "".join(f for f in filt)

obj_dir = os.path.join(workdir, name)
fits_file = os.path.join(obj_dir, f"{survey}_{filt}.fits")
obj_dir = Path(workdir, name)
fits_file = obj_dir / f"{survey}_{filt}.fits"
hdu = fits.open(fits_file)
hdu = remove_nan(hdu)

Expand Down Expand Up @@ -238,7 +239,7 @@ def create_mask(
masked_data = mask_image(data_sub, nogal_objs, r=r, sigma=sigma)
masked_hdu = deepcopy(hdu)
masked_hdu[0].data = masked_data
outfile = os.path.join(obj_dir, f"masked_{survey}_{filt}.fits")
outfile = obj_dir / f"masked_{survey}_{filt}.fits"
masked_hdu.writeto(outfile, overwrite=True)

if survey in ["DES", "VISTA", "UKIDSS"]:
Expand All @@ -247,15 +248,13 @@ def create_mask(
flip = False

if save_mask_params is True:
outfile = os.path.join(
obj_dir, f"{survey}_{filt}_mask_parameters.pickle"
)
outfile = obj_dir / f"{survey}_{filt}_mask_parameters.pickle"
with open(outfile, "wb") as fp:
mask_parameters = gal_obj, nogal_objs, img_wcs, sigma, r, flip
pickle.dump(mask_parameters, fp, protocol=4)

if save_plots:
outfile = os.path.join(obj_dir, f"masked_{survey}_{filt}.jpg")
outfile = obj_dir / f"masked_{survey}_{filt}.jpg"
title = f"{name}: {survey}-${filt}$"
plot_masked_image(
hdu,
Expand Down Expand Up @@ -293,8 +292,7 @@ def load_mask_params(name, filt, survey):
Mask paremeters with the same format as the output of the
``create_mask`` function.
"""
inputfile = os.path.join(workdir, name,
f'{survey}_{filt}_mask_parameters.pickle')
inputfile = Path(workdir, name, rf'{survey}_{filt}_mask_parameters.pickle')

mask_params = load_pickle(inputfile)

Expand Down
13 changes: 6 additions & 7 deletions src/hostphot/local_photometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
#
# Some parts of this notebook are based on https://github.com/djones1040/PS1_surface_brightness/blob/master/Surface%20Brightness%20Tutorial.ipynb and codes from Lluís Galbany

import os
from pathlib import Path

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -244,12 +245,12 @@ def photometry(

check_survey_validity(survey)
check_work_dir(workdir)
obj_dir = os.path.join(workdir, name)
obj_dir = Path(workdir, name)
if use_mask:
suffix = "masked_"
else:
suffix = ""
fits_file = os.path.join(obj_dir, f"{suffix}{survey}_{filt}.fits")
fits_file = obj_dir / f"{suffix}{survey}_{filt}.fits"

hdu = fits.open(fits_file)
hdu = remove_nan(hdu)
Expand Down Expand Up @@ -337,9 +338,7 @@ def photometry(
fluxes_err.append(flux_err)

if save_plots:
outfile = os.path.join(
obj_dir, f"local_{survey}_{filt}_{ap_radius}kpc.jpg"
)
outfile = obj_dir / rf"local_{survey}_{filt}_{ap_radius}kpc.jpg"
title = f'{name}: {survey}-${filt}$|r$={ap_radius}$ kpc @ $z={z}$'
plot_aperture(hdu, px, py, radius_pix, title, outfile)

Expand Down Expand Up @@ -471,7 +470,7 @@ def multi_band_phot(
results_dict[f"{filt}_zeropoint"] = np.nan

if save_results is True:
outfile = os.path.join(workdir, name, f"{survey}_local.csv")
outfile = Path(workdir, name, rf"{survey}_local.csv")
phot_df = pd.DataFrame(
{key: [val] for key, val in results_dict.items()}
)
Expand Down
16 changes: 9 additions & 7 deletions src/hostphot/sed_plotting.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
from pathlib import Path

import glob
import numpy as np
import pandas as pd
Expand All @@ -9,8 +11,8 @@
from hostphot._constants import workdir, font_family
from hostphot.utils import get_survey_filters

path = hostphot.__path__[0]
config_file = os.path.join(path, 'filters', 'config.txt')
path = Path(hostphot.__path__[0])
config_file = path.joinpath('filters', 'config.txt')
config_df = pd.read_csv(config_file, delim_whitespace=True)

colours = {'GALEX':'purple', 'PS1':'green', 'SDSS':'blue', 'DES':'lightblue',
Expand All @@ -34,11 +36,11 @@ def get_eff_wave(filt, survey):
eff_wave: float
Effective wavelength in angstroms.
"""
path = hostphot.__path__[0]
path = Path(hostphot.__path__[0])
if survey=='unWISE':
survey = 'WISE'

survey_files = glob.glob(os.path.join(path, 'filters', survey, '*'))
survey_files = glob.glob(path.joinpath('filters', survey, '*'))
filt_file = [file for file in survey_files if file.endswith(f'_{filt}.dat')][0]

wave, trans = np.loadtxt(filt_file).T
Expand Down Expand Up @@ -83,7 +85,7 @@ def plot_sed(name, phot_type='global', z=None, radius=None, include=None, exclud
assert radius is not None, "radius must be given with local photometry"

global colours
obj_path = os.path.join(workdir, name, '*')
obj_path = Path(workdir, name, '*')
phot_files = [file for file in glob.glob(obj_path)
if file.endswith(f'_{phot_type}.csv')]

Expand Down Expand Up @@ -218,9 +220,9 @@ def plot_sed(name, phot_type='global', z=None, radius=None, include=None, exclud

if save_plot is True:
if outfile is None:
obj_dir = os.path.join(workdir, name)
obj_dir = Path(workdir, name)
basename = f'sed_{phot_type}.jpg'
outfile = os.path.join(obj_dir, basename)
outfile = obj_dir / basename
plt.savefig(outfile)

plt.show()
Loading

0 comments on commit 9c4082d

Please sign in to comment.