def __init__(self, a=None, b=None, bins=50): """ a and b can be in either datetime or unix time """ self._a = UnixTime(a) self._b = UnixTime(b) self.bins = bins
def __init__(self, distribution_beginning, distribution_ending, bins=50, relation_formula=None): if not isinstance(distribution_beginning, rv_frozen): raise TypeError("'distribution_beginning' should be a scipy frozen distribution") if not isinstance(distribution_ending, rv_frozen): raise TypeError("'distribution_ending' should be a scipy frozen distribution") self._distribution_beginning = distribution_beginning self._distribution_ending = distribution_ending a, beginning = calculate_bounds_of_probability_distribution(distribution_beginning) ending, b = calculate_bounds_of_probability_distribution(distribution_ending) self._beginning = UnixTime(beginning) self._ending = UnixTime(ending) self.membership_function = MembershipFunction(self) bins_beginning = bins / 2 bins_ending = bins - bins_beginning self.interval_beginning = TimeInterval(a, beginning, bins_beginning) self.interval_ending = TimeInterval(ending, b, bins_ending) list.__init__(self, self.interval_beginning + self.interval_ending) TimeInterval.__init__(self, a, b, bins) if relation_formula is None: relation_formula = RelationFormulaGeometricMean() elif not isinstance(relation_formula, BaseRelationFormula): raise TypeError("'relation_formula' should be of type 'BaseRelationFormula'") relation_formula.bounds[distribution_beginning] = self.a, self.beginning relation_formula.bounds[distribution_ending] = self.ending, self.b self._formula_creator = FormulaCreator(relation_formula)
def __init__(self, distribution_beginning, distribution_ending, bins=50, distribution_integral_limit=DISTRIBUTION_INTEGRAL_LIMIT): if not isinstance(distribution_beginning, rv_frozen): raise TypeError( "'distribution_beginning' should be a scipy frozen distribution" ) if not isinstance(distribution_ending, rv_frozen): raise TypeError( "'distribution_ending' should be a scipy frozen distribution") if not 0 < distribution_integral_limit <= 1: raise TypeError( "'distribution_integral_limit' should be within (0, 1]") self._distribution_beginning = distribution_beginning self._distribution_ending = distribution_ending a, beginning = calculate_bounds_of_probability_distribution( distribution_beginning, distribution_integral_limit) ending, b = calculate_bounds_of_probability_distribution( distribution_ending, distribution_integral_limit) self._beginning = UnixTime(beginning) self._ending = UnixTime(ending) self.membership_function = MembershipFunction(self) bins_beginning = bins / 2 bins_ending = bins - bins_beginning self.interval_beginning = TimeInterval(a, beginning, bins_beginning) self.interval_ending = TimeInterval(ending, b, bins_ending) list.__init__(self, self.interval_beginning + self.interval_ending) TimeInterval.__init__(self, a, b, bins)
def plot(self): import matplotlib.pyplot as plt from spatiotemporal.unix_time import UnixTime plt.plot( [UnixTime(self.a).to_datetime(), UnixTime(self.b).to_datetime()], [1, 1]) return plt
def to_list(self): result = [] for i, time_step in enumerate(self): if i == len(self) - 1 and self.output_list[len(self) - 1] == 0: result.append(UnixTime(time_step - EPSILON)) result.append(time_step) if i == 0 and self.output_list[0] == 0: result.append(UnixTime(time_step + EPSILON)) return result
def plot(self): import matplotlib.pyplot as plt x_axis, y_axis = [], [] for time_step in self: x_axis.append(UnixTime(time_step - EPSILON).to_datetime()) x_axis.append(UnixTime(time_step + EPSILON).to_datetime()) y_axis.append(self.pdf(time_step - EPSILON)) y_axis.append(self.pdf(time_step + EPSILON)) plt.plot(x_axis, y_axis) return plt
def __init__(self, a, b, beginning=None, ending=None, beginning_factor=None, ending_factor=None): """ start and end can be in either datetime or unix time """ a, b = UnixTime(a), UnixTime(b) assert a < b, "'b' should be greater than 'a'" if (beginning, ending) != (None, None): assert (beginning_factor, ending_factor) == (None, None), "PiecewiseTemporalEvent() only accepts " \ "either 'beginning_factor' and 'ending_factor' " \ "or 'beginning' and 'ending'" if beginning_factor is not None: assert beginning_factor > 0 self.beginning_factor = beginning_factor if ending_factor is not None: assert ending_factor > 0 self.ending_factor = ending_factor if (beginning, ending) != (None, None): beginning = UnixTime(beginning) ending = UnixTime(ending) else: beginning, ending = 0, 0 while not a < beginning < ending < b: beginning = random_time( a, b, probability_distribution=t( # df, mean, variance 4, a + float(b - a) / self.beginning_factor, float(b - a) / self.beginning_factor)) ending = random_time( a, b, probability_distribution=t( # df, mean, variance 4, b - float(b - a) / self.ending_factor, float(b - a) / self.ending_factor)) TemporalEventPiecewiseLinear.__init__(self, { a: 0, beginning: 1 }, { ending: 1, b: 0 })
def random_time(self, start=None, stop=None, probability_distribution=None): if start is None: start = self.a else: start = UnixTime(start) assert self.a <= start <= self.b, "'start' should be within the interval of 'self'" if stop is None: stop = self.b else: stop = UnixTime(stop) assert self.a <= stop <= self.b, "'stop' should be within the interval of 'self'" return random_time(start, stop, probability_distribution)
def invoke_function_on(function, sequence_or_point): result = [] try: for point in sequence_or_point: if type(point) is datetime: point = UnixTime(point) result.append(function(point)) except: if type(sequence_or_point) is datetime: sequence_or_point = UnixTime(sequence_or_point) return function(sequence_or_point) return result
def __init__(self, a, b, beginning=None, ending=None, beginning_factor=None, ending_factor=None): """ start and end can be in either datetime or unix time """ a, b = UnixTime(a), UnixTime(b) assert a < b, "'b' should be greater than 'a'" if (beginning, ending) != (None, None): assert (beginning_factor, ending_factor) == (None, None), "PiecewiseTemporalEvent() only accepts " \ "either 'beginning_factor' and 'ending_factor' " \ "or 'beginning' and 'ending'" if not a < beginning < ending < b: raise AttributeError( "The inputs should satisfy 'a < beginning < ending < b' relation" ) if beginning_factor is not None: assert beginning_factor > 0 self.beginning_factor = beginning_factor if ending_factor is not None: assert ending_factor > 0 self.ending_factor = ending_factor if (beginning, ending) == (None, None): beginning, ending = 0, 0 while not a < beginning < ending < b: beginning = random_time( a, b, probability_distribution=t( # df, mean, variance 4, a + float(b - a) / self.beginning_factor, float(b - a) / self.beginning_factor)) ending = random_time( a, b, probability_distribution=t( # df, mean, variance 4, b - float(b - a) / self.ending_factor, float(b - a) / self.ending_factor)) TemporalEvent.__init__(self, uniform(loc=a, scale=UnixTime(beginning - a)), uniform(loc=ending, scale=UnixTime(b - ending)), bins=4)
def plot(self): import matplotlib.pyplot as plt from spatiotemporal.unix_time import UnixTime x_axis = [UnixTime(time).to_datetime() for time in self] plt.plot(x_axis, self.membership_function) return plt
def invoke_method_on(method, sequence_or_point): if method is None: return None if not callable(method): raise TypeError("'method' is not callable") result = [] try: for point in sequence_or_point: if type(point) is datetime: point = UnixTime(point) result.append(method(point)) except TypeError: if type(sequence_or_point) is datetime: sequence_or_point = UnixTime(sequence_or_point) return method(sequence_or_point) return result
def __init__(self, iterable): assert len(iterable) >= 2 iterable = sorted(iterable) list_converted_to_unix_time = [] first_item = True for current in iterable: current = UnixTime(current) list_converted_to_unix_time.append(current) if first_item: previous = current first_item = False continue assert current > previous previous = current list.__init__(self, list_converted_to_unix_time)
def temporal_relation_between(temporal_event_1, temporal_event_2): temporal_event_1 = TemporalEventTrapezium(1, 10) temporal_event_2 = TemporalEventTrapezium(11, 16) sum_times = sorted([ UnixTime(time).to_datetime() for time in set(temporal_event_1.to_list() + temporal_event_2.to_list()) ]) sum_certainties = [] for time_step in sum_times: sum_certainties.append( temporal_event_1.membership_function(time_step) + temporal_event_2.membership_function(time_step)) result = {} formulas = { 'p': beforeFormula, 'm': meetsFormula, 'o': overlapsFormula, 'F': finished_byFormula, 'D': containsFormula, 's': startsFormula, 'e': equalsFormula, 'S': started_byFormula, 'd': duringFormula, 'f': finishesFormula, 'O': overlapped_byFormula, 'M': met_byFormula, 'P': afterFormula } dist1, dist2 = temporal_event_1.to_dict(), temporal_event_2.to_dict() for name in formulas: degree = formulas[name](dist1, dist2) if degree > 0: result[name] = degree print result temporal_event_1.plot() plt = temporal_event_2.plot() plt.plot(sum_times, sum_certainties) plt.show() return result
def a(self, value): assert value > self[1] self[0] = UnixTime(value)
def b(self, value): assert value > self[-2] self[-1] = UnixTime(value)
def __getitem__(self, index): if index >= len(self): raise IndexError item = self.a + index * self._iter_step return UnixTime(item)
def to_datetime_list(self): if self._datetime is None: self._datetime = [UnixTime(time).to_datetime() for time in self] return self._datetime
def a(self, value): self._a = UnixTime(value)
def b(self, value): self._b = UnixTime(value)
def __getitem__(self, index): value = self.a + index * self.iter_step if value > self.b: raise IndexError return UnixTime(value)