Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check SpEC version when importing #6467

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
16 changes: 5 additions & 11 deletions support/Pipelines/EccentricityControl/EccentricityControlParams.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import pandas as pd
import yaml

from spectre.support.CheckSpecImport import check_spec_import
from spectre.support.Yaml import SafeDumper
from spectre.Visualization.PlotTrajectories import import_A_and_B
from spectre.Visualization.ReadH5 import to_dataframe
Expand Down Expand Up @@ -86,17 +87,10 @@ def eccentricity_control_params(
Dictionary with the keys listed in 'EccentricityParams'.
"""
# Import functions from SpEC until we have ported them over
try:
from OmegaDotEccRemoval import (
ComputeOmegaAndDerivsFromFile,
performAllFits,
)
except ImportError:
raise ImportError(
"Importing from SpEC failed. Make sure you have pointed "
"'-D SPEC_ROOT' to a SpEC installation when configuring the build "
"with CMake."
)
check_spec_import(
contains_commit="ecfabf1ce78daeacbdd026625a02215c8e84af0e",
)
from OmegaDotEccRemoval import ComputeOmegaAndDerivsFromFile, performAllFits

# Make sure h5_files is a sequence
if isinstance(h5_files, str):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import numpy as np
from scipy.optimize import minimize

from spectre.support.CheckSpecImport import check_spec_import

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -103,14 +105,8 @@ def initial_orbital_parameters(
# call old Fortran code (LSODA) through scipy.integrate.odeint, which leads
# to lots of noise in stdout. When porting these functions, we should
# modernize them to use scipy.integrate.solve_ivp.
try:
from ZeroEccParamsFromPN import nOrbitsAndTotalTime, omegaAndAdot
except ImportError:
raise ImportError(
"Importing from SpEC failed. Make sure you have pointed "
"'-D SPEC_ROOT' to a SpEC installation when configuring the build "
"with CMake."
)
check_spec_import()
from ZeroEccParamsFromPN import nOrbitsAndTotalTime, omegaAndAdot

# Find an omega0 that gives the right number of orbits or time to merger
if num_orbits is not None or time_to_merger is not None:
Expand Down
1 change: 1 addition & 0 deletions support/Python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ particular supercomputer" OFF)
spectre_python_add_module(
support
PYTHON_FILES
CheckSpecImport.py
CliExceptions.py
DirectoryStructure.py
Logging.py
Expand Down
36 changes: 36 additions & 0 deletions support/Python/CheckSpecImport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Distributed under the MIT License.
# See LICENSE.txt for details.

from pathlib import Path
from typing import Optional


def check_spec_import(contains_commit: Optional[str] = None):
"""Check that the SpEC Python modules can be imported.

Arguments:
contains_commit: If specified, check that the SpEC repository contains
the specified commit. This can be used as a version check to ensure
that the installed SpEC version is compatible with the code that is
importing it.
"""
import git

try:
import Utils as spec_utils
except ImportError:
raise ImportError(
"Importing from SpEC failed. Make sure you have pointed "
"'-D SPEC_ROOT' to a SpEC installation when configuring the build "
"with CMake."
)
if contains_commit:
spec_repo = git.Repo(Path(spec_utils.__file__).parent.parent.parent)
try:
assert spec_repo.is_ancestor(contains_commit, spec_repo.head.commit)
except (AssertionError, git.exc.GitCommandError):
raise ImportError(
"SpEC version mismatch: requested revision"
f" {contains_commit} is not in the SpEC repository at"
f" {spec_repo.working_dir}."
)
2 changes: 2 additions & 0 deletions support/Python/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# Click: to compose our Python CLI, load subcommands lazily, and for
# autocompletion
click
# GitPython: to interact with Git repositories, e.g. SpEC version check
GitPython
h5py >= 3.5.0
# Humanize: For human-readable output like "2 hours ago"
humanize
Expand Down
Loading