Skip to content
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

Chaining Interval Operations #33

Closed
geographika opened this issue Jul 26, 2020 · 4 comments
Closed

Chaining Interval Operations #33

geographika opened this issue Jul 26, 2020 · 4 comments
Labels
question Issue is a question

Comments

@geographika
Copy link

Very nice library - thanks!

I was wondering if you have a suggested method for chaining interval operations on lists of intervals?

For example for intersecting several lists you can use:

r = P.closed(100.1239812, 300.9012312) | P.closed(110, 300.9012312) | P.closed(97, 300.9012312)

If you have a list of unknown size is it possible to chain the intersection?

It would be nice if one of the approaches for chaining sets could be used as described here:

>> list(set.union(*map(set, a)))
[1, '44', '30', '42', '43', '40', '41', '34']
>>> from itertools import chain
>>> list(set(chain.from_iterable(d)))

So something like:

>>> from itertools import chain
>>> list(P.intersection(chain.from_iterable(d)))

Maybe this is already possible but I've not found a successful approach so far (beyond a simple loop and applying intersection one by one).

@AlexandreDecan AlexandreDecan added the question Issue is a question label Jul 26, 2020
@AlexandreDecan
Copy link
Owner

Hello,

I'm not 100% sure I understood the question, but if I'm right, what you're looking for is reduce from the functools module (see https://docs.python.org/3/library/functools.html#functools.reduce). The function will be P.Interval.intersection, the iterable will be the list of intervals, and the initializer will be the infinite interval (i.e. P.open(-P.inf, P.inf)).

For example:

>>> import portion as P
>>> m = [P.closed(0, 10), P.closed(2, 8), P.closed(1, 4)]
>>> from functools import reduce
>>> reduce(P.Interval.intersection, m, P.open(-P.inf, P.inf))
[2, 4]

Is it what you were looking for?

@geographika
Copy link
Author

@AlexandreDecan - yes exactly that. I was didn't realise P.Interval.intersection was a class method and hadn't thought of using P.open(-P.inf, P.inf) as the starting point.
Thanks!

@AlexandreDecan
Copy link
Owner

You're welcome ;-)

Note that P.Interval.intersection is not a class method but, thanks to Python, if a is an instance of class A, then a.f() is (nearly) equivalent to A.f(a), so we can use instance methods as class methods :-)

@geographika
Copy link
Author

Thanks for the further explanation.

For anyone who comes across this issue and needs to chain a union using P.empty() as follows allows a chained operation:

from functools import reduce
m = [P.closed(0, 10), P.closed(2, 8), P.closed(1, 4)]
reduce(P.Interval.union, m, P.empty())
>>> [0,10]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issue is a question
Projects
None yet
Development

No branches or pull requests

2 participants