-
-
Notifications
You must be signed in to change notification settings - Fork 34
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
IntervalDict iteration over sorted atomic intervals #44
Comments
Hello, Actually, you made use of an unsupported feature from In short, all this to say that I am surprised (positively) that it is possible to use I will look to see if there is a functional generic workaround, or if it is possible/useful to implement something that could solve your problem. |
I've a question about the expected output of your snippet when two keys are adjacent. Consider for example the following interval dict: import intervals as portion
d = portion.IntervalDict()
d[portion.closed(0, 2)] = 'a'
d[portion.openclosed(2, 4)] = 'b' When I run your snippet, I got I believe you would like to have |
Yes, that is correct. I had planned to open a separate issue on that problem. Would you still like me to do that? I'd be happy to do so if it will help you. BTW, here is a description of my full use case. The keys in my interval dictionary are made of Arrow datetimes, and the values are floats. My goal is to produce a graph of the values over time using plotly. I figured it would be more efficient to draw one horizontal line segment per atomic interval, rather than sample the interval dictionary at regular time steps. I also have code to draw the vertical segments to join the horizontal segments. That is why I need to process the atomic intervals in sorted order. |
Actually, I don't think it's an issue per se, it provides the expected output since Considering your use case, I think the easiest way to achieve it is to (1) retrieve all intervals from the dict, (2) retrieve all atomic intervals from these intervals, (3) and to sort them. To sort intervals, it's easier to define a custom sorting function based on the lower bound (its value and whether it is open or closed) as we do internally in In code: items = [] # List of (key, value) where key is an atomic interval
for interval, value in d.items():
for atomic in interval:
items.append((atomic, value))
items.sort() Or more concisely, using a list comprehension: items = sorted([(atomic, value) for key, value in d.items() for atomic in key]) The explicit sorting of intervals (in O(n.logn)) does not make this less efficient than the "old" solution based on |
Yes, I already have code that does what you describe. I just thought it might be convenient to add a method to your library to perform that function. Or, perhaps an iterator to iterate over these intervals. |
Moreover, now, I can even refer to this issue in the documentation :-D |
Do you still plan to fix the original question, giving an option to return a value when the user submitted an range that has only one value? |
Not for the moment, I'm not 100% convinced to be honest :-) |
Move information from issue AlexandreDecan#44 into README
Move information from issue AlexandreDecan#44 into README
Hi,
Thank you for making all the improvements in your library in portion 2. However, one of your changes from python-intervals to portion broke my code.
In python-intervals, if I passed an Interval to the IntervalDict.get() method, that method would return the value if the Interval corresponded to a single value in the IntervalDict. In portion, now the get() method always returns a new IntervalDict, even if there is only one entry. I would like a way to have the old functionality as an option, while also keeping the new functionality.
The Python dict.get() method always returns the value, so I would maybe suggest that IntervalDict.get() follow that precedent, and throw an exception if there is more than value for the submitted Interval. Then, the new functionality could be provided by a new method, or by setting a flag to True in the existing IntervalDict.get() method.
Here is an example of my use case. My goal is to iterate over the values in the IntervalDict, one for single-valued atomic interval, in sorted order. I have a cheap work-around, but it fails for infinite intervals.
Here is the output I get when I run the above example.
The text was updated successfully, but these errors were encountered: