Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions autofit/aggregator/aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import os
import zipfile
from collections import defaultdict
from os import path
from pathlib import Path
from shutil import rmtree
from typing import List, Union, Iterator, Optional
Expand Down Expand Up @@ -84,8 +83,8 @@ def unzip_directory(directory: str):
for filename in filenames:
if filename.endswith(".zip"):
try:
with zipfile.ZipFile(path.join(root, filename), "r") as f:
f.extractall(path.join(root, filename[:-4]))
with zipfile.ZipFile(Path(root) / filename, "r") as f:
f.extractall(Path(root) / filename[:-4])
except zipfile.BadZipFile:
raise zipfile.BadZipFile(
f"File is not a zip file: \n " f"{root} \n" f"{filename}"
Expand Down Expand Up @@ -213,7 +212,7 @@ def remove_unzipped(self):
Removes the unzipped output directory for each phase.
"""
for phase in self.search_outputs:
split_path = path.split(phase.directory)[0]
split_path = Path(phase.directory).parent

rmtree(split_path, ignore_errors=True)

Expand Down
3 changes: 1 addition & 2 deletions autofit/aggregator/search_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
import pickle
from abc import ABC
from os import path
from pathlib import Path
from typing import Generator, Tuple, Optional, List, cast, Type

Expand Down Expand Up @@ -372,7 +371,7 @@ def header(self) -> str:
"""
phase = self.phase or ""
dataset_name = self.dataset_name or ""
return path.join(phase, dataset_name)
return str(Path(phase) / dataset_name) if dataset_name else phase

@property
def search(self):
Expand Down
3 changes: 2 additions & 1 deletion autofit/database/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from pathlib import Path

from .sqlalchemy_ import sa

Expand Down Expand Up @@ -66,7 +67,7 @@ def open_database(
exist_ok=True
)

exists = os.path.exists(filename)
exists = Path(filename).exists()

string = f'sqlite:///{filename}'
kwargs = dict(
Expand Down
4 changes: 2 additions & 2 deletions autofit/mapper/model_mapper.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
from pathlib import Path

from autofit.mapper.prior_model.collection import Collection

path = os.path.dirname(os.path.realpath(__file__))
path = Path(__file__).resolve().parent


class ModelMapper(Collection):
Expand Down
8 changes: 2 additions & 6 deletions autofit/non_linear/grid/grid_search/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import copy
import logging
import os
from os import path
from pathlib import Path
from typing import List, Tuple, Union, Type, Optional, Dict

from autoconf.dictable import to_dict
Expand Down Expand Up @@ -312,11 +312,7 @@ def job_for_analysis_grid_priors_and_values(
)
)

name_path = path.join(
self.paths.name,
self.paths.identifier,
"_".join(labels),
)
name_path = str(Path(self.paths.name) / self.paths.identifier / "_".join(labels))

search_instance = self.search_instance(name_path=name_path)
search_instance.paths.model = model
Expand Down
7 changes: 3 additions & 4 deletions autofit/non_linear/paths/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import zipfile
from abc import ABC, abstractmethod
from configparser import NoSectionError
from os import path
from pathlib import Path
from typing import Optional

Expand Down Expand Up @@ -224,7 +223,7 @@ def image_path(self) -> Path:
The path to the image folder.
"""

if not os.path.exists(self.output_path / f"image{self.image_path_suffix}"):
if not (self.output_path / f"image{self.image_path_suffix}").exists():
os.makedirs(self.output_path / f"image{self.image_path_suffix}")

return self.output_path / f"image{self.image_path_suffix}"
Expand Down Expand Up @@ -257,7 +256,7 @@ def output_path(self) -> Path:
if self.is_identifier_in_paths:
strings.append(self.identifier)

return Path(path.join("", *strings))
return Path(*strings) if strings else Path("")

@property
def _files_path(self) -> Path:
Expand Down Expand Up @@ -296,7 +295,7 @@ def restore(self):
Copy files from the ``.zip`` file to the samples folder.
"""

if path.exists(self._zip_path):
if Path(self._zip_path).exists():
shutil.rmtree(self.output_path, ignore_errors=True)

try:
Expand Down
11 changes: 5 additions & 6 deletions autofit/non_linear/paths/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import json
import numpy as np
import os
from os import path
from pathlib import Path
from typing import Optional, Union, cast, Type
import logging
Expand Down Expand Up @@ -175,14 +174,14 @@ def is_object(self, name: str) -> bool:
"""
Is there a file pickles/{name}.pickle?
"""
return os.path.exists(self._path_for_pickle(name))
return self._path_for_pickle(name).exists()

@property
def is_complete(self) -> bool:
"""
Has the search been completed?
"""
return path.exists(self._has_completed_path)
return self._has_completed_path.exists()

def save_search_internal(self, obj):
"""
Expand Down Expand Up @@ -214,7 +213,7 @@ def load_search_internal(self):
import emcee

backend_filename = self.search_internal_path / "search_internal.hdf"
if os.path.isfile(backend_filename):
if backend_filename.is_file():
return emcee.backends.HDFBackend(filename=str(backend_filename))
except ImportError:
pass
Expand Down Expand Up @@ -400,7 +399,7 @@ def is_grid_search(self) -> bool:
"""
Is this a grid search which comprises a number of child searches?
"""
return os.path.exists(self._grid_search_path)
return self._grid_search_path.exists()

def create_child(
self,
Expand Down Expand Up @@ -533,7 +532,7 @@ def _make_path(self) -> str:
The path terminates with the identifier, unless the identifier has already
been added to the path.
"""
path_ = Path(path.join(conf.instance.output_path, self.path_prefix, self.name))
path_ = Path(conf.instance.output_path) / self.path_prefix / self.name
if self.is_identifier_in_paths:
path_ = path_ / self.identifier
return path_
3 changes: 2 additions & 1 deletion autofit/non_linear/search/mcmc/blackjax/nuts/search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import pickle
from pathlib import Path
from typing import Optional

import numpy as np
Expand Down Expand Up @@ -349,7 +350,7 @@ def backend_filename(self):
@property
def backend(self) -> dict:
"""Load the pickled search-internal dict written by ``_fit``."""
if not os.path.isfile(self.backend_filename):
if not Path(self.backend_filename).is_file():
raise FileNotFoundError(
f"search_internal.pickle does not exist at "
f"{self.paths.search_internal_path}"
Expand Down
3 changes: 2 additions & 1 deletion autofit/non_linear/search/mcmc/emcee/search.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
from pathlib import Path
from typing import Dict, Optional

import numpy as np
Expand Down Expand Up @@ -371,7 +372,7 @@ def backend(self) -> "emcee.backends.HDFBackend":
"""
import emcee

if os.path.isfile(self.backend_filename):
if Path(self.backend_filename).is_file():
return emcee.backends.HDFBackend(filename=str(self.backend_filename))
else:
raise FileNotFoundError(
Expand Down
3 changes: 2 additions & 1 deletion autofit/non_linear/search/nest/dynesty/search/abstract.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
from abc import ABC
from pathlib import Path
from typing import Dict, Optional, Tuple, Union

import numpy as np
Expand Down Expand Up @@ -185,7 +186,7 @@ def _fit(
)

if not isinstance(self.paths, NullPaths):
checkpoint_exists = os.path.exists(self.checkpoint_file)
checkpoint_exists = Path(self.checkpoint_file).exists()
else:
checkpoint_exists = False

Expand Down
3 changes: 2 additions & 1 deletion autofit/non_linear/search/nest/nautilus/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os
import sys
from pathlib import Path
from typing import Dict, Optional, Tuple

from autofit.database.sqlalchemy_ import sa
Expand Down Expand Up @@ -169,7 +170,7 @@ def _fit(self, model: AbstractPriorModel, analysis):
"""

if not isinstance(self.paths, NullPaths):
checkpoint_exists = os.path.exists(self.checkpoint_file)
checkpoint_exists = Path(self.checkpoint_file).exists()
else:
checkpoint_exists = False

Expand Down
10 changes: 5 additions & 5 deletions autofit/tools/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def zip_directory(source_directory, output=None):
for root, dirs, files in os.walk(source_directory):
for file in files:
f.write(
os.path.join(root, file),
os.path.join(root[len(str(source_directory)) :], file),
Path(root) / file,
Path(root[len(str(source_directory)):]) / file,
)


Expand Down Expand Up @@ -94,12 +94,12 @@ def numpy_array_to_json(
numpy_array_to_json(array_2d=array_2d, file_path='/path/to/file/filename.json', overwrite=True)
"""

file_dir = os.path.split(file_path)[0]
file_dir = Path(file_path).parent

if not os.path.exists(file_dir):
if not file_dir.exists():
os.makedirs(file_dir)

if overwrite and os.path.exists(file_path):
if overwrite and Path(file_path).exists():
os.remove(file_path)

with open(file_path, "w+") as f:
Expand Down
6 changes: 3 additions & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
# documentation root, use Path(...).resolve() to make it absolute, like shown here.
#

import os
import sys
from pathlib import Path

sys.path.insert(0, os.path.abspath("."))
sys.path.insert(0, str(Path(".").resolve()))

import autofit

Expand Down
3 changes: 1 addition & 2 deletions test_autofit/aggregator/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from os import path
from pathlib import Path

import pytest
Expand Down Expand Up @@ -28,7 +27,7 @@ def make_directory():

@pytest.fixture(name="aggregator_directory")
def make_aggregator_directory(directory):
directory = path.dirname(path.realpath(__file__))
directory = Path(__file__).resolve().parent

return directory.parent / "tools" / "files" / "aggregator"

Expand Down
9 changes: 4 additions & 5 deletions test_autofit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import shutil
import sys
from os import path
from pathlib import Path
from unittest.mock import MagicMock

Expand Down Expand Up @@ -79,7 +78,7 @@ def __init__(self):
self.paths = []

def __call__(self, path, *args, **kwargs):
self.paths.append(path)
self.paths.append(str(path))


@pytest.fixture(name="plot_patch")
Expand All @@ -105,14 +104,14 @@ def remove_logs():
for d, _, files in os.walk(directory):
for file in files:
if file.endswith(".log"):
os.remove(path.join(d, file))
os.remove(Path(d) / file)


@pytest.fixture(autouse=True)
def set_config_path():
conf.instance.push(
new_path=path.join(directory, "config"),
output_path=path.join(directory, "output"),
new_path=str(directory / "config"),
output_path=str(directory / "output"),
)


Expand Down
9 changes: 2 additions & 7 deletions test_autofit/database/paths/test_paths.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import os
from pathlib import Path

import pytest
Expand Down Expand Up @@ -29,9 +28,7 @@ def query_fit(session, paths):
)
def test_create():
m.open_database("test.sqlite")
assert os.path.exists(
output_path
)
assert Path(output_path).exists()


@output_path_for_test(
Expand All @@ -53,9 +50,7 @@ def test_create_postgres():
)
except Exception as e:
logging.exception(e)
assert not os.path.exists(
output_path
)
assert not Path(output_path).exists()


def test_incomplete(paths):
Expand Down
4 changes: 1 addition & 3 deletions test_autofit/database/paths/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,4 @@ def test_is_database_paths(search):
# Analysis()
# )
#
# assert not os.path.exists(
# output_path
# )
# assert not Path(output_path).exists()
3 changes: 1 addition & 2 deletions test_autofit/non_linear/paths/test_paths.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import pickle
from pathlib import Path

Expand Down Expand Up @@ -61,7 +60,7 @@ def test_identifier_file(model):
paths.search = af.DynestyStatic()
paths.save_all({}, {})

assert os.path.exists(output_path / paths.identifier / ".identifier")
assert (output_path / paths.identifier / ".identifier").exists()


def test_serialize(model):
Expand Down
Loading
Loading