Skip to content

Commit

Permalink
Added remove_directory function to replace use of shutil
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasstolker committed Jun 12, 2024
1 parent 082a3a1 commit 48fb465
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 21 deletions.
6 changes: 2 additions & 4 deletions species/data/isochrone_data/iso_linder2019.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import shutil

from pathlib import Path

import h5py
Expand All @@ -9,7 +7,7 @@
from typeguard import typechecked

from species.core import constants
from species.util.data_util import extract_tarfile
from species.util.data_util import extract_tarfile, remove_directory


@typechecked
Expand Down Expand Up @@ -79,7 +77,7 @@ def add_linder2019(database: h5py._hl.files.File, input_path: str) -> None:
# unpacked because the file permissions are set to read-only
# such that the extract_tarfile will cause an error if the
# files need to be overwritten
shutil.rmtree(data_folder)
remove_directory(data_folder)

data_folder.mkdir()

Expand Down
6 changes: 2 additions & 4 deletions species/data/spec_data/spec_allers2013.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
<https://github.com/aburgasser/splat>`_.
"""

import shutil

from pathlib import Path

import h5py
Expand All @@ -19,7 +17,7 @@
from astroquery.simbad import Simbad
from typeguard import typechecked

from species.util.data_util import extract_tarfile
from species.util.data_util import extract_tarfile, remove_directory
from species.util.query_util import get_simbad


Expand Down Expand Up @@ -87,7 +85,7 @@ def add_allers2013(input_path: str, database: h5py._hl.files.File) -> None:
)

if data_folder.exists():
shutil.rmtree(data_folder)
remove_directory(data_folder)

print(f"\nUnpacking {print_text} (173 kB)...", end="", flush=True)
extract_tarfile(str(data_file), str(data_folder))
Expand Down
5 changes: 2 additions & 3 deletions species/data/spec_data/spec_bonnefoy2014.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""

import gzip
import shutil

from pathlib import Path

Expand All @@ -16,7 +15,7 @@
from astropy.io import fits
from typeguard import typechecked

from species.util.data_util import extract_tarfile
from species.util.data_util import extract_tarfile, remove_directory


@typechecked
Expand Down Expand Up @@ -58,7 +57,7 @@ def add_bonnefoy2014(input_path: str, database: h5py._hl.files.File) -> None:
)

if data_folder.exists():
shutil.rmtree(data_folder)
remove_directory(data_folder)

print(f"\nUnpacking {print_text} (2.3 MB)...", end="", flush=True)
extract_tarfile(str(data_file), str(data_folder))
Expand Down
6 changes: 2 additions & 4 deletions species/data/spec_data/spec_kesseli2017.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
Kesseli et al. (2017) to the database.
"""

import shutil

from pathlib import Path

import h5py
Expand All @@ -14,7 +12,7 @@
from astropy.io import fits
from typeguard import typechecked

from species.util.data_util import extract_tarfile
from species.util.data_util import extract_tarfile, remove_directory


@typechecked
Expand Down Expand Up @@ -53,7 +51,7 @@ def add_kesseli2017(input_path: str, database: h5py._hl.files.File) -> None:
)

if data_folder.exists():
shutil.rmtree(data_folder)
remove_directory(data_folder)

print(
"\nUnpacking SDSS spectra from Kesseli et al. 2017 (145 MB)...",
Expand Down
27 changes: 27 additions & 0 deletions species/util/data_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,3 +1065,30 @@ def convert_units(
)

return flux_out


@typechecked
def remove_directory(dir_in: Path) -> None:
"""
Function for removing all files and directories within a
specified input directory.
Parameters
----------
dir_in : Path
Directory that should be recursively removed, that is,
including all the files and directories within ``dir_in``.
Returns
-------
NoneType
None
"""

for path_item in dir_in.iterdir():
if path_item.is_dir():
remove_directory(path_item)
else:
path_item.unlink()

dir_in.rmdir()
14 changes: 8 additions & 6 deletions species/util/fit_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,14 @@ def get_residuals(
else:
n_fixed = 0

warnings.warn("The 'fixed_param' group is not found in "
f"the results of {tag}. Probably the "
"results were obtained with an older "
"version of the package. Please rerun "
"FitModel to update the results. Setting "
"the number of fixed parameters to zero.")
warnings.warn(
"The 'fixed_param' group is not found in "
f"the results of {tag}. Probably the "
"results were obtained with an older "
"version of the package. Please rerun "
"FitModel to update the results. Setting "
"the number of fixed parameters to zero."
)

print("\nModel parameters:")
for param_idx in range(n_param):
Expand Down

0 comments on commit 48fb465

Please sign in to comment.