Skip to content

Commit

Permalink
ENH: never import ytcfg at the top level of a module, so that yt.conf…
Browse files Browse the repository at this point in the history
…ig doesn't need to be run at all at import time
  • Loading branch information
neutrinoceros committed Oct 28, 2021
1 parent 3ecb96a commit cfc81eb
Show file tree
Hide file tree
Showing 24 changed files with 99 additions and 33 deletions.
19 changes: 18 additions & 1 deletion yt/data_objects/construction_data_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from more_itertools import always_iterable
from tqdm import tqdm

from yt.config import ytcfg
from yt.data_objects.field_data import YTFieldData
from yt.data_objects.selection_objects.data_selection_objects import (
YTSelectionContainer1D,
Expand Down Expand Up @@ -497,6 +496,8 @@ def _mrep(self):
return MinimalProjectionData(self)

def deserialize(self, fields):
from yt.config import ytcfg

if not ytcfg.get("yt", "serialize"):
return False
for field in fields:
Expand All @@ -516,6 +517,8 @@ def deserialize(self, fields):
return deserialized_successfully

def serialize(self):
from yt.config import ytcfg

if not ytcfg.get("yt", "serialize"):
return
self._mrep.store(self.ds.parameter_filename + ".yt")
Expand Down Expand Up @@ -1882,6 +1885,8 @@ def export_obj(
... )
"""
from yt.config import ytcfg

if color_map is None:
color_map = ytcfg.get("yt", "default_colormap")
if self.vertices is None:
Expand Down Expand Up @@ -2001,6 +2006,8 @@ def _export_obj(
emit_field_max=None,
emit_field_min=None,
):
from yt.config import ytcfg

if color_map is None:
color_map = ytcfg.get("yt", "default_colormap")
if plot_index is None:
Expand Down Expand Up @@ -2213,6 +2220,8 @@ def export_blender(
... )
"""
from yt.config import ytcfg

if color_map is None:
color_map = ytcfg.get("yt", "default_colormap")
if self.vertices is None:
Expand Down Expand Up @@ -2258,6 +2267,8 @@ def _export_blender(
emit_field_max=None,
emit_field_min=None,
):
from yt.config import ytcfg

if color_map is None:
color_map = ytcfg.get("yt", "default_colormap")
if plot_index is None:
Expand Down Expand Up @@ -2359,6 +2370,8 @@ def export_ply(
... ]
>>> surf.export_ply("my_galaxy.ply", bounds=bounds)
"""
from yt.config import ytcfg

if color_map is None:
color_map = ytcfg.get("yt", "default_colormap")
if self.vertices is None:
Expand Down Expand Up @@ -2394,6 +2407,8 @@ def _export_ply(
color_log=True,
sample_type="face",
):
from yt.config import ytcfg

if color_map is None:
color_map = ytcfg.get("yt", "default_colormap")
if hasattr(filename, "read"):
Expand Down Expand Up @@ -2548,6 +2563,8 @@ def export_sketchfab(
... )
...
"""
from yt.config import ytcfg

if color_map is None:
color_map = ytcfg.get("yt", "default_colormap")
api_key = api_key or ytcfg.get("yt", "sketchfab_api_key")
Expand Down
3 changes: 2 additions & 1 deletion yt/data_objects/image_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import numpy as np

from yt.config import ytcfg
from yt.units.yt_array import YTArray
from yt.visualization.image_writer import write_bitmap, write_image

Expand Down Expand Up @@ -387,6 +386,8 @@ def write_image(
>>> im_arr.write_image("test_ImageArray.png")
"""
from yt.config import ytcfg

if cmap_name is None:
cmap_name = ytcfg.get("yt", "default_colormap")
if filename is not None and filename[-4:] != ".png":
Expand Down
7 changes: 3 additions & 4 deletions yt/data_objects/index_subobjects/grid_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import numpy as np

import yt.geometry.particle_deposit as particle_deposit
from yt.config import ytcfg
from yt.data_objects.selection_objects.data_selection_objects import (
YTSelectionContainer,
)
Expand All @@ -20,8 +19,6 @@
from yt.utilities.lib.mesh_utilities import clamp_edges
from yt.utilities.nodal_data_utils import get_nodal_slices

RECONSTRUCT_INDEX = bool(ytcfg.get("yt", "reconstruct_index"))


class AMRGridPatch(YTSelectionContainer):
_spatial = True
Expand Down Expand Up @@ -163,14 +160,16 @@ def _prepare_grid(self):
# This is definitely the slowest part of generating the index
# Now we give it pointers to all of its attributes
# Note that to keep in line with Enzo, we have broken PEP-8
from yt.config import ytcfg

h = self.index # cache it
my_ind = self.id - self._id_offset
self.ActiveDimensions = h.grid_dimensions[my_ind]
self.LeftEdge = h.grid_left_edge[my_ind]
self.RightEdge = h.grid_right_edge[my_ind]
# This can be expensive so we allow people to disable this behavior
# via a config option
if RECONSTRUCT_INDEX:
if ytcfg.get("yt", "reconstruct_index"):
if is_sequence(self.Parent) and len(self.Parent) > 0:
p = self.Parent[0]
else:
Expand Down
3 changes: 2 additions & 1 deletion yt/data_objects/particle_trajectories.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np

from yt.config import ytcfg
from yt.data_objects.field_data import YTFieldData
from yt.funcs import get_pbar, mylog
from yt.units.yt_array import array_like_field
Expand Down Expand Up @@ -59,6 +58,7 @@ class ParticleTrajectories:
def __init__(
self, outputs, indices, fields=None, suppress_logging=False, ptype=None
):
from yt.config import ytcfg

indices.sort() # Just in case the caller wasn't careful
self.field_data = YTFieldData()
Expand Down Expand Up @@ -218,6 +218,7 @@ def _get_data(self, fields):
The trajectory collection itself is a dict of 2D numpy arrays,
with shape (num_indices, num_steps)
"""
from yt.config import ytcfg

missing_fields = [field for field in fields if field not in self.field_data]
if not missing_fields:
Expand Down
7 changes: 5 additions & 2 deletions yt/data_objects/static_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from unyt.exceptions import UnitConversionError, UnitParseError

from yt._maintenance.deprecation import issue_deprecation_warning
from yt.config import ytcfg
from yt.data_objects.particle_filters import filter_registry
from yt.data_objects.particle_unions import ParticleUnion
from yt.data_objects.region_expression import RegionExpression
Expand Down Expand Up @@ -57,7 +56,9 @@


_cached_datasets = weakref.WeakValueDictionary()
_ds_store = ParameterFileStore()

# TODO: this needs an actual solution
_ds_store = None # ParameterFileStore()


def _unsupported_object(ds, obj_name):
Expand Down Expand Up @@ -149,6 +150,8 @@ class Dataset(abc.ABC):
domain_center = MutableAttribute(True)

def __new__(cls, filename=None, *args, **kwargs):
from yt.config import ytcfg

if not isinstance(filename, str):
obj = object.__new__(cls)
# The Stream frontend uses a StreamHandler object to pass metadata
Expand Down
3 changes: 2 additions & 1 deletion yt/data_objects/time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import numpy as np
from more_itertools import always_iterable

from yt.config import ytcfg
from yt.data_objects.analyzer_objects import AnalysisTask, create_quantity_proxy
from yt.data_objects.particle_trajectories import ParticleTrajectories
from yt.funcs import is_sequence, mylog
Expand Down Expand Up @@ -193,6 +192,8 @@ def _get_filenames_from_glob_pattern(outputs):
Helper function to DatasetSeries.__new__
handle a special case where "outputs" is assumed to be really a pattern string
"""
from yt.config import ytcfg

pattern = outputs
epattern = os.path.expanduser(pattern)
data_dir = ytcfg.get("yt", "test_data_dir")
Expand Down
3 changes: 2 additions & 1 deletion yt/fields/xray_emission_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import numpy as np

from yt.config import ytcfg
from yt.fields.derived_field import DerivedField
from yt.funcs import mylog, only_on_root, parse_h5_attr
from yt.units.yt_array import YTArray, YTQuantity
Expand All @@ -20,6 +19,8 @@


def _get_data_file(table_type, data_dir=None):
from yt.config import ytcfg

data_file = "%s_emissivity_v%d.h5" % (table_type, data_version[table_type])
if data_dir is None:
supp_data_dir = ytcfg.get("yt", "supp_data_dir")
Expand Down
2 changes: 1 addition & 1 deletion yt/frontends/amrvac/data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import numpy as np
from more_itertools import always_iterable

from yt.config import ytcfg
from yt.data_objects.index_subobjects.grid_patch import AMRGridPatch
from yt.data_objects.static_output import Dataset
from yt.funcs import mylog, setdefaultattr
Expand Down Expand Up @@ -191,6 +190,7 @@ def __init__(
"""
# note: geometry_override and parfiles are specific to this frontend
from yt.config import ytcfg

self._geometry_override = geometry_override
super().__init__(
Expand Down
2 changes: 1 addition & 1 deletion yt/frontends/fits/data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import numpy.core.defchararray as np_char
from more_itertools import always_iterable

from yt.config import ytcfg
from yt.data_objects.index_subobjects.grid_patch import AMRGridPatch
from yt.data_objects.static_output import Dataset
from yt.funcs import mylog, setdefaultattr
Expand Down Expand Up @@ -330,6 +329,7 @@ def __init__(
units_override=None,
unit_system="cgs",
):
from yt.config import ytcfg

if parameters is None:
parameters = {}
Expand Down
2 changes: 1 addition & 1 deletion yt/frontends/owls/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import numpy as np

from yt.config import ytcfg
from yt.fields.species_fields import (
add_species_field_by_density,
add_species_field_by_fraction,
Expand Down Expand Up @@ -319,6 +318,7 @@ def setup_fluid_fields(self):
# exist it will download the data from http://yt-project.org/data
# -------------------------------------------------------------
def _get_owls_ion_data_dir(self):
from yt.config import ytcfg

txt = "Attempting to download ~ 30 Mb of owls ion data from %s to %s."
data_file = "owls_ion_data.tar.gz"
Expand Down
6 changes: 4 additions & 2 deletions yt/frontends/ramses/definitions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# These functions are RAMSES-specific
import re

from yt.config import ytcfg
from yt.funcs import mylog
# from yt.funcs import mylog


def ramses_header(hvals):
Expand Down Expand Up @@ -69,6 +68,8 @@ def ramses_header(hvals):
"gas_tracer": 0,
}

# TODO: this needs an actual solution
"""
if ytcfg.has_section("ramses-families"):
for key in particle_families.keys():
val = ytcfg.get("ramses-families", key, fallback=None)
Expand All @@ -77,3 +78,4 @@ def ramses_header(hvals):
"Changing family %s from %s to %s", key, particle_families[key], val
)
particle_families[key] = val
"""
3 changes: 2 additions & 1 deletion yt/frontends/ramses/field_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os
from typing import List

from yt.config import ytcfg
from yt.funcs import mylog
from yt.utilities.cython_fortran_utils import FortranFile

Expand Down Expand Up @@ -305,6 +304,8 @@ class HydroFieldFileHandler(FieldFileHandler):
@classmethod
def detect_fields(cls, ds):
# Try to get the detected fields
from yt.config import ytcfg

detected_fields = cls.get_detected_fields(ds)
if detected_fields:
return detected_fields
Expand Down
3 changes: 2 additions & 1 deletion yt/frontends/ramses/particle_handlers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import abc
import os

from yt.config import ytcfg
from yt.funcs import mylog
from yt.utilities.cython_fortran_utils import FortranFile

Expand Down Expand Up @@ -60,6 +59,8 @@ def __init_subclass__(cls, *args, **kwargs):
return cls

def __init__(self, domain):
from yt.config import ytcfg

self.setup_handler(domain)

# Attempt to read the list of fields from the config file
Expand Down
3 changes: 2 additions & 1 deletion yt/geometry/geometry_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import numpy as np

from yt.config import ytcfg
from yt.units.yt_array import YTArray, uconcatenate
from yt.utilities.exceptions import YTFieldNotFound
from yt.utilities.io_handler import io_registry
Expand Down Expand Up @@ -54,6 +53,8 @@ def _initialize_state_variables(self):
self.num_grids = None

def _initialize_data_storage(self):
from yt.config import ytcfg

if not ytcfg.get("yt", "serialize"):
return
fn = self.ds.storage_filename
Expand Down
3 changes: 2 additions & 1 deletion yt/geometry/grid_geometry_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import numpy as np

from yt.arraytypes import blankRecordArray
from yt.config import ytcfg
from yt.fields.derived_field import ValidateSpatial
from yt.fields.field_detector import FieldDetector
from yt.funcs import ensure_numpy_array, iter_fields
Expand Down Expand Up @@ -402,6 +401,8 @@ def _chunk_io(
preload_fields=None,
chunk_sizing="auto",
):
from yt.config import ytcfg

# local_only is only useful for inline datasets and requires
# implementation by subclasses.
if preload_fields is None:
Expand Down

0 comments on commit cfc81eb

Please sign in to comment.