Skip to content

Commit

Permalink
feat: import speed final (#3042)
Browse files Browse the repository at this point in the history
* import speed improvements

* import speed improvements
  • Loading branch information
seanpearsonuk authored Jul 10, 2024
1 parent 03d11f5 commit e1f430b
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 57 deletions.
18 changes: 12 additions & 6 deletions src/ansys/fluent/core/fluent_connection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Provides a module for Fluent connection functionality."""

from __future__ import annotations

from ctypes import c_int, sizeof
from dataclasses import dataclass
import itertools
Expand All @@ -22,12 +24,16 @@
from ansys.fluent.core.utils.file_transfer_service import RemoteFileTransferStrategy
from ansys.fluent.core.warnings import PyFluentDeprecationWarning
from ansys.platform.instancemanagement import Instance
import docker
from docker.models.containers import Container

logger = logging.getLogger("pyfluent.general")


def _docker():
import docker

return docker


class PortNotProvided(ValueError):
"""Raised when port is not provided."""

Expand Down Expand Up @@ -105,11 +111,11 @@ def get_container(container_id_or_name: str) -> Union[bool, Container, None]:
if not isinstance(container_id_or_name, str):
container_id_or_name = str(container_id_or_name)
try:
docker_client = docker.from_env()
docker_client = _docker().from_env()
container = docker_client.containers.get(container_id_or_name)
except docker.errors.NotFound: # NotFound is a child from DockerException
except _docker().errors.NotFound: # NotFound is a child from DockerException
return False
except docker.errors.DockerException as exc:
except _docker().errors.DockerException as exc:
logger.info(f"{type(exc).__name__}: {exc}")
return None
return container
Expand Down Expand Up @@ -515,7 +521,7 @@ def _force_exit_container(self):
if get_container(container_id):
try:
container.exec_run(["bash", cleanup_file_name], detach=True)
except docker.errors.APIError as e:
except _docker().errors.APIError as e:
logger.info(f"{type(e).__name__}: {e}")
logger.debug(
"Caught Docker APIError, Docker container probably not running anymore."
Expand Down
3 changes: 2 additions & 1 deletion src/ansys/fluent/core/launcher/fluent_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
from ansys.fluent.core.session import _parse_server_info_file
from ansys.fluent.core.utils.execution import timeout_loop
from ansys.fluent.core.utils.networking import get_free_port
import docker

logger = logging.getLogger("pyfluent.launcher")
DEFAULT_CONTAINER_MOUNT_PATH = "/mnt/pyfluent"
Expand Down Expand Up @@ -400,6 +399,8 @@ def start_fluent_container(
host_server_info_file.touch(exist_ok=True)
last_mtime = host_server_info_file.stat().st_mtime

import docker

docker_client = docker.from_env()

logger.debug("Starting Fluent docker container...")
Expand Down
46 changes: 26 additions & 20 deletions src/ansys/fluent/core/launcher/launcher_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import time
from typing import Any, Dict, Union

from beartype import BeartypeConf, beartype

from ansys.fluent.core.exceptions import InvalidArgument
from ansys.fluent.core.utils.networking import find_remoting_ip

Expand Down Expand Up @@ -79,25 +77,33 @@ def _confirm_watchdog_start(start_watchdog, cleanup_on_exit, fluent_connection):
return start_watchdog


@beartype(conf=BeartypeConf(violation_type=TypeError))
def _build_journal_argument(
topy: Union[None, bool, str], journal_file_names: Union[None, str, list[str]]
) -> str:
"""Build Fluent commandline journal argument."""
if topy and not journal_file_names:
raise InvalidArgument(
"Use 'journal_file_names' to specify and convert journal files."
)
fluent_jou_arg = ""
if isinstance(journal_file_names, str):
journal_file_names = [journal_file_names]
if journal_file_names:
fluent_jou_arg += "".join(
[f' -i "{journal}"' for journal in journal_file_names]
)
if topy:
if isinstance(topy, str):
fluent_jou_arg += f' -topy="{topy}"'
else:
fluent_jou_arg += " -topy"
return fluent_jou_arg

from beartype import BeartypeConf, beartype

@beartype(conf=BeartypeConf(violation_type=TypeError))
def _impl(
topy: Union[None, bool, str], journal_file_names: Union[None, str, list[str]]
) -> str:
if topy and not journal_file_names:
raise InvalidArgument(
"Use 'journal_file_names' to specify and convert journal files."
)
fluent_jou_arg = ""
if isinstance(journal_file_names, str):
journal_file_names = [journal_file_names]
if journal_file_names:
fluent_jou_arg += "".join(
[f' -i "{journal}"' for journal in journal_file_names]
)
if topy:
if isinstance(topy, str):
fluent_jou_arg += f' -topy="{topy}"'
else:
fluent_jou_arg += " -topy"
return fluent_jou_arg

return _impl(topy, journal_file_names)
4 changes: 2 additions & 2 deletions src/ansys/fluent/core/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import os
from typing import Optional, Union

import yaml

_logging_file_enabled = False


Expand Down Expand Up @@ -80,6 +78,8 @@ def get_default_config() -> dict:
'pyfluent.tui': {'handlers': ['pyfluent_file'], 'level': 'DEBUG'}},
'version': 1}
"""
import yaml

file_name = os.path.abspath(__file__)
file_dir = os.path.dirname(file_name)
yaml_path = os.path.join(file_dir, "logging_config.yaml")
Expand Down
41 changes: 22 additions & 19 deletions src/ansys/fluent/core/solver/flobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,20 @@
import weakref
from zipimport import zipimporter

try:
from ansys.fluent.core.warnings import (
PyFluentDeprecationWarning,
PyFluentUserWarning,
)
import ansys.units as ansys_units

from .flunits import UnhandledQuantity, get_si_unit_for_fluent_quantity
except ImportError:
get_unit_for_fl_quantity_attr = None
ansys_units = None
PyFluentDeprecationWarning = FutureWarning
PyFluentUserWarning = UserWarning
from ansys.fluent.core.warnings import PyFluentDeprecationWarning, PyFluentUserWarning

from .flunits import UnhandledQuantity, get_si_unit_for_fluent_quantity


def _ansys_units():

try:
import ansys.units

return ansys.units
except ImportError:
pass


import ansys.fluent.core as pyfluent

Expand Down Expand Up @@ -474,17 +475,17 @@ class RealNumerical(Numerical):
Get the units string.
"""

def as_quantity(self) -> Optional[ansys_units.Quantity]:
def as_quantity(self) -> Optional[ansys.units.Quantity]:
"""Get the state of the object as an ansys.units.Quantity."""
error = None
if not ansys_units:
if not _ansys_units():
error = "Code not configured to support units."
if not error:
quantity = self.get_attr("units-quantity")
units = get_si_unit_for_fluent_quantity(quantity)
if units is not None:
try:
return ansys_units.Quantity(
return _ansys_units().Quantity(
value=self.get_state(),
units=units,
)
Expand Down Expand Up @@ -520,9 +521,11 @@ def get_units():
raise UnhandledQuantity(self.path, state)
return units

if ansys_units and isinstance(state, (ansys_units.Quantity, tuple)):
if _ansys_units() and isinstance(state, (_ansys_units().Quantity, tuple)):
state = (
ansys_units.Quantity(*state) if isinstance(state, tuple) else state
_ansys_units().Quantity(*state)
if isinstance(state, tuple)
else state
)
state = state.to(get_units()).value
elif isinstance(state, tuple):
Expand Down Expand Up @@ -712,7 +715,7 @@ def _unalias(cls, value):
def set_state(self, state: Optional[StateT] = None, **kwargs):
"""Set the state of the object."""
with self._while_setting_state():
if isinstance(state, (tuple, ansys_units.Quantity)) and hasattr(
if isinstance(state, (tuple, _ansys_units().Quantity)) and hasattr(
self, "value"
):
self.value.set_state(state, **kwargs)
Expand Down
13 changes: 9 additions & 4 deletions src/ansys/fluent/core/streaming_services/monitor_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
from typing import Dict, List, Optional, Tuple, Union

import numpy as np
import pandas as pd

from ansys.api.fluent.v0 import monitor_pb2 as MonitorModule
from ansys.fluent.core.streaming_services.streaming import StreamingService


def _pandas():
import pandas

return pandas


class MonitorsManager(StreamingService):
"""Manages monitors (Fluent residuals and report definitions monitors).
Expand Down Expand Up @@ -161,12 +166,12 @@ def _populate_dataframes(self, data_received, *args, **kwargs):
monitor_data.append(data_received[monitor_name])

if monitor_data:
new_df = pd.DataFrame([monitor_data], columns=monitors)
new_df = _pandas().DataFrame([monitor_data], columns=monitors)
new_df.set_index("xvalues", inplace=True)
if df.empty:
df_data["df"] = new_df
else:
df_data["df"] = pd.concat([df, new_df])
df_data["df"] = _pandas().concat([df, new_df])
for callback_map in self._service_callbacks.values():
callback, args, kwargs = callback_map
callback(*args, **kwargs)
Expand Down Expand Up @@ -203,7 +208,7 @@ def _update_dataframe(self):
continue
self._data_frames[monitor_set_name] = {}
monitors_name = list(monitor_set_info["monitors"]) + ["xvalues"]
df = pd.DataFrame([], columns=monitors_name)
df = _pandas().DataFrame([], columns=monitors_name)
df.set_index("xvalues", inplace=True)
self._data_frames[monitor_set_name]["df"] = df
self._data_frames[monitor_set_name]["monitors"] = monitors_name
3 changes: 2 additions & 1 deletion src/ansys/fluent/core/utils/file_transfer_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import ansys.fluent.core as pyfluent
from ansys.fluent.core.warnings import PyFluentUserWarning
import ansys.platform.instancemanagement as pypim
import docker

logger = logging.getLogger("pyfluent.file_transfer_service")

Expand Down Expand Up @@ -144,6 +143,8 @@ def __init__(
host_mount_path: Union[str, Path], optional
Existing path in the host operating system to be available inside the container.
"""
import docker

self.docker_client = docker.from_env()
self.image_name = (
image_name if image_name else "ghcr.io/ansys/tools-filetransfer"
Expand Down
4 changes: 2 additions & 2 deletions src/ansys/fluent/core/utils/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from typing import Any, Optional
import warnings

from ansys.fluent.core.services.datamodel_tui import TUIMenu
from ansys.fluent.core.session_solver import Solver
from ansys.fluent.core.solver import flobject
from ansys.fluent.core.solver.error_message import closest_allowed_names
from ansys.fluent.core.utils.fluent_version import (
Expand Down Expand Up @@ -66,7 +64,9 @@ def _remove_suffix(input: str, suffix):

def _get_version_path_prefix_from_obj(obj: Any):
from ansys.fluent.core.services.datamodel_se import PyMenu, PyNamedObjectContainer
from ansys.fluent.core.services.datamodel_tui import TUIMenu
from ansys.fluent.core.session_pure_meshing import PureMeshing
from ansys.fluent.core.session_solver import Solver

path = None
version = None
Expand Down
3 changes: 1 addition & 2 deletions tests/test_flobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,8 +992,7 @@ def _check_vector_units(obj, units):
assert len(state) == len(state_with_units[0])
assert all(x == y for x, y in zip(state, state_with_units[0]))
assert units == state_with_units[1]
if flobject.ansys_units:
assert obj.as_quantity() == ansys.units.Quantity(obj.get_state(), units)
assert obj.as_quantity() == ansys.units.Quantity(obj.get_state(), units)


@pytest.mark.fluent_version(">=24.1")
Expand Down

0 comments on commit e1f430b

Please sign in to comment.