from ApplianceTrace import ApplianceTrace
from ApplianceInstance import ApplianceInstance
from ApplianceType import ApplianceType
from ApplianceSet import ApplianceSet
from pandas import concat

def concatenate_traces(traces, metadata=None, how="strict"):
    '''
    Given a list of appliance traces, returns a single concatenated
    trace. With how="strict" option, must be sampled at the same rate and
    consecutive, without overlapping datapoints.
    '''
    if not metadata:
        metadata = traces[0].metadata

    if how == "strict":
        # require ordered list of consecutive, similarly sampled traces with no
        # missing data.
        return ApplianceTrace(concat([t.series for t in traces],metadata))
    else:
        raise NotImplementedError

def aggregate_traces(traces, metadata, how="strict"):
    '''
    Given an array of temporally aligned traces, aggregate them into a single
    signal.
    '''
    if how == "strict":
        # require that traces are exactly aligned
        summed_series = traces[0].series
        for trace in traces[1:]:
            summed_series += trace.series
        return ApplianceTrace(summed_series,metadata)
    else:
        return NotImplementedError

@@ -841,15 +841,13 @@
 en a
-n array
+ list
  of 
@@ -1168,32 +1168,32 @@
 data)%0A    else:%0A
-
         return N
@@ -1191,28 +1191,521 @@
  return NotImplementedError%0A
+%0Adef aggregate_instances(instances,how=%22strict%22):%0A    '''%0A    Given a list of temporally aligned instances, aggregate them into a single%0A    signal.%0A    '''%0A    if how == %22strict%22:%0A        traces = %5Binstance.traces for instance in instances%5D%0A        traces = %5Blist(t) for t in zip(*traces)%5D # transpose%0A        traces = %5B aggregate_traces(t,%7B%7D) for t in traces%5D%0A        # TODO how to aggregate metadata?%0A        return ApplianceInstance(traces)%0A    else:%0A        return NotImplementedError%0A%0A%0A%0A

add instance aggregation