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

Feature/base ref set #517

Closed
wants to merge 6 commits into from
Closed
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
4 changes: 2 additions & 2 deletions podpac/core/algorithm/reprojection.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ def _source_eval(self, coordinates, selector, output=None):
else:
return self.source.eval(coords, output=output, _selector=selector)

@property
def base_ref(self):
@tl.default('base_ref')
def _default_base_ref(self):
return "{}_reprojected".format(self.source.base_ref)
8 changes: 4 additions & 4 deletions podpac/core/algorithm/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,8 +919,8 @@ def _eval(self, coordinates, output=None, _selector=None):

return output

@property
def base_ref(self):
@tl.default('base_ref')
def _default_base_ref(self):
"""
Default node reference/name in node definitions

Expand Down Expand Up @@ -1011,8 +1011,8 @@ def _eval(self, coordinates, output=None, _selector=None):

return output

@property
def base_ref(self):
@tl.default('base_ref')
def _default_base_ref(self):
"""
Default node reference/name in node definitions

Expand Down
4 changes: 2 additions & 2 deletions podpac/core/data/reprojection.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ def get_data(self, coordinates, coordinates_index):
coordinates.drop(drop_dims)
return data

@property
def base_ref(self):
@tl.default('base_ref')
def _default_base_ref(self):
return "{}_reprojected".format(self.source.base_ref)
49 changes: 39 additions & 10 deletions podpac/core/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ class Node(tl.HasTraits):
cache_output = tl.Bool()
force_eval = tl.Bool(False)
cache_ctrl = tl.Instance(CacheCtrl, allow_none=True)

base_ref = tl.Unicode()

# list of attribute names, used by __repr__ and __str__ to display minimal info about the node
# e.g. data sources use ['source']
Expand Down Expand Up @@ -187,6 +189,7 @@ def _cache_ctrl_default(self):

def __init__(self, **kwargs):
"""Do not overwrite me"""


# Shortcut for users to make setting the cache_ctrl simpler:
if "cache_ctrl" in kwargs and isinstance(kwargs["cache_ctrl"], list):
Expand Down Expand Up @@ -467,8 +470,8 @@ def probe(self, lat=None, lon=None, time=None, alt=None, crs=None):
# Serialization
# -----------------------------------------------------------------------------------------------------------------

@property
def base_ref(self):
@tl.default('base_ref')
def _default_base_ref(self):
"""
Default reference/name in node definitions

Expand All @@ -477,7 +480,7 @@ def base_ref(self):
str
Name of the node in node definitions
"""
return self.__class__.__name__
return self.__class__.__name__

@property
def _base_definition(self):
Expand Down Expand Up @@ -531,6 +534,8 @@ def _base_definition(self):
if inputs:
d["inputs"] = inputs

if not type(self.style) is Style and isinstance(self.style, Style):
d["style_class"] = self.style.__class__.__module__ + "." + self.style.__class__.__name__
# style
if self.style.definition:
d["style"] = self.style.definition
Expand Down Expand Up @@ -643,6 +648,8 @@ def hash(self):
for k in d:
if "style" in d[k]:
del d[k]["style"]
if "style_class" in d[k]:
del d[k]["style_class"]

s = json.dumps(d, separators=(",", ":"), cls=JSONEncoder)
return hash_alg(s.encode("utf-8")).hexdigest()
Expand Down Expand Up @@ -1301,7 +1308,10 @@ def _process_kwargs(name, d, definition, nodes):

kwargs = {}
for k, v in d.get("attrs", {}).items():
kwargs[k] = v
if isinstance(getattr(node_class, k), tl.TraitType) and hasattr(getattr(node_class, k), "klass") and isinstance(v, OrderedDict) and getattr(node_class, k).klass == Coordinates:
kwargs[k] = Coordinates.from_definition(v)
else:
kwargs[k] = v

for k, v in d.get("inputs", {}).items():
kwargs[k] = _lookup_input(nodes, name, v, definition)
Expand All @@ -1310,7 +1320,24 @@ def _process_kwargs(name, d, definition, nodes):
kwargs[k] = _lookup_attr(nodes, name, v)

if "style" in d:
style_class = getattr(node_class, "style", Style)
if "style_class" in d:
style_root = module_root
# style_string = "%s.%s" % (style_root, d["style_class"])
style_string = d["style_class"]
module_style_name, style_name = style_string.rsplit(".", 1)

try:
style_module = importlib.import_module(module_style_name)
except ImportError:
raise ValueError("Invalid definition for style module '%s': no module found '%s'" % (name, module_style_name))
try:
style_class = getattr(style_module, style_name)
except AttributeError:
raise ValueError(
"Invalid definition for style '%s': style class '%s' not found in style module '%s'" % (name, style_name, module_style_name)
)
else:
style_class = getattr(node_class, "style", Style)
if isinstance(style_class, tl.TraitType):
# Now we actually have to look through the class to see
# if there is a custom initializer for style
Expand All @@ -1321,24 +1348,26 @@ def _process_kwargs(name, d, definition, nodes):
try:
style_class = atr(node_class)
except Exception as e:
# print ("couldn't make style from class", e)
try:
style_class = atr(node_class())
except:
# print ("couldn't make style from class instance", e)
style_class = style_class.klass
try:
kwargs["style"] = style_class.from_definition(d["style"])
except Exception as e:
kwargs["style"] = Style.from_definition(d["style"])
# print ("couldn't make style from inferred style class", e)


for k in d:
if k not in ["node", "inputs", "attrs", "lookup_attrs", "plugin", "style"]:
if k not in ["node", "inputs", "attrs", "lookup_attrs", "plugin", "style", "style_class"]:
raise ValueError("Invalid definition for node '%s': unexpected property '%s'" % (name, k))

nodes[name] = node_class(**kwargs)
for k in kwargs.keys():
if not (hasattr(node_class, k) and isinstance(getattr(node_class, k), tl.TraitType)):
logging.warn("Node definition has key '{}' that will not be set at node creation: attribute is not of type tl.TraitType".format(k))

nodes[name] = node_class(**kwargs)


# --------------------------------------------------------#
# Mixins
Expand Down