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

Logging fix #184

Merged
merged 5 commits into from
Jan 5, 2022
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
3 changes: 2 additions & 1 deletion examples/1_Simple/logger_example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3

import logging
from simsopt import initialize_logging
from simsopt.util.log import initialize_logging

"""
Example file for transparently logging both MPI and serial jobs
Expand All @@ -20,6 +20,7 @@
comm = MPI.COMM_WORLD
except:
comm = None
print("MPI not found")

if comm is not None:
initialize_logging(mpi=True, filename='mpi.log')
Expand Down
1 change: 0 additions & 1 deletion src/simsopt/_core/graph_optimizable.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import logging

import numpy as np
from deprecated import deprecated

from ..util.types import RealArray, StrArray, BoolArray, Key
from .util import ImmutableId, OptimizableMeta, WeakKeyDefaultDict, \
Expand Down
4 changes: 0 additions & 4 deletions src/simsopt/mhd/boozer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from typing import Union, Iterable

import numpy as np
from ..util.dev import SimsoptRequires

logger = logging.getLogger(__name__)

Expand All @@ -36,9 +35,6 @@

from .._core.graph_optimizable import Optimizable

# Temporarily commenting out the decorator till __instancecheck__ method is made working
# @SimsoptRequires(MPI is not None, "mpi4py needs to be installed for running booz-xform"


class Boozer(Optimizable):
"""
Expand Down
2 changes: 0 additions & 2 deletions src/simsopt/mhd/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@
from .._core.graph_optimizable import Optimizable
from .._core.util import ObjectiveFailure
from ..geo.surfacerzfourier import SurfaceRZFourier
from ..util.dev import SimsoptRequires
if MPI is not None:
from ..util.mpi import MpiPartition
else:
MpiPartition = None


@SimsoptRequires(MPI is not None, "mpi4py needs to be installed for running SPEC")
class Spec(Optimizable):
"""
This class represents the SPEC equilibrium code.
Expand Down
3 changes: 0 additions & 3 deletions src/simsopt/mhd/vmec.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
vmec = None
logger.debug(str(e))

from ..util.dev import SimsoptRequires
from .._core.graph_optimizable import Optimizable
from .._core.util import Struct, ObjectiveFailure
from ..geo.surfacerzfourier import SurfaceRZFourier
Expand Down Expand Up @@ -79,8 +78,6 @@
# control its own run history


# Temporarily commenting out the decorator till __instancecheck__ method is made working
#@SimsoptRequires(MPI is not None, "mpi4py needs to be installed for running VMEC")
class Vmec(Optimizable):
r"""
This class represents the VMEC equilibrium code.
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/simsopt/util/mpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from typing import Union
import numpy as np

from monty.dev import requires
from .dev import SimsoptRequires
try:
from mpi4py import MPI
except ImportError:
Expand Down Expand Up @@ -42,7 +42,7 @@ def log(level: int = logging.INFO):
logger = logging.getLogger(__name__)


@requires(MPI is not None, "mpi4py is not installed")
@SimsoptRequires(MPI is not None, "mpi4py is not installed")
class MpiPartition:
"""
This module contains functions related to dividing up the set of
Expand Down
4 changes: 1 addition & 3 deletions src/simsopt/util/mpi_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@
import logging
import array
import atexit
import sys

from monty.dev import requires
#import mpi4py
try:
from mpi4py import MPI
except ImportError:
Expand Down Expand Up @@ -60,7 +59,6 @@ def _destroy_log_comm():
_log_comm_list = []


@requires(MPI is not None, "mpi4py is needed by MPILogHandler, but not installed")
class MPILogHandler(logging.Handler):
"""A Handler which logs messages over MPI to a single process
which then write them to a file.
Expand Down
31 changes: 31 additions & 0 deletions tests/util/test_dev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import unittest

try:
import numpy as np
except ImportError:
np = None

from simsopt.util.dev import SimsoptRequires, deprecated
from simsopt._core.graph_optimizable import Optimizable

@SimsoptRequires(np is not None, "numpy is not installed.")
class TestClass(Optimizable):
def __init__(self):
x = np.array([1.2, 0.9, -0.4])
fixed = np.full(3, False)
super().__init__(x0=x, fixed=fixed)

def J(self):
return np.exp(self.full_x[0] ** 2 - np.exp(self.full_x[1]) \
+ np.sin(self.full_x[2]))

return_fn_map = {'J': J}

class SimsoptRequiresTest(unittest.TestCase):
def test_instance_check(self):
tf = TestClass()
self.assertIsInstance(tf, TestClass)


if __name__ == '__main__':
unittest.main()