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

Get number of times multiple intervals overlap #70

Closed
jvinolas opened this issue Mar 28, 2022 · 3 comments
Closed

Get number of times multiple intervals overlap #70

jvinolas opened this issue Mar 28, 2022 · 3 comments
Labels
question Issue is a question

Comments

@jvinolas
Copy link

jvinolas commented Mar 28, 2022

Is there any way with your library using IntervalDict to get atomic intervals while getting as value the number of times it was overlapped with other intervals in list?

I've done this simple script that gets atomic intervals but I don't know how to get as value the units field in dict multiplied by the number of times the new atomic interval is overlapped with original intervals list.

plans = [{
    "start": "2022-03-29T00:00:00+0000" ,
    "end":   "2022-03-29T05:59:59+0000" ,
    "units": 10 ,
},{
    "start": "2022-03-29T01:00:00+0000" ,
    "end":   "2022-03-29T04:59:59+0000" ,
    "units": 10 ,
},{
    "start": "2022-03-29T02:00:00+0000" ,
    "end":   "2022-03-29T03:59:59+0000" ,
    "units": 10 ,
},{
    "start": "2022-03-29T02:29:59+0000" ,
    "end":   "2022-03-29T03:29:59+0000" ,
    "units": 10 ,
},{
    "start": "2022-03-29T06:29:59+0000" ,
    "end":   "2022-03-29T07:29:59+0000" ,
    "units": 10 ,
}]

def get_plans_intersected(plans):
    d = P.IntervalDict()
    i=1
    for plan in plans:
        d[P.closed(plan["start"], plan["end"])] = i*plan["units"]
        i+=1

    items=[]
    for interval, value in d.items():
        for atomic in interval: 
            items.append((atomic, value))
    items.sort()
    return [{"start":P.to_data(item[0])[0][1],"end":P.to_data(item[0])[0][2],"units":item[1]} for item in items]

pprint(get_plans_intersected(plans))

And this is the output:

[{'end': '2022-03-29T01:00:00+0000',
  'start': '2022-03-29T00:00:00+0000',
  'units': 10},
 {'end': '2022-03-29T02:00:00+0000',
  'start': '2022-03-29T01:00:00+0000',
  'units': 20},
 {'end': '2022-03-29T02:29:59+0000',
  'start': '2022-03-29T02:00:00+0000',
  'units': 30},
 {'end': '2022-03-29T03:29:59+0000',
  'start': '2022-03-29T02:29:59+0000',
  'units': 40},
 {'end': '2022-03-29T03:59:59+0000',
  'start': '2022-03-29T03:29:59+0000',
  'units': 30},
 {'end': '2022-03-29T04:59:59+0000',
  'start': '2022-03-29T03:59:59+0000',
  'units': 20},
 {'end': '2022-03-29T05:59:59+0000',
  'start': '2022-03-29T04:59:59+0000',
  'units': 10},
 {'end': '2022-03-29T07:29:59+0000',
  'start': '2022-03-29T06:29:59+0000',
  'units': 50}]

The result units values are just a coincidence of the original plans list data ordering as for example the non overlapping interval gets 50 units.

@AlexandreDecan AlexandreDecan added the question Issue is a question label Mar 29, 2022
@AlexandreDecan
Copy link
Owner

Hi,

I'm not sure I understood exactly what you would like to have at the end, but have a look at IntervalDict.combine. E.g.:

output = P.IntervalDict()
for interval in plans:
    i = P.closed(interval['start'], interval['end'])
    d = P.IntervalDict({i: 1})
    output = output.combine(d, how=lambda x,y: x+y)

Given all the things that happen in combine, do not expect it to work fast on large list of intervals, though.

@jvinolas
Copy link
Author

It seems now I'm getting the number of times the atomic interval was overlapped. I understand now the combine should be done between each two intervals to decide what value should I get as a result. That is really powerful.

Thanks a lot for this great library for handling intervals in python!

@AlexandreDecan
Copy link
Owner

You're welcome :-)

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