Code Monkey home page Code Monkey logo

Comments (8)

AlexandreDecan avatar AlexandreDecan commented on May 23, 2024

Hello,

Actually, you made use of an unsupported feature from python-intervals (I've just discovered how and why this works :-D). It's really an edge case that calling .get with an AtomicInterval behaves that way ;-) When I developed this method, I only thought about either passing Interval instances, or "single values", I've never thought about AtomicInterval. Since an AtomicInterval instance falls in the "single value" part of the if-else, it leads to if key in i: which turns out to be true because python-intervals defined containment for AtomicInterval as well.

In short, all this to say that I am surprised (positively) that it is possible to use AtomicIntervals as a key in python-intervals :-)
Obviously, as the AtomicInterval type no longer exists in portion this makes your situation more complicated :-)

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.

from portion.

AlexandreDecan avatar AlexandreDecan commented on May 23, 2024

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 None.
The reason why I got None is because d.domain() returns [0,4], and this interval is never contained in any key of the interval dict.

I believe you would like to have [[0,2], (2, 4]] so that you get ['a', 'b'] as a result, right?

from portion.

msarrel avatar msarrel commented on May 23, 2024

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.

from portion.

AlexandreDecan avatar AlexandreDecan commented on May 23, 2024

Actually, I don't think it's an issue per se, it provides the expected output since IntervalDict were not expected to work with AtomicInterval. Anyway, that "potential issue" is no longer valid in portion given there is no longer an AtomicInterval type :-)

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 portion. By default, sorted relies on < to sort elements of a list, but intervals are not comparable the usual way, as explained in the documentation. However, since the intervals stored in an IntervalDict are non-overlapping by definition, and since we will handle atomic intervals from these intervals, there is no issue to use sorted and <. This is not the case for overlapping intervals (e.g., [0,3] is neither < nor > than [2,4]), and not the case for non-atomic ones (e.g., [0,1] | [4,5] is neither < nor > than [2,3]). As a result, we can safely use sorted in our case.

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 d.domain(), since d.domain() creates an Interval instance that internally sorts intervals as well. Moreover, with the "new" solution, there is no more a need to lookup values from d (i.e. no more d.get(i), an operation that is not the cheapest one in an IntervalDict).

from portion.

msarrel avatar msarrel commented on May 23, 2024

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.

from portion.

AlexandreDecan avatar AlexandreDecan commented on May 23, 2024

IntervalDict.items() (and other view-based methods) already returns a sorted list of intervals, but these are not necessarily atomic. I already thought about adding a Boolean atomic parameter to the as_dict method of an IntervalDict, but I found the use case a bit too specific (and the "workaround" so simple) to take the plunge.

Moreover, now, I can even refer to this issue in the documentation :-D

from portion.

msarrel avatar msarrel commented on May 23, 2024

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.

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?

from portion.

AlexandreDecan avatar AlexandreDecan commented on May 23, 2024

Not for the moment, I'm not 100% convinced to be honest :-)
Maybe you can open another issue specifically for this, so we can discuss it?

from portion.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.