def annotation_counts(self, ts, layer, start, end, period, channels=None): """ Retrives annotation counts for a given ts, channel, start, end, and/or layer """ if channels is None: ch_list = [] #empty uses all channels else: ch_list = [self._get_id(x) for x in channels] if isinstance(start, datetime.datetime): start = usecs_since_epoch(start) if isinstance(end, datetime.datetime): end = usecs_since_epoch(end) period = parse_timedelta(period) params = { 'start': int(start), 'end': int(end), 'channelIds': ch_list, 'period': period, 'layer': layer.name } path = self._uri('/{ts_id}/annotations', ts_id=self._get_id(ts)) resp = self._get(path, params=params) return resp
def _send_contiguous(self, channel, data, period): """ Provided channel series values, send segments in chunks channel: Blackfynn TimeSeriesChannel data: Pandas Series period: Sample period of data """ for offset in np.arange(0, len(data), self.max_segment_size): # divide data into chunks chunk = data[offset:offset + self.max_segment_size] # make data segment seg = self._make_ingest_segment(start_time=chunk.index[0], channel_id=channel.id, period=period, values=chunk.values) # send data segment resp = self._send_segment(seg) if 'ResponseMetadata' not in resp: raise Exception("Incorrect response from Kinesis.") if resp['ResponseMetadata']['HTTPStatusCode'] != 200: raise Exception("Received non-200 response code from Kinesis.") # NOTE: this should be done by the streaming consumer (server), # but until then, we'll update channel start/end time start = usecs_since_epoch(data.index[0]) end = usecs_since_epoch(data.index[-1]) if end > channel.end: channel.end = end if start < channel.start or channel.start == 0: channel.start = start channel.update()
def query_annotations(self, ts, layer, start=None, end=None, channels=None, limit=100, offset=0): """ Retrieves timeseries annotations for a particular range on array of channels. """ ch_list = self.requested_channels(ts, channels) ts_start, ts_end = ts.limits() if start is None: start = ts_start elif isinstance(start, datetime.datetime): start = usecs_since_epoch(start) if end is None: end = ts_end elif isinstance(end, datetime.datetime): end = usecs_since_epoch(end) params = { 'start': int(start), 'end': int(end), 'channelIds': ch_list, 'layerName': layer.name, 'limit': limit, 'offset': offset } path = self._uri('/{ts_id}/layers/{layer_id}/annotations', ts_id = ts.id,layer_id = layer.id) resp = self._get(path, params=params) return [TimeSeriesAnnotation.from_dict(x, api=self.session) for x in resp['annotations']['results']]
def query_annotation_counts(self, ts, layers, start, end, period, channels=None, merge_periods=False): """ Retrieves annotation counts for a given ts, channel, start, end, and/or layer Args: ts (TimeSeries) : The timeseries package for which to count annotations layers ([TimeSeriesLayer]) : List of layers for which to count annotations start (datetime or microseconds) : The starting time of the range to query end (datetime or microseconds) : The ending time of the the range to query period (string) : The length of time to group the counts. Formatted as a string - e.g. '1s', '5m', '3h' channels ([TimeSeriesChannel]) : List of channel (if omitted, all channels will be used) merge_periods(Boolean) : If true, merge consecutive result periods together to reduce the size of the resulting payload Returns: A dict layer_id -> list of counts for each period """ if channels is None: ch_list = [] # empty uses all channels else: ch_list = [self._get_id(x) for x in channels] if isinstance(start, datetime.datetime): start = usecs_since_epoch(start) if isinstance(end, datetime.datetime): end = usecs_since_epoch(end) period = parse_timedelta(period) params = { 'aggregation': 'count', 'start': long(start), 'end': long(end), 'period': long(period), 'mergePeriods': merge_periods, 'layerIds': [l.id for l in layers], 'channelIds': ch_list } path = self._uri('/{ts_id}/annotations/window', ts_id=self._get_id(ts)) resp = self._get(path, params=params) return resp
def query_annotation_counts(self, ts, layers, start, end, period, channels=None, merge_periods=False): """ Retrieves annotation counts for a given ts, channel, start, end, and/or layer Args: ts (TimeSeries) : The timeseries package for which to count annotations layers ([TimeSeriesLayer]) : List of layers for which to count annotations start (datetime or microseconds) : The starting time of the range to query end (datetime or microseconds) : The ending time of the the range to query period (string) : The length of time to group the counts. Formatted as a string - e.g. '1s', '5m', '3h' channels ([TimeSeriesChannel]) : List of channel (if omitted, all channels will be used) merge_periods(Boolean) : If true, merge consecutive result periods together to reduce the size of the resulting payload Returns: A dict layer_id -> list of counts for each period """ ch_list = self.requested_channels(ts, channels) if isinstance(start, datetime.datetime): start = usecs_since_epoch(start) if isinstance(end, datetime.datetime): end = usecs_since_epoch(end) period = parse_timedelta(period) params = { "aggregation": "count", "start": int(start), "end": int(end), "period": int(period), "mergePeriods": merge_periods, "layerIds": [l.id for l in layers], "channelIds": ch_list, } path = self._uri("/{ts_id}/annotations/window", ts_id=self._get_id(ts)) resp = self._get(path, params=params) return resp
def _make_ingest_segment(self, start_time, channel_id, period, values): segment = IngestSegment() segment.startTime = usecs_since_epoch(start_time) segment.channelId = channel_id segment.samplePeriod = period segment.data.extend(values) return segment
def _annotation_query_params(ts, start, end, period, layer, channels): # parse channel input channels = self._channel_list(ts, channels) # channel IDs ch_ids = [x.id for x in channels] params = { 'start': usecs_since_epoch(start), 'end': usecs_since_epoch(end), 'channels': ch_ids, 'period': period } if layer is not None: params['layer'] = layer return params
def _annotation_query_params(ts, start, end, period, layer, channels): # parse channel input channels = self._channel_list(ts, channels) # channel IDs ch_ids = [x.id for x in channels] params = { "start": usecs_since_epoch(start), "end": usecs_since_epoch(end), "channels": ch_ids, "period": period, } if layer is not None: params["layer"] = layer return params