Back to list
tatsuki-washimi

extend-gwpy

by tatsuki-washimi

gwpy expansions for experiments

0🍴 0📅 Jan 25, 2026

SKILL.md


name: extend_gwpy description: GWpy/Astropyクラスの安全な継承と拡張のためのガイドライン

Extend GWpy Class

This skill provides guidelines and patterns for safely inheriting from and extending GWpy/Astropy classes (TimeSeries, FrequencySeries, Spectrogram, etc.).

Common Pitfalls & Solutions

1. __new__ and Metadata

GWpy uses __new__ to preserve units and metadata. When subclassing, ensure metadata is propagated.

class MyTimeSeries(TimeSeries):
    def __new__(cls, data, *args, **kwargs):
        obj = super().__new__(cls, data, *args, **kwargs)
        return obj

2. Unit Preservation

Always ensure that mathematical operations preserve units.

4. Metadata Preservation in __array_finalize__

When using NumPy ufuncs (like +, *) or arithmetic operations, NumPy creates new instances and calls __array_finalize__(self, obj). obj is the original instance. You must copy your custom metadata from obj to preserve it.

def __array_finalize__(self, obj):
    super().__array_finalize__(obj)
    if obj is None:
        return
    
    # Preserve custom attributes
    for attr in ["_my_meta", "_domain_label"]:
        val = getattr(obj, attr, None)
        if val is not None:
            setattr(self, attr, val)

CRITICAL PITFALL: If __new__ sets default values (e.g., self.attr = "default"), then __array_finalize__ might see those defaults and decide not to copy from obj.

  • Solution: Initialize custom attributes to None in __new__ and handle default logic within __array_finalize__.
  • Reference: See gwexpy/types/array4d.py for how it handles _axis0_index. It defers setting the arange default until after it fails to find an index on obj.

Note: Be careful if the metadata depends on the axis names or shape, which might have changed (though ufuncs usually preserve these). Use the pattern in Array4D and FieldBase as a reference.

5. Type Preservation in Collections

When implementing batch methods in collection classes (like FieldList or FieldDict), use self.__class__ to ensure that subclasses return instances of their own type.

def map_all(self, func):
    return self.__class__([func(item) for item in self])

Score

Total Score

60/100

Based on repository quality metrics

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

+10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

0/5

Reviews

💬

Reviews coming soon