Skip to content

New utility function to generate a polarities array from a dictionary.#343

Merged
thurinj merged 4 commits into
masterfrom
restore_sort_polarities
Mar 4, 2026
Merged

New utility function to generate a polarities array from a dictionary.#343
thurinj merged 4 commits into
masterfrom
restore_sort_polarities

Conversation

@thurinj
Copy link
Copy Markdown
Member

@thurinj thurinj commented Feb 23, 2026

This functions addresses a need to provide a sorted array for polarities values [-1,0,1] for each stations, from a polarity dictionary.

From the Waveforms+Polarities.py example:

    polarities_dict = {
        "BMR":   0,
        "DIV":  +1,
        "EYAK": +1,
        "PAX":  +1,
        ...
        "PERI": +1,
        "SOLD":  0,
        "TUPA": +1,
        }

    polarities = polarities_array_from_dict(polarities_dict, stations)

it replaces the following loop:

    polarities = np.zeros(len(stations))
    for _i, station in enumerate(stations):
        polarities[_i] = polarities_dict[station.station]

and adds warning prints if there is a discrepancy between the names in stations and polarities_dict.

Mostly straightforward PR, but I am happy to get any feedback if you think it is too redundant (@rmodrak, @ammcpherson, @SeismoFelix).

Planning to merge en Feb 26th.

@SeismoFelix pointed out that this function has been removed from the main branch.

This is a re-introduction with a very minimal change, to replace the .format() with a f string.
@rmodrak
Copy link
Copy Markdown
Member

rmodrak commented Feb 23, 2026

Could you please?
-rename the function something shorter, such as sort_polarities
-place the function in mtuq/misfit/polarity.py

@thurinj
Copy link
Copy Markdown
Member Author

thurinj commented Feb 24, 2026

Hi @rmodrak, thanks for the suggestions.
I went with a shorter name (you're right it was a tad bit too long), but slightly different from your suggestion, since we're not really "sorting" anything per-se (polarities_from_dict). And I moved the function to the polarity misfit.

Should be ready to merge if that's fine for you.

@thurinj thurinj merged commit 309067e into master Mar 4, 2026
1 check passed
@thurinj thurinj deleted the restore_sort_polarities branch April 21, 2026 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants