Skip to content

Commit

Permalink
Merged in discretise_bounding (pull request #7)
Browse files Browse the repository at this point in the history
discretisation bounding added
  • Loading branch information
dream committed Apr 27, 2015
2 parents d72f162 + 8fa1c86 commit f321829
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
20 changes: 17 additions & 3 deletions datection/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ class DurationRRule(object):
"""

def __init__(self, duration_rrule, apply_exclusion=True):
def __init__(
self,
duration_rrule,
apply_exclusion=True,
forced_lower_bound=None,
forced_upper_bound=None):
self.duration_rrule = duration_rrule
self.apply_exclusion = apply_exclusion
self.forced_lower_bound = forced_lower_bound
self.forced_upper_bound = forced_upper_bound

def __hash__(self):
data = {
Expand All @@ -56,8 +63,15 @@ def __iter__(self):
for dtime in self.date_producer:
yield dtime
else:
self.rrule._dtstart = datetime.combine(date.today(), DAY_START)
end_bound_date = date.today() + timedelta(days=365)
if self.forced_lower_bound:
start_bound_date = self.forced_lower_bound
else:
start_bound_date = date.today()
self.rrule._dtstart = datetime.combine(start_bound_date, DAY_START)
if self.forced_upper_bound:
end_bound_date = self.forced_upper_bound
else:
end_bound_date = date.today() + timedelta(days=365)
end_bound = datetime.combine(end_bound_date, DAY_END)
for dtime in self.date_producer:
if dtime < end_bound:
Expand Down
43 changes: 33 additions & 10 deletions datection/similarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,19 @@ def discretise_day_interval(start_datetime, end_datetime, minutes_interval=30):
return out


def discretise_schedule(schedule, grain_level="day", grain_quantity=1):
def discretise_schedule(
schedule,
grain_level="day",
grain_quantity=1,
forced_lower_bound=None,
forced_upper_bound=None):
"""Discretise the schedule in chunks of 30 minutes"""
sc_set = set()
for duration_rrule in schedule:
drr = DurationRRule(duration_rrule)
drr = DurationRRule(
duration_rrule,
forced_lower_bound=forced_lower_bound,
forced_upper_bound=forced_upper_bound)
for timepoint in drr:
if grain_level == "min":
discrete_interval = discretise_day_interval(
Expand All @@ -51,25 +59,40 @@ def discretise_schedule(schedule, grain_level="day", grain_quantity=1):
end_datetime=timepoint + timedelta(
minutes=drr.duration), minutes_interval=60*grain_quantity)
for d_timepoint in discrete_interval:
d_timepoint.replace(minute=0)
d_timepoint.replace(minute=0, second=0)
sc_set.add(d_timepoint)
elif grain_level == "day":
timepoint = timepoint.replace(hour=0,minute=0)
timepoint = timepoint.replace(hour=0,minute=0, second=0)
sc_set.add(timepoint)
elif grain_level == "month":
timepoint = timepoint.replace(day=1,hour=0,minute=0)
timepoint = timepoint.replace(day=1,hour=0,minute=0, second=0)
sc_set.add(timepoint)
elif grain_level == "year":
timepoint = timepoint.replace(month=1,day=1,hour=0,minute=0)
timepoint = timepoint.replace(
month=1,day=1,hour=0,minute=0, second=0)
sc_set.add(timepoint)
return sc_set


def similarity(schedule1, schedule2, grain_level="day", grain_quantity=1):
def similarity(
schedule1,
schedule2,
grain_level="day",
grain_quantity=1,
forced_lower_bound=None,
forced_upper_bound=None):
"""Returns the jaccard similarity distance bewteen the schedules"""
discrete_schedule1 = discretise_schedule(schedule1, grain_level=grain_level, grain_quantity=grain_quantity)
discrete_schedule2 = discretise_schedule(schedule2, grain_level=grain_level, grain_quantity=grain_quantity)
return jaccard_distance(discrete_schedule1, discrete_schedule2)

def discretise(sched):
""" use given default for similarity analysis """
return discretise_schedule(
sched,
grain_level=grain_level,
grain_quantity=grain_quantity,
forced_lower_bound=forced_lower_bound,
forced_upper_bound=forced_upper_bound)

return jaccard_distance(discretise(schedule1), discretise(schedule2))


def min_distance(drrules1, drrules2):
Expand Down

0 comments on commit f321829

Please sign in to comment.