-
Notifications
You must be signed in to change notification settings - Fork 0
Extract intervals_chain, intervals_tuple, intervals_distribution modules #104
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
Open
Maximus2012
wants to merge
2
commits into
main
Choose a base branch
from
intervals-chain
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import numpy as np | ||
| from numpy import ndarray | ||
|
|
||
| from foapy.core import mode as constants_mode | ||
|
|
||
|
|
||
| def intervals_chain(ar: ndarray, mode: int) -> ndarray: | ||
| """ | ||
| Build an intervals chain from a 1-D array. | ||
|
|
||
| An intervals chain is an n-tuple of natural numbers representing the distance | ||
| between equal elements in a sequence. This function encapsulates the core | ||
| chain-building logic: given a 1-D array [ar] (already oriented for the chosen | ||
| binding direction) and a [mode], it computes the raw intervals array before | ||
| any binding-specific reversal is applied. | ||
|
|
||
| The function supports four behavioural strategies at sequence boundaries: | ||
|
|
||
| * **normal / bounded** ([mode.normal]) – the leading boundary interval | ||
| (distance from the virtual start to the first occurrence) is included; | ||
| the trailing boundary interval is not added. | ||
| * **cyclic** ([mode.cycle]) – the leading and trailing boundary intervals | ||
| are summed into a single interval placed at the position of the first | ||
| occurrence, as if the sequence were circular. | ||
| * **lossy** ([mode.lossy]) – boundary (first-occurrence) intervals are set | ||
| to [0] so the caller can filter them out. | ||
| * **redundant** ([mode.redundant]) – same as [mode.normal] for the chain itself; | ||
| the caller is responsible for appending the trailing boundary intervals. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| ar : ndarray | ||
| 1-D array whose intervals chain is to be built. For [binding.end] | ||
| the caller must reverse [ar] **before** passing it here, and reverse | ||
| the result afterwards. | ||
| mode : int | ||
| One of [mode.lossy], [mode.normal], [mode.cycle], | ||
| [mode.redundant]. Controls how boundary intervals are handled. | ||
|
|
||
| Returns | ||
| ------- | ||
| chain : ndarray | ||
| Raw intervals array of the same length as [ar], in the original | ||
| element order. For [mode.lossy] the boundary zeros are still | ||
| present; filtering is left to the caller. | ||
|
|
||
| Notes | ||
| ----- | ||
| This function is the low-level building block used by | ||
| [foapy.intervals]. It does not validate [mode] or the shape | ||
| of [ar] – validation is the responsibility of the caller. | ||
|
|
||
| Examples | ||
| -------- | ||
| Build a bounded (normal) intervals chain: | ||
|
|
||
| >>> import numpy as np | ||
| >>> from foapy.core import intervals_chain | ||
| >>> from foapy.core import mode | ||
| >>> ar = np.asarray(['b', 'a', 'b', 'c', 'b']) | ||
| >>> intervals_chain(ar, mode.normal) | ||
| array([1, 2, 2, 4, 2]) | ||
|
|
||
| Build a cyclic intervals chain (leading boundary becomes wrap-around sum): | ||
|
|
||
| >>> intervals_chain(ar, mode.cycle) | ||
| array([1, 5, 2, 5, 2]) | ||
|
|
||
| For lossy mode the first-occurrence intervals are zeroed out so the | ||
| caller can filter them with [result][result != 0]: | ||
|
|
||
| >>> intervals_chain(ar, mode.lossy) | ||
| array([0, 0, 2, 0, 2]) | ||
| """ | ||
|
|
||
| perm = ar.argsort(kind="mergesort") | ||
|
|
||
| mask = np.empty(ar.shape[0] + 1, dtype=bool) | ||
| mask[:1] = True | ||
| mask[1:-1] = ar[perm[1:]] != ar[perm[:-1]] | ||
| mask[-1:] = True # or mask[-1:] = True | ||
|
|
||
| first_mask = mask[:-1] | ||
| last_mask = mask[1:] | ||
|
|
||
| chain = np.empty(ar.shape, dtype=np.intp) | ||
| chain[1:] = perm[1:] - perm[:-1] | ||
|
|
||
| delta = len(ar) - perm[last_mask] if mode == constants_mode.cycle else 1 | ||
| chain[first_mask] = perm[first_mask] + delta | ||
|
|
||
| if mode == constants_mode.lossy: | ||
| chain[first_mask] = 0 | ||
|
|
||
| inverse_perm = np.empty(ar.shape, dtype=np.intp) | ||
| inverse_perm[perm] = np.arange(ar.shape[0]) | ||
| chain = chain[inverse_perm] | ||
|
|
||
| return chain |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import numpy as np | ||
| from numpy import ndarray | ||
|
|
||
| from foapy.core import intervals_tuple as _intervals_tuple # noqa: F401 | ||
|
|
||
|
|
||
| def intervals_distribution(tuple_result: ndarray) -> ndarray: | ||
| """ | ||
| Calculate intervals distribution from an intervals tuple. | ||
|
|
||
| An intervals distribution is an n-tuple of natural numbers where the | ||
| index represents the interval length and the value is the count of | ||
| its appearances in the intervals tuple. | ||
|
|
||
| Caller is responsible for preparing the tuple_result correctly: | ||
| - For mode.lossy: boundary intervals already removed by intervals_tuple | ||
| - For mode.redundant: trailing intervals already appended by intervals_tuple | ||
| - For mode.normal, mode.cycle: pass tuple_result as-is | ||
|
|
||
| Parameters | ||
| ---------- | ||
| tuple_result : ndarray | ||
| Intervals tuple produced by intervals_tuple. | ||
|
|
||
| Returns | ||
| ------- | ||
| result : ndarray | ||
| Array of length max(tuple_result) where result[i] is the count | ||
| of interval value i+1 in the tuple. | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> import numpy as np | ||
| >>> from foapy import binding, mode | ||
| >>> from foapy.core import intervals_chain | ||
| >>> from foapy.core import intervals_tuple | ||
| >>> from foapy.core import intervals_distribution | ||
| >>> ar = np.asarray([2, 4, 2, 2, 4]) | ||
|
|
||
| From documentation example - chain [1, 2, 3, 2, 4, 6]: | ||
|
|
||
| >>> intervals_distribution(np.array([1, 2, 3, 2, 4, 6])) | ||
| array([1, 2, 1, 1, 0, 1]) | ||
|
|
||
| Normal mode: | ||
|
|
||
| >>> chain = intervals_chain(ar, mode.normal) | ||
| >>> tpl = intervals_tuple(ar, chain, mode.normal, binding.start) | ||
| >>> intervals_distribution(tpl) | ||
| array([2, 2, 1]) | ||
|
|
||
| Lossy mode — boundary intervals already removed: | ||
|
|
||
| >>> chain = intervals_chain(ar, mode.lossy) | ||
| >>> tpl = intervals_tuple(ar, chain, mode.lossy, binding.start) | ||
| >>> intervals_distribution(tpl) | ||
| array([1, 1, 1]) | ||
|
|
||
| Redundant mode — trailing intervals already appended: | ||
|
|
||
| >>> chain = intervals_chain(ar, mode.redundant) | ||
| >>> tpl = intervals_tuple(ar, chain, mode.redundant, binding.start) | ||
| >>> intervals_distribution(tpl) | ||
| array([2, 3, 1]) | ||
|
|
||
| Empty tuple: | ||
|
|
||
| >>> intervals_distribution(np.array([])) | ||
| array([]) | ||
| """ | ||
|
|
||
| if len(tuple_result) == 0: | ||
| return np.array([], dtype=np.intp) | ||
|
|
||
| max_interval = int(tuple_result.max()) | ||
| distribution = np.zeros(max_interval, dtype=np.intp) | ||
| for interval in tuple_result: | ||
| distribution[int(interval) - 1] += 1 | ||
|
|
||
| return distribution | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import numpy as np | ||
| from numpy import ndarray | ||
|
|
||
| from foapy.core import binding as constants_binding | ||
| from foapy.core import mode as constants_mode | ||
|
|
||
|
|
||
| def intervals_tuple(ar: ndarray, chain: ndarray, mode: int, binding: int) -> ndarray: | ||
| """ | ||
| Convert an intervals chain into a final intervals tuple. | ||
|
|
||
| Applies unchaining strategies to the raw intervals chain produced by | ||
| [intervals_chain], handling boundary intervals according to the given mode. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| ar : ndarray | ||
| 1-D array (already oriented for binding direction) used to compute | ||
| trailing boundary intervals for [mode.redundant]. | ||
| chain : ndarray | ||
| Raw intervals chain produced by [intervals_chain]. | ||
| mode : int | ||
| One of [mode.lossy], [mode.normal], [mode.cycle], [mode.redundant]. | ||
| binding : int | ||
| One of [binding.start], [binding.end]. Used to correctly order | ||
| trailing boundary intervals for [mode.redundant]. | ||
|
|
||
| Returns | ||
| ------- | ||
| result : ndarray | ||
| Final intervals tuple. | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> import numpy as np | ||
| >>> from foapy.core import mode, binding | ||
| >>> from foapy.core import intervals_chain | ||
| >>> from foapy.core import intervals_tuple | ||
| >>> ar = np.asarray(['b', 'a', 'b', 'c', 'b']) | ||
|
|
||
| Normal mode — chain is returned as-is: | ||
|
|
||
| >>> chain = intervals_chain(ar, mode.normal) | ||
| >>> intervals_tuple(ar, chain, mode.normal) | ||
| array([1, 2, 2, 4, 2]) | ||
|
|
||
| Lossy mode — boundary (zero) intervals are removed: | ||
|
|
||
| >>> chain = intervals_chain(ar, mode.lossy) | ||
| >>> intervals_tuple(ar, chain, mode.lossy) | ||
| array([2, 2]) | ||
|
|
||
| Redundant mode — trailing boundary intervals are appended: | ||
|
|
||
| >>> chain = intervals_chain(ar, mode.redundant) | ||
| >>> intervals_tuple(ar, chain, mode.redundant) | ||
| array([1, 2, 2, 4, 2, 4, 1, 2]) | ||
| """ | ||
|
|
||
| if mode == constants_mode.lossy: | ||
| return chain[chain != 0] | ||
|
|
||
| if mode == constants_mode.redundant: | ||
| perm = ar.argsort(kind="mergesort") | ||
| mask = np.empty(ar.shape[0] + 1, dtype=bool) | ||
| mask[:1] = True | ||
| mask[1:-1] = ar[perm[1:]] != ar[perm[:-1]] | ||
| mask[-1:] = True | ||
| last_mask = mask[1:] | ||
| trailing = len(ar) - perm[last_mask] | ||
| if binding == constants_binding.end: | ||
| trailing = trailing[::-1] | ||
| return np.concatenate((chain, trailing)) | ||
|
Comment on lines
+63
to
+73
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это вряд ли работает корректно. Нужно сделать тесты. |
||
|
|
||
| return chain | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not use loops. We should use numpy functions instead of loops