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

API: separation of fields and mappers #86

Merged
merged 8 commits into from
Dec 21, 2023
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
1 change: 1 addition & 0 deletions .commitlint.rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
"covariance",
"io",
"fields",
"maps",
"plot",
"progress",
"twopoint",
Expand Down
167 changes: 98 additions & 69 deletions examples/example.ipynb

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions heracles/catalog/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ def __getitem__(self, where):
"""create a view with the given selection"""
...

@property
def metadata(self):
"""return a dictionary of metadata for the catalogue"""
...

@property
def label(self):
"""return a human-friendly identifier for the source of data"""
Expand Down Expand Up @@ -200,6 +205,11 @@ def base(self):
"""base catalogue of this view"""
return self._catalog

@property
def metadata(self):
"""return metadata for this view"""
return self._catalog.metadata

@property
def label(self):
"""human-friendly label of the catalogue (not settable in view)"""
Expand Down Expand Up @@ -325,6 +335,15 @@ def base(self):
"""returns ``None`` since this is not a view of another catalogue"""
return

@property
def metadata(self):
"""return the metadata for this catalogue"""
return MappingProxyType(
{
"catalog": self._label,
},
)

@property
def label(self):
"""optional human-friendly label for catalogue"""
Expand Down
21 changes: 20 additions & 1 deletion heracles/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ def toc_filter(obj, include=None, exclude=None):
raise TypeError(msg)


def toc_nearest(obj, key):
"""return the closest match to *key* in *obj*."""
if isinstance(key, Sequence):
t = tuple(key)
else:
t = (key,)
while t:
if t in obj:
return obj[t]
if len(t) == 1 and t[0] in obj:
return obj[t[0]]
t = t[:-1]
if t in obj:
return obj[t]
raise KeyError(key)


# subclassing UserDict here since that returns the correct type from methods
# such as __copy__(), __or__(), etc.
class TocDict(UserDict):
Expand Down Expand Up @@ -90,11 +107,13 @@ def __getitem__(self, pattern):
return found


def update_metadata(array, **metadata):
def update_metadata(array, *sources, **metadata):
"""update metadata of an array dtype"""
md = {}
if array.dtype.metadata is not None:
md.update(array.dtype.metadata)
for source in sources:
md.update(source.metadata)
md.update(metadata)
# create the new dtype with only the new metadata
dt = array.dtype
Expand Down
Loading
Loading