Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add dilation/erosion and morphological operation (open - close) #103

Closed
Labels
discussionLet's discuss about it!enhancementNew feature or requestquestionIssue is a question
@gjeanner

Description

@gjeanner

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionLet's discuss about it!enhancementNew feature or requestquestionIssue is a question

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp