Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork37
Closed
Description
Je propose d’ajouter des opérations de dilatation/érosion et de morphologie simple (ouverture/fermeture) pour les objets portion.Interval. L’objectif est de permettre des extensions ou réductions d’intervalles directement via apply tout en restant cohérent avec l’esprit de portion.
Motivation :
Actuellement, il n’existe pas d’API native pour décaler facilement les bornes d’un intervalle de manière abstraite (temps, distance, etc.). L’implémentation proposée s’appuie sur apply, ce qui reste simple et performant.
Points clés :
- L’érosion est obtenue via dilate_intervals avec magnitude < 0.
- Ouverture/fermeture morphologique = deux dilatations successives.
- Conserve la simplicité et la flexibilité de portion tout en ajoutant des fonctionnalités utiles.
Questions / discussion :
- Serait-il pertinent d’ajouter ces méthodes directement à l’API Interval ?
-Devrait-on prévoir des tests unitaires spécifiques pour ces nouvelles opérations ?
importportionasPfromportionimportInterval,closedfromdatetimeimporttimedelta##### Dilatation (et érosion via magnitude négative) #####def_shift_interval(interval:Interval,magnitude:float,bound:str):"""Décale un intervalle selon la magnitude et le côté choisi. Parameters ---------- interval : Interval Intervalle à décaler. magnitude : float Quantité de décalage (abstraite, e.g., temps, distance...). bound : str Côté à décaler : 'both', 'low', ou 'high'. Returns ------- Interval L'intervalle décalé. """ifbound=="both":returnclosed(interval.lower-magnitude,interval.upper+magnitude)elifbound=="low":returnclosed(interval.lower-magnitude,interval.upper)elifbound=="high":returnclosed(interval.lower,interval.upper+magnitude)else:raiseValueError(f"Invalid value for 'bound':{bound!r}. Expected 'both', 'low', or 'high'.")defdilate_intervals(intervals:Interval,magnitude:int|float,bound:str="both")->Interval:"""Applique une dilatation (ou érosion si magnitude négative) à un ensemble d'intervalles. Parameters ---------- intervals : Interval Collection d'intervalles à modifier. magnitude : float Quantité de dilatation (positive) ou d'érosion (négative). bound : str, optional Côté à modifier : 'both', 'low', ou 'high'. Par défaut 'both'. Returns ------- Interval Les intervalles modifiés. """ifnotisinstance(magnitude, (int,float,timedelta)):raiseTypeError(f"'magnitude' must be numeric, not{type(magnitude).__name__}.")returnintervals.apply(lambdax:_shift_interval(x,magnitude,bound))##### Opérations morphologiques simplifiées #####defopen_intervals(interval:Interval,magnitude:int|float)->Interval:"""Ouverture morphologique : érosion puis dilatation."""# open = dilate(dilate(interval, -magnitude), +magnitude)result=dilate_intervals(dilate_intervals(interval,-magnitude),+magnitude)valid_intervals=P.empty()forinterinresult:ifinter.lower<inter.upper:valid_intervals|=P.closed(inter.lower,inter.upper)returnvalid_intervalsdefclose_intervals(interval:Interval,magnitude:int|float)->Interval:"""Fermeture morphologique : dilatation puis érosion."""# close = dilate(dilate(interval, +magnitude), -magnitude)result=dilate_intervals(dilate_intervals(interval,+magnitude),-magnitude)valid_intervals=P.empty()forinterinresult:ifinter.lower<inter.upper:valid_intervals|=P.closed(inter.lower,inter.upper)returnvalid_intervals