id
int32
0
252k
repo
stringlengths
7
55
path
stringlengths
4
127
func_name
stringlengths
1
88
original_string
stringlengths
75
19.8k
language
stringclasses
1 value
code
stringlengths
51
19.8k
code_tokens
sequence
docstring
stringlengths
3
17.3k
docstring_tokens
sequence
sha
stringlengths
40
40
url
stringlengths
87
242
3,900
ruipgil/TrackToTrip
tracktotrip/smooth.py
with_inverse
def with_inverse(points, noise): """ Smooths a set of points It smooths them twice, once in given order, another one in the reverse order. The the first half of the results will be taken from the reverse order and the second half from the normal order. Args: points (:obj:`list` of :obj:`Point`) noise (float): Expected noise, the higher it is the more the path will be smoothed. Returns: :obj:`list` of :obj:`Point` """ # noise_sample = 20 n_points = len(points)/2 break_point = n_points points_part = copy.deepcopy(points) points_part = list(reversed(points_part)) part = kalman_filter(points_part, noise) total = kalman_filter(points, noise) result = list(reversed(part))[:break_point] + total[break_point:] result[break_point] = point_mean(part[break_point], total[break_point]) return result
python
def with_inverse(points, noise): # noise_sample = 20 n_points = len(points)/2 break_point = n_points points_part = copy.deepcopy(points) points_part = list(reversed(points_part)) part = kalman_filter(points_part, noise) total = kalman_filter(points, noise) result = list(reversed(part))[:break_point] + total[break_point:] result[break_point] = point_mean(part[break_point], total[break_point]) return result
[ "def", "with_inverse", "(", "points", ",", "noise", ")", ":", "# noise_sample = 20", "n_points", "=", "len", "(", "points", ")", "/", "2", "break_point", "=", "n_points", "points_part", "=", "copy", ".", "deepcopy", "(", "points", ")", "points_part", "=", "list", "(", "reversed", "(", "points_part", ")", ")", "part", "=", "kalman_filter", "(", "points_part", ",", "noise", ")", "total", "=", "kalman_filter", "(", "points", ",", "noise", ")", "result", "=", "list", "(", "reversed", "(", "part", ")", ")", "[", ":", "break_point", "]", "+", "total", "[", "break_point", ":", "]", "result", "[", "break_point", "]", "=", "point_mean", "(", "part", "[", "break_point", "]", ",", "total", "[", "break_point", "]", ")", "return", "result" ]
Smooths a set of points It smooths them twice, once in given order, another one in the reverse order. The the first half of the results will be taken from the reverse order and the second half from the normal order. Args: points (:obj:`list` of :obj:`Point`) noise (float): Expected noise, the higher it is the more the path will be smoothed. Returns: :obj:`list` of :obj:`Point`
[ "Smooths", "a", "set", "of", "points" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/smooth.py#L76-L102
3,901
ruipgil/TrackToTrip
tracktotrip/spatiotemporal_segmentation.py
temporal_segmentation
def temporal_segmentation(segments, min_time): """ Segments based on time distant points Args: segments (:obj:`list` of :obj:`list` of :obj:`Point`): segment points min_time (int): minimum required time for segmentation """ final_segments = [] for segment in segments: final_segments.append([]) for point in segment: if point.dt > min_time: final_segments.append([]) final_segments[-1].append(point) return final_segments
python
def temporal_segmentation(segments, min_time): final_segments = [] for segment in segments: final_segments.append([]) for point in segment: if point.dt > min_time: final_segments.append([]) final_segments[-1].append(point) return final_segments
[ "def", "temporal_segmentation", "(", "segments", ",", "min_time", ")", ":", "final_segments", "=", "[", "]", "for", "segment", "in", "segments", ":", "final_segments", ".", "append", "(", "[", "]", ")", "for", "point", "in", "segment", ":", "if", "point", ".", "dt", ">", "min_time", ":", "final_segments", ".", "append", "(", "[", "]", ")", "final_segments", "[", "-", "1", "]", ".", "append", "(", "point", ")", "return", "final_segments" ]
Segments based on time distant points Args: segments (:obj:`list` of :obj:`list` of :obj:`Point`): segment points min_time (int): minimum required time for segmentation
[ "Segments", "based", "on", "time", "distant", "points" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/spatiotemporal_segmentation.py#L8-L23
3,902
ruipgil/TrackToTrip
tracktotrip/spatiotemporal_segmentation.py
correct_segmentation
def correct_segmentation(segments, clusters, min_time): """ Corrects the predicted segmentation This process prevents over segmentation Args: segments (:obj:`list` of :obj:`list` of :obj:`Point`): segments to correct min_time (int): minimum required time for segmentation """ # segments = [points for points in segments if len(points) > 1] result_segments = [] prev_segment = None for i, segment in enumerate(segments): if len(segment) >= 1: continue cluster = clusters[i] if prev_segment is None: prev_segment = segment else: cluster_dt = 0 if len(cluster) > 0: cluster_dt = abs(cluster[0].time_difference(cluster[-1])) if cluster_dt <= min_time: prev_segment.extend(segment) else: prev_segment.append(segment[0]) result_segments.append(prev_segment) prev_segment = segment if prev_segment is not None: result_segments.append(prev_segment) return result_segments
python
def correct_segmentation(segments, clusters, min_time): # segments = [points for points in segments if len(points) > 1] result_segments = [] prev_segment = None for i, segment in enumerate(segments): if len(segment) >= 1: continue cluster = clusters[i] if prev_segment is None: prev_segment = segment else: cluster_dt = 0 if len(cluster) > 0: cluster_dt = abs(cluster[0].time_difference(cluster[-1])) if cluster_dt <= min_time: prev_segment.extend(segment) else: prev_segment.append(segment[0]) result_segments.append(prev_segment) prev_segment = segment if prev_segment is not None: result_segments.append(prev_segment) return result_segments
[ "def", "correct_segmentation", "(", "segments", ",", "clusters", ",", "min_time", ")", ":", "# segments = [points for points in segments if len(points) > 1]", "result_segments", "=", "[", "]", "prev_segment", "=", "None", "for", "i", ",", "segment", "in", "enumerate", "(", "segments", ")", ":", "if", "len", "(", "segment", ")", ">=", "1", ":", "continue", "cluster", "=", "clusters", "[", "i", "]", "if", "prev_segment", "is", "None", ":", "prev_segment", "=", "segment", "else", ":", "cluster_dt", "=", "0", "if", "len", "(", "cluster", ")", ">", "0", ":", "cluster_dt", "=", "abs", "(", "cluster", "[", "0", "]", ".", "time_difference", "(", "cluster", "[", "-", "1", "]", ")", ")", "if", "cluster_dt", "<=", "min_time", ":", "prev_segment", ".", "extend", "(", "segment", ")", "else", ":", "prev_segment", ".", "append", "(", "segment", "[", "0", "]", ")", "result_segments", ".", "append", "(", "prev_segment", ")", "prev_segment", "=", "segment", "if", "prev_segment", "is", "not", "None", ":", "result_segments", ".", "append", "(", "prev_segment", ")", "return", "result_segments" ]
Corrects the predicted segmentation This process prevents over segmentation Args: segments (:obj:`list` of :obj:`list` of :obj:`Point`): segments to correct min_time (int): minimum required time for segmentation
[ "Corrects", "the", "predicted", "segmentation" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/spatiotemporal_segmentation.py#L25-L59
3,903
ruipgil/TrackToTrip
tracktotrip/spatiotemporal_segmentation.py
spatiotemporal_segmentation
def spatiotemporal_segmentation(points, eps, min_time): """ Splits a set of points into multiple sets of points based on spatio-temporal stays DBSCAN is used to predict possible segmentations, furthermore we check to see if each clusters is big enough in time (>=min_time). If that's the case than the segmentation is considered valid. When segmenting, the last point of the ith segment will be the same of the (i-1)th segment. Segments are identified through clusters. The last point of a clusters, that comes after a sub-segment A, will be present on the sub-segment A. Args: points (:obj:`list` of :obj:`Point`): segment's points eps (float): Epsilon to feed to the DBSCAN algorithm. Maximum distance between two samples, to be considered in the same cluster. min_time (float): Minimum time of a stay Returns: :obj:`list` of :obj:`list` of :obj:`Point`: Initial set of points in different segments """ # min time / sample rate dt_average = np.median([point.dt for point in points]) min_samples = min_time / dt_average data = [point.gen3arr() for point in points] data = StandardScaler().fit_transform(data) print 'min_samples: %f' % min_samples db_cluster = DBSCAN(eps=eps, min_samples=min_samples).fit(data) labels = db_cluster.labels_ n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0) segments = [[] for _ in range(n_clusters_+1)] clusters = [[] for _ in range(n_clusters_+1)] current_segment = 0 print 'clusters' print n_clusters_ if n_clusters_ == 1: segments = temporal_segmentation([points], min_time) return [segment for segment in segments if len(segment) > 1] # split segments identified with dbscan for i, label in enumerate(labels): if label != -1 and label + 1 != current_segment: current_segment = label + 1 point = points[i] if label == -1: segments[current_segment].append(point) else: clusters[label + 1].append(point) if len(segments) == 0 or sum([len(s) for s in segments]): segments = [points] segments = temporal_segmentation(segments, min_time) # segments = temporal_segmentation(correct_segmentation(segments, clusters, min_time), min_time) return [segment for segment in segments if len(segment) > 1]
python
def spatiotemporal_segmentation(points, eps, min_time): # min time / sample rate dt_average = np.median([point.dt for point in points]) min_samples = min_time / dt_average data = [point.gen3arr() for point in points] data = StandardScaler().fit_transform(data) print 'min_samples: %f' % min_samples db_cluster = DBSCAN(eps=eps, min_samples=min_samples).fit(data) labels = db_cluster.labels_ n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0) segments = [[] for _ in range(n_clusters_+1)] clusters = [[] for _ in range(n_clusters_+1)] current_segment = 0 print 'clusters' print n_clusters_ if n_clusters_ == 1: segments = temporal_segmentation([points], min_time) return [segment for segment in segments if len(segment) > 1] # split segments identified with dbscan for i, label in enumerate(labels): if label != -1 and label + 1 != current_segment: current_segment = label + 1 point = points[i] if label == -1: segments[current_segment].append(point) else: clusters[label + 1].append(point) if len(segments) == 0 or sum([len(s) for s in segments]): segments = [points] segments = temporal_segmentation(segments, min_time) # segments = temporal_segmentation(correct_segmentation(segments, clusters, min_time), min_time) return [segment for segment in segments if len(segment) > 1]
[ "def", "spatiotemporal_segmentation", "(", "points", ",", "eps", ",", "min_time", ")", ":", "# min time / sample rate", "dt_average", "=", "np", ".", "median", "(", "[", "point", ".", "dt", "for", "point", "in", "points", "]", ")", "min_samples", "=", "min_time", "/", "dt_average", "data", "=", "[", "point", ".", "gen3arr", "(", ")", "for", "point", "in", "points", "]", "data", "=", "StandardScaler", "(", ")", ".", "fit_transform", "(", "data", ")", "print", "'min_samples: %f'", "%", "min_samples", "db_cluster", "=", "DBSCAN", "(", "eps", "=", "eps", ",", "min_samples", "=", "min_samples", ")", ".", "fit", "(", "data", ")", "labels", "=", "db_cluster", ".", "labels_", "n_clusters_", "=", "len", "(", "set", "(", "labels", ")", ")", "-", "(", "1", "if", "-", "1", "in", "labels", "else", "0", ")", "segments", "=", "[", "[", "]", "for", "_", "in", "range", "(", "n_clusters_", "+", "1", ")", "]", "clusters", "=", "[", "[", "]", "for", "_", "in", "range", "(", "n_clusters_", "+", "1", ")", "]", "current_segment", "=", "0", "print", "'clusters'", "print", "n_clusters_", "if", "n_clusters_", "==", "1", ":", "segments", "=", "temporal_segmentation", "(", "[", "points", "]", ",", "min_time", ")", "return", "[", "segment", "for", "segment", "in", "segments", "if", "len", "(", "segment", ")", ">", "1", "]", "# split segments identified with dbscan", "for", "i", ",", "label", "in", "enumerate", "(", "labels", ")", ":", "if", "label", "!=", "-", "1", "and", "label", "+", "1", "!=", "current_segment", ":", "current_segment", "=", "label", "+", "1", "point", "=", "points", "[", "i", "]", "if", "label", "==", "-", "1", ":", "segments", "[", "current_segment", "]", ".", "append", "(", "point", ")", "else", ":", "clusters", "[", "label", "+", "1", "]", ".", "append", "(", "point", ")", "if", "len", "(", "segments", ")", "==", "0", "or", "sum", "(", "[", "len", "(", "s", ")", "for", "s", "in", "segments", "]", ")", ":", "segments", "=", "[", "points", "]", "segments", "=", "temporal_segmentation", "(", "segments", ",", "min_time", ")", "# segments = temporal_segmentation(correct_segmentation(segments, clusters, min_time), min_time)", "return", "[", "segment", "for", "segment", "in", "segments", "if", "len", "(", "segment", ")", ">", "1", "]" ]
Splits a set of points into multiple sets of points based on spatio-temporal stays DBSCAN is used to predict possible segmentations, furthermore we check to see if each clusters is big enough in time (>=min_time). If that's the case than the segmentation is considered valid. When segmenting, the last point of the ith segment will be the same of the (i-1)th segment. Segments are identified through clusters. The last point of a clusters, that comes after a sub-segment A, will be present on the sub-segment A. Args: points (:obj:`list` of :obj:`Point`): segment's points eps (float): Epsilon to feed to the DBSCAN algorithm. Maximum distance between two samples, to be considered in the same cluster. min_time (float): Minimum time of a stay Returns: :obj:`list` of :obj:`list` of :obj:`Point`: Initial set of points in different segments
[ "Splits", "a", "set", "of", "points", "into", "multiple", "sets", "of", "points", "based", "on", "spatio", "-", "temporal", "stays" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/spatiotemporal_segmentation.py#L61-L124
3,904
ruipgil/TrackToTrip
tracktotrip/kalman.py
kalman_filter
def kalman_filter(points, noise): """ Smooths points with kalman filter See https://github.com/open-city/ikalman Args: points (:obj:`list` of :obj:`Point`): points to smooth noise (float): expected noise """ kalman = ikalman.filter(noise) for point in points: kalman.update_velocity2d(point.lat, point.lon, point.dt) (lat, lon) = kalman.get_lat_long() point.lat = lat point.lon = lon return points
python
def kalman_filter(points, noise): kalman = ikalman.filter(noise) for point in points: kalman.update_velocity2d(point.lat, point.lon, point.dt) (lat, lon) = kalman.get_lat_long() point.lat = lat point.lon = lon return points
[ "def", "kalman_filter", "(", "points", ",", "noise", ")", ":", "kalman", "=", "ikalman", ".", "filter", "(", "noise", ")", "for", "point", "in", "points", ":", "kalman", ".", "update_velocity2d", "(", "point", ".", "lat", ",", "point", ".", "lon", ",", "point", ".", "dt", ")", "(", "lat", ",", "lon", ")", "=", "kalman", ".", "get_lat_long", "(", ")", "point", ".", "lat", "=", "lat", "point", ".", "lon", "=", "lon", "return", "points" ]
Smooths points with kalman filter See https://github.com/open-city/ikalman Args: points (:obj:`list` of :obj:`Point`): points to smooth noise (float): expected noise
[ "Smooths", "points", "with", "kalman", "filter" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/kalman.py#L7-L22
3,905
ruipgil/TrackToTrip
tracktotrip/transportation_mode.py
learn_transportation_mode
def learn_transportation_mode(track, clf): """ Inserts transportation modes of a track into a classifier Args: track (:obj:`Track`) clf (:obj:`Classifier`) """ for segment in track.segments: tmodes = segment.transportation_modes points = segment.points features = [] labels = [] for tmode in tmodes: points_part = points[tmode['from']:tmode['to']] if len(points_part) > 0: features.append(extract_features_2(points_part)) labels.append(tmode['label']) clf.learn(features, labels)
python
def learn_transportation_mode(track, clf): for segment in track.segments: tmodes = segment.transportation_modes points = segment.points features = [] labels = [] for tmode in tmodes: points_part = points[tmode['from']:tmode['to']] if len(points_part) > 0: features.append(extract_features_2(points_part)) labels.append(tmode['label']) clf.learn(features, labels)
[ "def", "learn_transportation_mode", "(", "track", ",", "clf", ")", ":", "for", "segment", "in", "track", ".", "segments", ":", "tmodes", "=", "segment", ".", "transportation_modes", "points", "=", "segment", ".", "points", "features", "=", "[", "]", "labels", "=", "[", "]", "for", "tmode", "in", "tmodes", ":", "points_part", "=", "points", "[", "tmode", "[", "'from'", "]", ":", "tmode", "[", "'to'", "]", "]", "if", "len", "(", "points_part", ")", ">", "0", ":", "features", ".", "append", "(", "extract_features_2", "(", "points_part", ")", ")", "labels", ".", "append", "(", "tmode", "[", "'label'", "]", ")", "clf", ".", "learn", "(", "features", ",", "labels", ")" ]
Inserts transportation modes of a track into a classifier Args: track (:obj:`Track`) clf (:obj:`Classifier`)
[ "Inserts", "transportation", "modes", "of", "a", "track", "into", "a", "classifier" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/transportation_mode.py#L57-L76
3,906
ruipgil/TrackToTrip
tracktotrip/transportation_mode.py
speed_difference
def speed_difference(points): """ Computes the speed difference between each adjacent point Args: points (:obj:`Point`) Returns: :obj:`list` of int: Indexes of changepoints """ data = [0] for before, after in pairwise(points): data.append(before.vel - after.vel) return data
python
def speed_difference(points): data = [0] for before, after in pairwise(points): data.append(before.vel - after.vel) return data
[ "def", "speed_difference", "(", "points", ")", ":", "data", "=", "[", "0", "]", "for", "before", ",", "after", "in", "pairwise", "(", "points", ")", ":", "data", ".", "append", "(", "before", ".", "vel", "-", "after", ".", "vel", ")", "return", "data" ]
Computes the speed difference between each adjacent point Args: points (:obj:`Point`) Returns: :obj:`list` of int: Indexes of changepoints
[ "Computes", "the", "speed", "difference", "between", "each", "adjacent", "point" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/transportation_mode.py#L116-L127
3,907
ruipgil/TrackToTrip
tracktotrip/transportation_mode.py
acc_difference
def acc_difference(points): """ Computes the accelaration difference between each adjacent point Args: points (:obj:`Point`) Returns: :obj:`list` of int: Indexes of changepoints """ data = [0] for before, after in pairwise(points): data.append(before.acc - after.acc) return data
python
def acc_difference(points): data = [0] for before, after in pairwise(points): data.append(before.acc - after.acc) return data
[ "def", "acc_difference", "(", "points", ")", ":", "data", "=", "[", "0", "]", "for", "before", ",", "after", "in", "pairwise", "(", "points", ")", ":", "data", ".", "append", "(", "before", ".", "acc", "-", "after", ".", "acc", ")", "return", "data" ]
Computes the accelaration difference between each adjacent point Args: points (:obj:`Point`) Returns: :obj:`list` of int: Indexes of changepoints
[ "Computes", "the", "accelaration", "difference", "between", "each", "adjacent", "point" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/transportation_mode.py#L129-L140
3,908
ruipgil/TrackToTrip
tracktotrip/transportation_mode.py
detect_changepoints
def detect_changepoints(points, min_time, data_processor=acc_difference): """ Detects changepoints on points that have at least a specific duration Args: points (:obj:`Point`) min_time (float): Min time that a sub-segmented, bounded by two changepoints, must have data_processor (function): Function to extract data to feed to the changepoint algorithm. Defaults to `speed_difference` Returns: :obj:`list` of int: Indexes of changepoints """ data = data_processor(points) changepoints = pelt(normal_mean(data, np.std(data)), len(data)) changepoints.append(len(points) - 1) result = [] for start, end in pairwise(changepoints): time_diff = points[end].time_difference(points[start]) if time_diff > min_time: result.append(start) # adds the first point result.append(0) # adds the last changepoint detected result.append(len(points) - 1) return sorted(list(set(result)))
python
def detect_changepoints(points, min_time, data_processor=acc_difference): data = data_processor(points) changepoints = pelt(normal_mean(data, np.std(data)), len(data)) changepoints.append(len(points) - 1) result = [] for start, end in pairwise(changepoints): time_diff = points[end].time_difference(points[start]) if time_diff > min_time: result.append(start) # adds the first point result.append(0) # adds the last changepoint detected result.append(len(points) - 1) return sorted(list(set(result)))
[ "def", "detect_changepoints", "(", "points", ",", "min_time", ",", "data_processor", "=", "acc_difference", ")", ":", "data", "=", "data_processor", "(", "points", ")", "changepoints", "=", "pelt", "(", "normal_mean", "(", "data", ",", "np", ".", "std", "(", "data", ")", ")", ",", "len", "(", "data", ")", ")", "changepoints", ".", "append", "(", "len", "(", "points", ")", "-", "1", ")", "result", "=", "[", "]", "for", "start", ",", "end", "in", "pairwise", "(", "changepoints", ")", ":", "time_diff", "=", "points", "[", "end", "]", ".", "time_difference", "(", "points", "[", "start", "]", ")", "if", "time_diff", ">", "min_time", ":", "result", ".", "append", "(", "start", ")", "# adds the first point", "result", ".", "append", "(", "0", ")", "# adds the last changepoint detected", "result", ".", "append", "(", "len", "(", "points", ")", "-", "1", ")", "return", "sorted", "(", "list", "(", "set", "(", "result", ")", ")", ")" ]
Detects changepoints on points that have at least a specific duration Args: points (:obj:`Point`) min_time (float): Min time that a sub-segmented, bounded by two changepoints, must have data_processor (function): Function to extract data to feed to the changepoint algorithm. Defaults to `speed_difference` Returns: :obj:`list` of int: Indexes of changepoints
[ "Detects", "changepoints", "on", "points", "that", "have", "at", "least", "a", "specific", "duration" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/transportation_mode.py#L142-L167
3,909
ruipgil/TrackToTrip
tracktotrip/transportation_mode.py
group_modes
def group_modes(modes): """ Groups consecutive transportation modes with same label, into one Args: modes (:obj:`list` of :obj:`dict`) Returns: :obj:`list` of :obj:`dict` """ if len(modes) > 0: previous = modes[0] grouped = [] for changep in modes[1:]: if changep['label'] != previous['label']: previous['to'] = changep['from'] grouped.append(previous) previous = changep previous['to'] = modes[-1]['to'] grouped.append(previous) return grouped else: return modes
python
def group_modes(modes): if len(modes) > 0: previous = modes[0] grouped = [] for changep in modes[1:]: if changep['label'] != previous['label']: previous['to'] = changep['from'] grouped.append(previous) previous = changep previous['to'] = modes[-1]['to'] grouped.append(previous) return grouped else: return modes
[ "def", "group_modes", "(", "modes", ")", ":", "if", "len", "(", "modes", ")", ">", "0", ":", "previous", "=", "modes", "[", "0", "]", "grouped", "=", "[", "]", "for", "changep", "in", "modes", "[", "1", ":", "]", ":", "if", "changep", "[", "'label'", "]", "!=", "previous", "[", "'label'", "]", ":", "previous", "[", "'to'", "]", "=", "changep", "[", "'from'", "]", "grouped", ".", "append", "(", "previous", ")", "previous", "=", "changep", "previous", "[", "'to'", "]", "=", "modes", "[", "-", "1", "]", "[", "'to'", "]", "grouped", ".", "append", "(", "previous", ")", "return", "grouped", "else", ":", "return", "modes" ]
Groups consecutive transportation modes with same label, into one Args: modes (:obj:`list` of :obj:`dict`) Returns: :obj:`list` of :obj:`dict`
[ "Groups", "consecutive", "transportation", "modes", "with", "same", "label", "into", "one" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/transportation_mode.py#L169-L191
3,910
ruipgil/TrackToTrip
tracktotrip/transportation_mode.py
speed_clustering
def speed_clustering(clf, points, min_time): """ Transportation mode infering, based on changepoint segmentation Args: clf (:obj:`Classifier`): Classifier to use points (:obj:`list` of :obj:`Point`) min_time (float): Min time, in seconds, before do another segmentation Returns: :obj:`list` of :obj:`dict` """ # get changepoint indexes changepoints = detect_changepoints(points, min_time) # info for each changepoint cp_info = [] for i in range(0, len(changepoints) - 1): from_index = changepoints[i] to_index = changepoints[i+1] info = classify(clf, points[from_index:to_index], min_time, from_index, to_index) if info: cp_info.append(info) return group_modes(cp_info)
python
def speed_clustering(clf, points, min_time): # get changepoint indexes changepoints = detect_changepoints(points, min_time) # info for each changepoint cp_info = [] for i in range(0, len(changepoints) - 1): from_index = changepoints[i] to_index = changepoints[i+1] info = classify(clf, points[from_index:to_index], min_time, from_index, to_index) if info: cp_info.append(info) return group_modes(cp_info)
[ "def", "speed_clustering", "(", "clf", ",", "points", ",", "min_time", ")", ":", "# get changepoint indexes", "changepoints", "=", "detect_changepoints", "(", "points", ",", "min_time", ")", "# info for each changepoint", "cp_info", "=", "[", "]", "for", "i", "in", "range", "(", "0", ",", "len", "(", "changepoints", ")", "-", "1", ")", ":", "from_index", "=", "changepoints", "[", "i", "]", "to_index", "=", "changepoints", "[", "i", "+", "1", "]", "info", "=", "classify", "(", "clf", ",", "points", "[", "from_index", ":", "to_index", "]", ",", "min_time", ",", "from_index", ",", "to_index", ")", "if", "info", ":", "cp_info", ".", "append", "(", "info", ")", "return", "group_modes", "(", "cp_info", ")" ]
Transportation mode infering, based on changepoint segmentation Args: clf (:obj:`Classifier`): Classifier to use points (:obj:`list` of :obj:`Point`) min_time (float): Min time, in seconds, before do another segmentation Returns: :obj:`list` of :obj:`dict`
[ "Transportation", "mode", "infering", "based", "on", "changepoint", "segmentation" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/transportation_mode.py#L208-L231
3,911
ruipgil/TrackToTrip
tracktotrip/compression.py
distance
def distance(p_a, p_b): """ Euclidean distance, between two points Args: p_a (:obj:`Point`) p_b (:obj:`Point`) Returns: float: distance, in degrees """ return sqrt((p_a.lat - p_b.lat) ** 2 + (p_a.lon - p_b.lon) ** 2)
python
def distance(p_a, p_b): return sqrt((p_a.lat - p_b.lat) ** 2 + (p_a.lon - p_b.lon) ** 2)
[ "def", "distance", "(", "p_a", ",", "p_b", ")", ":", "return", "sqrt", "(", "(", "p_a", ".", "lat", "-", "p_b", ".", "lat", ")", "**", "2", "+", "(", "p_a", ".", "lon", "-", "p_b", ".", "lon", ")", "**", "2", ")" ]
Euclidean distance, between two points Args: p_a (:obj:`Point`) p_b (:obj:`Point`) Returns: float: distance, in degrees
[ "Euclidean", "distance", "between", "two", "points" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/compression.py#L40-L49
3,912
ruipgil/TrackToTrip
tracktotrip/compression.py
point_line_distance
def point_line_distance(point, start, end): """ Distance from a point to a line, formed by two points Args: point (:obj:`Point`) start (:obj:`Point`): line point end (:obj:`Point`): line point Returns: float: distance to line, in degrees """ if start == end: return distance(point, start) else: un_dist = abs( (end.lat-start.lat)*(start.lon-point.lon) - (start.lat-point.lat)*(end.lon-start.lon) ) n_dist = sqrt( (end.lat-start.lat)**2 + (end.lon-start.lon)**2 ) if n_dist == 0: return 0 else: return un_dist / n_dist
python
def point_line_distance(point, start, end): if start == end: return distance(point, start) else: un_dist = abs( (end.lat-start.lat)*(start.lon-point.lon) - (start.lat-point.lat)*(end.lon-start.lon) ) n_dist = sqrt( (end.lat-start.lat)**2 + (end.lon-start.lon)**2 ) if n_dist == 0: return 0 else: return un_dist / n_dist
[ "def", "point_line_distance", "(", "point", ",", "start", ",", "end", ")", ":", "if", "start", "==", "end", ":", "return", "distance", "(", "point", ",", "start", ")", "else", ":", "un_dist", "=", "abs", "(", "(", "end", ".", "lat", "-", "start", ".", "lat", ")", "*", "(", "start", ".", "lon", "-", "point", ".", "lon", ")", "-", "(", "start", ".", "lat", "-", "point", ".", "lat", ")", "*", "(", "end", ".", "lon", "-", "start", ".", "lon", ")", ")", "n_dist", "=", "sqrt", "(", "(", "end", ".", "lat", "-", "start", ".", "lat", ")", "**", "2", "+", "(", "end", ".", "lon", "-", "start", ".", "lon", ")", "**", "2", ")", "if", "n_dist", "==", "0", ":", "return", "0", "else", ":", "return", "un_dist", "/", "n_dist" ]
Distance from a point to a line, formed by two points Args: point (:obj:`Point`) start (:obj:`Point`): line point end (:obj:`Point`): line point Returns: float: distance to line, in degrees
[ "Distance", "from", "a", "point", "to", "a", "line", "formed", "by", "two", "points" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/compression.py#L51-L73
3,913
ruipgil/TrackToTrip
tracktotrip/compression.py
drp
def drp(points, epsilon): """ Douglas ramer peucker Based on https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm Args: points (:obj:`list` of :obj:`Point`) epsilon (float): drp threshold Returns: :obj:`list` of :obj:`Point` """ dmax = 0.0 index = 0 for i in range(1, len(points)-1): dist = point_line_distance(points[i], points[0], points[-1]) if dist > dmax: index = i dmax = dist if dmax > epsilon: return drp(points[:index+1], epsilon)[:-1] + drp(points[index:], epsilon) else: return [points[0], points[-1]]
python
def drp(points, epsilon): dmax = 0.0 index = 0 for i in range(1, len(points)-1): dist = point_line_distance(points[i], points[0], points[-1]) if dist > dmax: index = i dmax = dist if dmax > epsilon: return drp(points[:index+1], epsilon)[:-1] + drp(points[index:], epsilon) else: return [points[0], points[-1]]
[ "def", "drp", "(", "points", ",", "epsilon", ")", ":", "dmax", "=", "0.0", "index", "=", "0", "for", "i", "in", "range", "(", "1", ",", "len", "(", "points", ")", "-", "1", ")", ":", "dist", "=", "point_line_distance", "(", "points", "[", "i", "]", ",", "points", "[", "0", "]", ",", "points", "[", "-", "1", "]", ")", "if", "dist", ">", "dmax", ":", "index", "=", "i", "dmax", "=", "dist", "if", "dmax", ">", "epsilon", ":", "return", "drp", "(", "points", "[", ":", "index", "+", "1", "]", ",", "epsilon", ")", "[", ":", "-", "1", "]", "+", "drp", "(", "points", "[", "index", ":", "]", ",", "epsilon", ")", "else", ":", "return", "[", "points", "[", "0", "]", ",", "points", "[", "-", "1", "]", "]" ]
Douglas ramer peucker Based on https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm Args: points (:obj:`list` of :obj:`Point`) epsilon (float): drp threshold Returns: :obj:`list` of :obj:`Point`
[ "Douglas", "ramer", "peucker" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/compression.py#L75-L98
3,914
ruipgil/TrackToTrip
tracktotrip/compression.py
td_sp
def td_sp(points, speed_threshold): """ Top-Down Speed-Based Trajectory Compression Algorithm Detailed in https://www.itc.nl/library/Papers_2003/peer_ref_conf/meratnia_new.pdf Args: points (:obj:`list` of :obj:`Point`): trajectory or part of it speed_threshold (float): max speed error, in km/h Returns: :obj:`list` of :obj:`Point`, compressed trajectory """ if len(points) <= 2: return points else: max_speed_threshold = 0 found_index = 0 for i in range(1, len(points)-1): dt1 = time_dist(points[i], points[i-1]) if dt1 == 0: dt1 = 0.000000001 vim = loc_dist(points[i], points[i-1]) / dt1 dt2 = time_dist(points[i+1], points[i]) if dt2 == 0: dt2 = 0.000000001 vi_ = loc_dist(points[i+1], points[i]) / dt2 if abs(vi_ - vim) > max_speed_threshold: max_speed_threshold = abs(vi_ - vim) found_index = i if max_speed_threshold > speed_threshold: one = td_sp(points[:found_index], speed_threshold) two = td_sp(points[found_index:], speed_threshold) one.extend(two) return one else: return [points[0], points[-1]]
python
def td_sp(points, speed_threshold): if len(points) <= 2: return points else: max_speed_threshold = 0 found_index = 0 for i in range(1, len(points)-1): dt1 = time_dist(points[i], points[i-1]) if dt1 == 0: dt1 = 0.000000001 vim = loc_dist(points[i], points[i-1]) / dt1 dt2 = time_dist(points[i+1], points[i]) if dt2 == 0: dt2 = 0.000000001 vi_ = loc_dist(points[i+1], points[i]) / dt2 if abs(vi_ - vim) > max_speed_threshold: max_speed_threshold = abs(vi_ - vim) found_index = i if max_speed_threshold > speed_threshold: one = td_sp(points[:found_index], speed_threshold) two = td_sp(points[found_index:], speed_threshold) one.extend(two) return one else: return [points[0], points[-1]]
[ "def", "td_sp", "(", "points", ",", "speed_threshold", ")", ":", "if", "len", "(", "points", ")", "<=", "2", ":", "return", "points", "else", ":", "max_speed_threshold", "=", "0", "found_index", "=", "0", "for", "i", "in", "range", "(", "1", ",", "len", "(", "points", ")", "-", "1", ")", ":", "dt1", "=", "time_dist", "(", "points", "[", "i", "]", ",", "points", "[", "i", "-", "1", "]", ")", "if", "dt1", "==", "0", ":", "dt1", "=", "0.000000001", "vim", "=", "loc_dist", "(", "points", "[", "i", "]", ",", "points", "[", "i", "-", "1", "]", ")", "/", "dt1", "dt2", "=", "time_dist", "(", "points", "[", "i", "+", "1", "]", ",", "points", "[", "i", "]", ")", "if", "dt2", "==", "0", ":", "dt2", "=", "0.000000001", "vi_", "=", "loc_dist", "(", "points", "[", "i", "+", "1", "]", ",", "points", "[", "i", "]", ")", "/", "dt2", "if", "abs", "(", "vi_", "-", "vim", ")", ">", "max_speed_threshold", ":", "max_speed_threshold", "=", "abs", "(", "vi_", "-", "vim", ")", "found_index", "=", "i", "if", "max_speed_threshold", ">", "speed_threshold", ":", "one", "=", "td_sp", "(", "points", "[", ":", "found_index", "]", ",", "speed_threshold", ")", "two", "=", "td_sp", "(", "points", "[", "found_index", ":", "]", ",", "speed_threshold", ")", "one", ".", "extend", "(", "two", ")", "return", "one", "else", ":", "return", "[", "points", "[", "0", "]", ",", "points", "[", "-", "1", "]", "]" ]
Top-Down Speed-Based Trajectory Compression Algorithm Detailed in https://www.itc.nl/library/Papers_2003/peer_ref_conf/meratnia_new.pdf Args: points (:obj:`list` of :obj:`Point`): trajectory or part of it speed_threshold (float): max speed error, in km/h Returns: :obj:`list` of :obj:`Point`, compressed trajectory
[ "Top", "-", "Down", "Speed", "-", "Based", "Trajectory", "Compression", "Algorithm" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/compression.py#L100-L134
3,915
ruipgil/TrackToTrip
tracktotrip/compression.py
td_tr
def td_tr(points, dist_threshold): """ Top-Down Time-Ratio Trajectory Compression Algorithm Detailed in https://www.itc.nl/library/Papers_2003/peer_ref_conf/meratnia_new.pdf Args: points (:obj:`list` of :obj:`Point`): trajectory or part of it dist_threshold (float): max distance error, in meters Returns: :obj:`list` of :obj:`Point`, compressed trajectory """ if len(points) <= 2: return points else: max_dist_threshold = 0 found_index = 0 delta_e = time_dist(points[-1], points[0]) * I_3600 d_lat = points[-1].lat - points[0].lat d_lon = points[-1].lon - points[0].lon for i in range(1, len(points)-1): delta_i = time_dist(points[i], points[0]) * I_3600 di_de = delta_i / delta_e point = Point( points[0].lat + d_lat * di_de, points[0].lon + d_lon * di_de, None ) dist = loc_dist(points[i], point) if dist > max_dist_threshold: max_dist_threshold = dist found_index = i if max_dist_threshold > dist_threshold: one = td_tr(points[:found_index], dist_threshold) two = td_tr(points[found_index:], dist_threshold) one.extend(two) return one else: return [points[0], points[-1]]
python
def td_tr(points, dist_threshold): if len(points) <= 2: return points else: max_dist_threshold = 0 found_index = 0 delta_e = time_dist(points[-1], points[0]) * I_3600 d_lat = points[-1].lat - points[0].lat d_lon = points[-1].lon - points[0].lon for i in range(1, len(points)-1): delta_i = time_dist(points[i], points[0]) * I_3600 di_de = delta_i / delta_e point = Point( points[0].lat + d_lat * di_de, points[0].lon + d_lon * di_de, None ) dist = loc_dist(points[i], point) if dist > max_dist_threshold: max_dist_threshold = dist found_index = i if max_dist_threshold > dist_threshold: one = td_tr(points[:found_index], dist_threshold) two = td_tr(points[found_index:], dist_threshold) one.extend(two) return one else: return [points[0], points[-1]]
[ "def", "td_tr", "(", "points", ",", "dist_threshold", ")", ":", "if", "len", "(", "points", ")", "<=", "2", ":", "return", "points", "else", ":", "max_dist_threshold", "=", "0", "found_index", "=", "0", "delta_e", "=", "time_dist", "(", "points", "[", "-", "1", "]", ",", "points", "[", "0", "]", ")", "*", "I_3600", "d_lat", "=", "points", "[", "-", "1", "]", ".", "lat", "-", "points", "[", "0", "]", ".", "lat", "d_lon", "=", "points", "[", "-", "1", "]", ".", "lon", "-", "points", "[", "0", "]", ".", "lon", "for", "i", "in", "range", "(", "1", ",", "len", "(", "points", ")", "-", "1", ")", ":", "delta_i", "=", "time_dist", "(", "points", "[", "i", "]", ",", "points", "[", "0", "]", ")", "*", "I_3600", "di_de", "=", "delta_i", "/", "delta_e", "point", "=", "Point", "(", "points", "[", "0", "]", ".", "lat", "+", "d_lat", "*", "di_de", ",", "points", "[", "0", "]", ".", "lon", "+", "d_lon", "*", "di_de", ",", "None", ")", "dist", "=", "loc_dist", "(", "points", "[", "i", "]", ",", "point", ")", "if", "dist", ">", "max_dist_threshold", ":", "max_dist_threshold", "=", "dist", "found_index", "=", "i", "if", "max_dist_threshold", ">", "dist_threshold", ":", "one", "=", "td_tr", "(", "points", "[", ":", "found_index", "]", ",", "dist_threshold", ")", "two", "=", "td_tr", "(", "points", "[", "found_index", ":", "]", ",", "dist_threshold", ")", "one", ".", "extend", "(", "two", ")", "return", "one", "else", ":", "return", "[", "points", "[", "0", "]", ",", "points", "[", "-", "1", "]", "]" ]
Top-Down Time-Ratio Trajectory Compression Algorithm Detailed in https://www.itc.nl/library/Papers_2003/peer_ref_conf/meratnia_new.pdf Args: points (:obj:`list` of :obj:`Point`): trajectory or part of it dist_threshold (float): max distance error, in meters Returns: :obj:`list` of :obj:`Point`, compressed trajectory
[ "Top", "-", "Down", "Time", "-", "Ratio", "Trajectory", "Compression", "Algorithm" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/compression.py#L136-L177
3,916
ruipgil/TrackToTrip
tracktotrip/compression.py
spt
def spt(points, max_dist_error, max_speed_error): """ A combination of both `td_sp` and `td_tr` Detailed in, Spatiotemporal Compression Techniques for Moving Point Objects, Nirvana Meratnia and Rolf A. de By, 2004, in Advances in Database Technology - EDBT 2004: 9th International Conference on Extending Database Technology, Heraklion, Crete, Greece, March 14-18, 2004 Args: points (:obj:`list` of :obj:`Point`) max_dist_error (float): max distance error, in meters max_speed_error (float): max speed error, in km/h Returns: :obj:`list` of :obj:`Point` """ if len(points) <= 2: return points else: is_error = False e = 1 while e < len(points) and not is_error: i = 1 while i < e and not is_error: delta_e = time_dist(points[e], points[0]) * I_3600 delta_i = time_dist(points[i], points[0]) * I_3600 di_de = 0 if delta_e != 0: di_de = delta_i / delta_e d_lat = points[e].lat - points[0].lat d_lon = points[e].lon - points[0].lon point = Point( points[0].lat + d_lat * di_de, points[0].lon + d_lon * di_de, None ) dt1 = time_dist(points[i], points[i-1]) if dt1 == 0: dt1 = 0.000000001 dt2 = time_dist(points[i+1], points[i]) if dt2 == 0: dt2 = 0.000000001 v_i_1 = loc_dist(points[i], points[i-1]) / dt1 v_i = loc_dist(points[i+1], points[i]) / dt2 if loc_dist(points[i], point) > max_dist_error or abs(v_i - v_i_1) > max_speed_error: is_error = True else: i = i + 1 if is_error: return [points[0]] + spt(points[i:len(points)], max_dist_error, max_speed_error) e = e + 1 if not is_error: return [points[0], points[len(points)-1]]
python
def spt(points, max_dist_error, max_speed_error): if len(points) <= 2: return points else: is_error = False e = 1 while e < len(points) and not is_error: i = 1 while i < e and not is_error: delta_e = time_dist(points[e], points[0]) * I_3600 delta_i = time_dist(points[i], points[0]) * I_3600 di_de = 0 if delta_e != 0: di_de = delta_i / delta_e d_lat = points[e].lat - points[0].lat d_lon = points[e].lon - points[0].lon point = Point( points[0].lat + d_lat * di_de, points[0].lon + d_lon * di_de, None ) dt1 = time_dist(points[i], points[i-1]) if dt1 == 0: dt1 = 0.000000001 dt2 = time_dist(points[i+1], points[i]) if dt2 == 0: dt2 = 0.000000001 v_i_1 = loc_dist(points[i], points[i-1]) / dt1 v_i = loc_dist(points[i+1], points[i]) / dt2 if loc_dist(points[i], point) > max_dist_error or abs(v_i - v_i_1) > max_speed_error: is_error = True else: i = i + 1 if is_error: return [points[0]] + spt(points[i:len(points)], max_dist_error, max_speed_error) e = e + 1 if not is_error: return [points[0], points[len(points)-1]]
[ "def", "spt", "(", "points", ",", "max_dist_error", ",", "max_speed_error", ")", ":", "if", "len", "(", "points", ")", "<=", "2", ":", "return", "points", "else", ":", "is_error", "=", "False", "e", "=", "1", "while", "e", "<", "len", "(", "points", ")", "and", "not", "is_error", ":", "i", "=", "1", "while", "i", "<", "e", "and", "not", "is_error", ":", "delta_e", "=", "time_dist", "(", "points", "[", "e", "]", ",", "points", "[", "0", "]", ")", "*", "I_3600", "delta_i", "=", "time_dist", "(", "points", "[", "i", "]", ",", "points", "[", "0", "]", ")", "*", "I_3600", "di_de", "=", "0", "if", "delta_e", "!=", "0", ":", "di_de", "=", "delta_i", "/", "delta_e", "d_lat", "=", "points", "[", "e", "]", ".", "lat", "-", "points", "[", "0", "]", ".", "lat", "d_lon", "=", "points", "[", "e", "]", ".", "lon", "-", "points", "[", "0", "]", ".", "lon", "point", "=", "Point", "(", "points", "[", "0", "]", ".", "lat", "+", "d_lat", "*", "di_de", ",", "points", "[", "0", "]", ".", "lon", "+", "d_lon", "*", "di_de", ",", "None", ")", "dt1", "=", "time_dist", "(", "points", "[", "i", "]", ",", "points", "[", "i", "-", "1", "]", ")", "if", "dt1", "==", "0", ":", "dt1", "=", "0.000000001", "dt2", "=", "time_dist", "(", "points", "[", "i", "+", "1", "]", ",", "points", "[", "i", "]", ")", "if", "dt2", "==", "0", ":", "dt2", "=", "0.000000001", "v_i_1", "=", "loc_dist", "(", "points", "[", "i", "]", ",", "points", "[", "i", "-", "1", "]", ")", "/", "dt1", "v_i", "=", "loc_dist", "(", "points", "[", "i", "+", "1", "]", ",", "points", "[", "i", "]", ")", "/", "dt2", "if", "loc_dist", "(", "points", "[", "i", "]", ",", "point", ")", ">", "max_dist_error", "or", "abs", "(", "v_i", "-", "v_i_1", ")", ">", "max_speed_error", ":", "is_error", "=", "True", "else", ":", "i", "=", "i", "+", "1", "if", "is_error", ":", "return", "[", "points", "[", "0", "]", "]", "+", "spt", "(", "points", "[", "i", ":", "len", "(", "points", ")", "]", ",", "max_dist_error", ",", "max_speed_error", ")", "e", "=", "e", "+", "1", "if", "not", "is_error", ":", "return", "[", "points", "[", "0", "]", ",", "points", "[", "len", "(", "points", ")", "-", "1", "]", "]" ]
A combination of both `td_sp` and `td_tr` Detailed in, Spatiotemporal Compression Techniques for Moving Point Objects, Nirvana Meratnia and Rolf A. de By, 2004, in Advances in Database Technology - EDBT 2004: 9th International Conference on Extending Database Technology, Heraklion, Crete, Greece, March 14-18, 2004 Args: points (:obj:`list` of :obj:`Point`) max_dist_error (float): max distance error, in meters max_speed_error (float): max speed error, in km/h Returns: :obj:`list` of :obj:`Point`
[ "A", "combination", "of", "both", "td_sp", "and", "td_tr" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/compression.py#L179-L236
3,917
ruipgil/TrackToTrip
tracktotrip/track.py
Track.generate_name
def generate_name(self, name_format=DEFAULT_FILE_NAME_FORMAT): """ Generates a name for the track The name is generated based on the date of the first point of the track, or in case it doesn't exist, "EmptyTrack" Args: name_format (str, optional): Name formar to give to the track, based on its start time. Defaults to DEFAULT_FILE_NAME_FORMAT Returns: str """ if len(self.segments) > 0: return self.segments[0].points[0].time.strftime(name_format) + ".gpx" else: return "EmptyTrack"
python
def generate_name(self, name_format=DEFAULT_FILE_NAME_FORMAT): if len(self.segments) > 0: return self.segments[0].points[0].time.strftime(name_format) + ".gpx" else: return "EmptyTrack"
[ "def", "generate_name", "(", "self", ",", "name_format", "=", "DEFAULT_FILE_NAME_FORMAT", ")", ":", "if", "len", "(", "self", ".", "segments", ")", ">", "0", ":", "return", "self", ".", "segments", "[", "0", "]", ".", "points", "[", "0", "]", ".", "time", ".", "strftime", "(", "name_format", ")", "+", "\".gpx\"", "else", ":", "return", "\"EmptyTrack\"" ]
Generates a name for the track The name is generated based on the date of the first point of the track, or in case it doesn't exist, "EmptyTrack" Args: name_format (str, optional): Name formar to give to the track, based on its start time. Defaults to DEFAULT_FILE_NAME_FORMAT Returns: str
[ "Generates", "a", "name", "for", "the", "track" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/track.py#L44-L59
3,918
ruipgil/TrackToTrip
tracktotrip/track.py
Track.smooth
def smooth(self, strategy, noise): """ In-place smoothing of segments Returns: :obj:`Track`: self """ print noise for segment in self.segments: segment.smooth(noise, strategy) return self
python
def smooth(self, strategy, noise): print noise for segment in self.segments: segment.smooth(noise, strategy) return self
[ "def", "smooth", "(", "self", ",", "strategy", ",", "noise", ")", ":", "print", "noise", "for", "segment", "in", "self", ".", "segments", ":", "segment", ".", "smooth", "(", "noise", ",", "strategy", ")", "return", "self" ]
In-place smoothing of segments Returns: :obj:`Track`: self
[ "In", "-", "place", "smoothing", "of", "segments" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/track.py#L71-L80
3,919
ruipgil/TrackToTrip
tracktotrip/track.py
Track.segment
def segment(self, eps, min_time): """In-place segmentation of segments Spatio-temporal segmentation of each segment The number of segments may increse after this step Returns: This track """ new_segments = [] for segment in self.segments: segmented = segment.segment(eps, min_time) for seg in segmented: new_segments.append(Segment(seg)) self.segments = new_segments return self
python
def segment(self, eps, min_time): new_segments = [] for segment in self.segments: segmented = segment.segment(eps, min_time) for seg in segmented: new_segments.append(Segment(seg)) self.segments = new_segments return self
[ "def", "segment", "(", "self", ",", "eps", ",", "min_time", ")", ":", "new_segments", "=", "[", "]", "for", "segment", "in", "self", ".", "segments", ":", "segmented", "=", "segment", ".", "segment", "(", "eps", ",", "min_time", ")", "for", "seg", "in", "segmented", ":", "new_segments", ".", "append", "(", "Segment", "(", "seg", ")", ")", "self", ".", "segments", "=", "new_segments", "return", "self" ]
In-place segmentation of segments Spatio-temporal segmentation of each segment The number of segments may increse after this step Returns: This track
[ "In", "-", "place", "segmentation", "of", "segments" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/track.py#L82-L97
3,920
ruipgil/TrackToTrip
tracktotrip/track.py
Track.simplify
def simplify(self, eps, max_dist_error, max_speed_error, topology_only=False): """ In-place simplification of segments Args: max_dist_error (float): Min distance error, in meters max_speed_error (float): Min speed error, in km/h topology_only: Boolean, optional. True to keep the topology, neglecting velocity and time accuracy (use common Douglas-Ramen-Peucker). False (default) to simplify segments keeping the velocity between points. Returns: This track """ for segment in self.segments: segment.simplify(eps, max_dist_error, max_speed_error, topology_only) return self
python
def simplify(self, eps, max_dist_error, max_speed_error, topology_only=False): for segment in self.segments: segment.simplify(eps, max_dist_error, max_speed_error, topology_only) return self
[ "def", "simplify", "(", "self", ",", "eps", ",", "max_dist_error", ",", "max_speed_error", ",", "topology_only", "=", "False", ")", ":", "for", "segment", "in", "self", ".", "segments", ":", "segment", ".", "simplify", "(", "eps", ",", "max_dist_error", ",", "max_speed_error", ",", "topology_only", ")", "return", "self" ]
In-place simplification of segments Args: max_dist_error (float): Min distance error, in meters max_speed_error (float): Min speed error, in km/h topology_only: Boolean, optional. True to keep the topology, neglecting velocity and time accuracy (use common Douglas-Ramen-Peucker). False (default) to simplify segments keeping the velocity between points. Returns: This track
[ "In", "-", "place", "simplification", "of", "segments" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/track.py#L99-L115
3,921
ruipgil/TrackToTrip
tracktotrip/track.py
Track.infer_transportation_mode
def infer_transportation_mode(self, clf, min_time): """In-place transportation mode inferring of segments Returns: This track """ for segment in self.segments: segment.infer_transportation_mode(clf, min_time) return self
python
def infer_transportation_mode(self, clf, min_time): for segment in self.segments: segment.infer_transportation_mode(clf, min_time) return self
[ "def", "infer_transportation_mode", "(", "self", ",", "clf", ",", "min_time", ")", ":", "for", "segment", "in", "self", ".", "segments", ":", "segment", ".", "infer_transportation_mode", "(", "clf", ",", "min_time", ")", "return", "self" ]
In-place transportation mode inferring of segments Returns: This track
[ "In", "-", "place", "transportation", "mode", "inferring", "of", "segments" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/track.py#L117-L125
3,922
ruipgil/TrackToTrip
tracktotrip/track.py
Track.to_trip
def to_trip( self, smooth, smooth_strategy, smooth_noise, seg, seg_eps, seg_min_time, simplify, simplify_max_dist_error, simplify_max_speed_error ): """In-place, transformation of a track into a trip A trip is a more accurate depiction of reality than a track. For a track to become a trip it need to go through the following steps: + noise removal + smoothing + spatio-temporal segmentation + simplification At the end of these steps we have a less noisy, track that has less points, but that holds the same information. It's required that each segment has their metrics calculated or has been preprocessed. Args: name: An optional string with the name of the trip. If none is given, one will be generated by generateName Returns: This Track instance """ self.compute_metrics() self.remove_noise() print (smooth, seg, simplify) if smooth: self.compute_metrics() self.smooth(smooth_strategy, smooth_noise) if seg: self.compute_metrics() self.segment(seg_eps, seg_min_time) if simplify: self.compute_metrics() self.simplify(0, simplify_max_dist_error, simplify_max_speed_error) self.compute_metrics() return self
python
def to_trip( self, smooth, smooth_strategy, smooth_noise, seg, seg_eps, seg_min_time, simplify, simplify_max_dist_error, simplify_max_speed_error ): self.compute_metrics() self.remove_noise() print (smooth, seg, simplify) if smooth: self.compute_metrics() self.smooth(smooth_strategy, smooth_noise) if seg: self.compute_metrics() self.segment(seg_eps, seg_min_time) if simplify: self.compute_metrics() self.simplify(0, simplify_max_dist_error, simplify_max_speed_error) self.compute_metrics() return self
[ "def", "to_trip", "(", "self", ",", "smooth", ",", "smooth_strategy", ",", "smooth_noise", ",", "seg", ",", "seg_eps", ",", "seg_min_time", ",", "simplify", ",", "simplify_max_dist_error", ",", "simplify_max_speed_error", ")", ":", "self", ".", "compute_metrics", "(", ")", "self", ".", "remove_noise", "(", ")", "print", "(", "smooth", ",", "seg", ",", "simplify", ")", "if", "smooth", ":", "self", ".", "compute_metrics", "(", ")", "self", ".", "smooth", "(", "smooth_strategy", ",", "smooth_noise", ")", "if", "seg", ":", "self", ".", "compute_metrics", "(", ")", "self", ".", "segment", "(", "seg_eps", ",", "seg_min_time", ")", "if", "simplify", ":", "self", ".", "compute_metrics", "(", ")", "self", ".", "simplify", "(", "0", ",", "simplify_max_dist_error", ",", "simplify_max_speed_error", ")", "self", ".", "compute_metrics", "(", ")", "return", "self" ]
In-place, transformation of a track into a trip A trip is a more accurate depiction of reality than a track. For a track to become a trip it need to go through the following steps: + noise removal + smoothing + spatio-temporal segmentation + simplification At the end of these steps we have a less noisy, track that has less points, but that holds the same information. It's required that each segment has their metrics calculated or has been preprocessed. Args: name: An optional string with the name of the trip. If none is given, one will be generated by generateName Returns: This Track instance
[ "In", "-", "place", "transformation", "of", "a", "track", "into", "a", "trip" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/track.py#L137-L189
3,923
ruipgil/TrackToTrip
tracktotrip/track.py
Track.infer_transportation_modes
def infer_transportation_modes(self, dt_threshold=10): """In-place transportation inferring of segments Returns: This track """ self.segments = [ segment.infer_transportation_mode(dt_threshold=dt_threshold) for segment in self.segments ] return self
python
def infer_transportation_modes(self, dt_threshold=10): self.segments = [ segment.infer_transportation_mode(dt_threshold=dt_threshold) for segment in self.segments ] return self
[ "def", "infer_transportation_modes", "(", "self", ",", "dt_threshold", "=", "10", ")", ":", "self", ".", "segments", "=", "[", "segment", ".", "infer_transportation_mode", "(", "dt_threshold", "=", "dt_threshold", ")", "for", "segment", "in", "self", ".", "segments", "]", "return", "self" ]
In-place transportation inferring of segments Returns: This track
[ "In", "-", "place", "transportation", "inferring", "of", "segments" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/track.py#L191-L201
3,924
ruipgil/TrackToTrip
tracktotrip/track.py
Track.infer_location
def infer_location( self, location_query, max_distance, google_key, foursquare_client_id, foursquare_client_secret, limit ): """In-place location inferring of segments Returns: This track """ self.segments = [ segment.infer_location( location_query, max_distance, google_key, foursquare_client_id, foursquare_client_secret, limit ) for segment in self.segments ] return self
python
def infer_location( self, location_query, max_distance, google_key, foursquare_client_id, foursquare_client_secret, limit ): self.segments = [ segment.infer_location( location_query, max_distance, google_key, foursquare_client_id, foursquare_client_secret, limit ) for segment in self.segments ] return self
[ "def", "infer_location", "(", "self", ",", "location_query", ",", "max_distance", ",", "google_key", ",", "foursquare_client_id", ",", "foursquare_client_secret", ",", "limit", ")", ":", "self", ".", "segments", "=", "[", "segment", ".", "infer_location", "(", "location_query", ",", "max_distance", ",", "google_key", ",", "foursquare_client_id", ",", "foursquare_client_secret", ",", "limit", ")", "for", "segment", "in", "self", ".", "segments", "]", "return", "self" ]
In-place location inferring of segments Returns: This track
[ "In", "-", "place", "location", "inferring", "of", "segments" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/track.py#L204-L229
3,925
ruipgil/TrackToTrip
tracktotrip/track.py
Track.to_json
def to_json(self): """Converts track to a JSON serializable format Returns: Map with the name, and segments of the track. """ return { 'name': self.name, 'segments': [segment.to_json() for segment in self.segments], 'meta': self.meta }
python
def to_json(self): return { 'name': self.name, 'segments': [segment.to_json() for segment in self.segments], 'meta': self.meta }
[ "def", "to_json", "(", "self", ")", ":", "return", "{", "'name'", ":", "self", ".", "name", ",", "'segments'", ":", "[", "segment", ".", "to_json", "(", ")", "for", "segment", "in", "self", ".", "segments", "]", ",", "'meta'", ":", "self", ".", "meta", "}" ]
Converts track to a JSON serializable format Returns: Map with the name, and segments of the track.
[ "Converts", "track", "to", "a", "JSON", "serializable", "format" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/track.py#L231-L241
3,926
ruipgil/TrackToTrip
tracktotrip/track.py
Track.merge_and_fit
def merge_and_fit(self, track, pairings): """ Merges another track with this one, ordering the points based on a distance heuristic Args: track (:obj:`Track`): Track to merge with pairings Returns: :obj:`Segment`: self """ for (self_seg_index, track_seg_index, _) in pairings: self_s = self.segments[self_seg_index] ss_start = self_s.points[0] track_s = track.segments[track_seg_index] tt_start = track_s.points[0] tt_end = track_s.points[-1] d_start = ss_start.distance(tt_start) d_end = ss_start.distance(tt_end) if d_start > d_end: track_s = track_s.copy() track_s.points = list(reversed(track_s.points)) self_s.merge_and_fit(track_s) return self
python
def merge_and_fit(self, track, pairings): for (self_seg_index, track_seg_index, _) in pairings: self_s = self.segments[self_seg_index] ss_start = self_s.points[0] track_s = track.segments[track_seg_index] tt_start = track_s.points[0] tt_end = track_s.points[-1] d_start = ss_start.distance(tt_start) d_end = ss_start.distance(tt_end) if d_start > d_end: track_s = track_s.copy() track_s.points = list(reversed(track_s.points)) self_s.merge_and_fit(track_s) return self
[ "def", "merge_and_fit", "(", "self", ",", "track", ",", "pairings", ")", ":", "for", "(", "self_seg_index", ",", "track_seg_index", ",", "_", ")", "in", "pairings", ":", "self_s", "=", "self", ".", "segments", "[", "self_seg_index", "]", "ss_start", "=", "self_s", ".", "points", "[", "0", "]", "track_s", "=", "track", ".", "segments", "[", "track_seg_index", "]", "tt_start", "=", "track_s", ".", "points", "[", "0", "]", "tt_end", "=", "track_s", ".", "points", "[", "-", "1", "]", "d_start", "=", "ss_start", ".", "distance", "(", "tt_start", ")", "d_end", "=", "ss_start", ".", "distance", "(", "tt_end", ")", "if", "d_start", ">", "d_end", ":", "track_s", "=", "track_s", ".", "copy", "(", ")", "track_s", ".", "points", "=", "list", "(", "reversed", "(", "track_s", ".", "points", ")", ")", "self_s", ".", "merge_and_fit", "(", "track_s", ")", "return", "self" ]
Merges another track with this one, ordering the points based on a distance heuristic Args: track (:obj:`Track`): Track to merge with pairings Returns: :obj:`Segment`: self
[ "Merges", "another", "track", "with", "this", "one", "ordering", "the", "points", "based", "on", "a", "distance", "heuristic" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/track.py#L244-L270
3,927
ruipgil/TrackToTrip
tracktotrip/track.py
Track.get_point_index
def get_point_index(self, point): """ Gets of the closest first point Args: point (:obj:`Point`) Returns: (int, int): Segment id and point index in that segment """ for i, segment in enumerate(self.segments): idx = segment.getPointIndex(point) if idx != -1: return i, idx return -1, -1
python
def get_point_index(self, point): for i, segment in enumerate(self.segments): idx = segment.getPointIndex(point) if idx != -1: return i, idx return -1, -1
[ "def", "get_point_index", "(", "self", ",", "point", ")", ":", "for", "i", ",", "segment", "in", "enumerate", "(", "self", ".", "segments", ")", ":", "idx", "=", "segment", ".", "getPointIndex", "(", "point", ")", "if", "idx", "!=", "-", "1", ":", "return", "i", ",", "idx", "return", "-", "1", ",", "-", "1" ]
Gets of the closest first point Args: point (:obj:`Point`) Returns: (int, int): Segment id and point index in that segment
[ "Gets", "of", "the", "closest", "first", "point" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/track.py#L272-L284
3,928
ruipgil/TrackToTrip
tracktotrip/track.py
Track.bounds
def bounds(self, thr=0): """ Gets the bounds of this segment Returns: (float, float, float, float): Bounds, with min latitude, min longitude, max latitude and max longitude """ min_lat = float("inf") min_lon = float("inf") max_lat = -float("inf") max_lon = -float("inf") for segment in self.segments: milat, milon, malat, malon = segment.bounds(thr=thr) min_lat = min(milat, min_lat) min_lon = min(milon, min_lon) max_lat = max(malat, max_lat) max_lon = max(malon, max_lon) return min_lat, min_lon, max_lat, max_lon
python
def bounds(self, thr=0): min_lat = float("inf") min_lon = float("inf") max_lat = -float("inf") max_lon = -float("inf") for segment in self.segments: milat, milon, malat, malon = segment.bounds(thr=thr) min_lat = min(milat, min_lat) min_lon = min(milon, min_lon) max_lat = max(malat, max_lat) max_lon = max(malon, max_lon) return min_lat, min_lon, max_lat, max_lon
[ "def", "bounds", "(", "self", ",", "thr", "=", "0", ")", ":", "min_lat", "=", "float", "(", "\"inf\"", ")", "min_lon", "=", "float", "(", "\"inf\"", ")", "max_lat", "=", "-", "float", "(", "\"inf\"", ")", "max_lon", "=", "-", "float", "(", "\"inf\"", ")", "for", "segment", "in", "self", ".", "segments", ":", "milat", ",", "milon", ",", "malat", ",", "malon", "=", "segment", ".", "bounds", "(", "thr", "=", "thr", ")", "min_lat", "=", "min", "(", "milat", ",", "min_lat", ")", "min_lon", "=", "min", "(", "milon", ",", "min_lon", ")", "max_lat", "=", "max", "(", "malat", ",", "max_lat", ")", "max_lon", "=", "max", "(", "malon", ",", "max_lon", ")", "return", "min_lat", ",", "min_lon", ",", "max_lat", ",", "max_lon" ]
Gets the bounds of this segment Returns: (float, float, float, float): Bounds, with min latitude, min longitude, max latitude and max longitude
[ "Gets", "the", "bounds", "of", "this", "segment" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/track.py#L286-L303
3,929
ruipgil/TrackToTrip
tracktotrip/track.py
Track.similarity
def similarity(self, track): """ Compares two tracks based on their topology This method compares the given track against this instance. It only verifies if given track is close to this one, not the other way arround Args: track (:obj:`Track`) Returns: Two-tuple with global similarity between tracks and an array the similarity between segments """ idx = index.Index() i = 0 for i, segment in enumerate(self.segments): idx.insert(i, segment.bounds(), obj=segment) final_siml = [] final_diff = [] for i, segment in enumerate(track.segments): query = idx.intersection(segment.bounds(), objects=True) res_siml = [] res_diff = [] for result in query: siml, diff = segment_similarity(segment, result.object) res_siml.append(siml) res_diff.append((result.id, i, diff)) if len(res_siml) > 0: final_siml.append(max(res_siml)) final_diff.append(res_diff[np.argmax(res_siml)]) else: final_siml.append(0) final_diff.append([]) return np.mean(final_siml), final_diff
python
def similarity(self, track): idx = index.Index() i = 0 for i, segment in enumerate(self.segments): idx.insert(i, segment.bounds(), obj=segment) final_siml = [] final_diff = [] for i, segment in enumerate(track.segments): query = idx.intersection(segment.bounds(), objects=True) res_siml = [] res_diff = [] for result in query: siml, diff = segment_similarity(segment, result.object) res_siml.append(siml) res_diff.append((result.id, i, diff)) if len(res_siml) > 0: final_siml.append(max(res_siml)) final_diff.append(res_diff[np.argmax(res_siml)]) else: final_siml.append(0) final_diff.append([]) return np.mean(final_siml), final_diff
[ "def", "similarity", "(", "self", ",", "track", ")", ":", "idx", "=", "index", ".", "Index", "(", ")", "i", "=", "0", "for", "i", ",", "segment", "in", "enumerate", "(", "self", ".", "segments", ")", ":", "idx", ".", "insert", "(", "i", ",", "segment", ".", "bounds", "(", ")", ",", "obj", "=", "segment", ")", "final_siml", "=", "[", "]", "final_diff", "=", "[", "]", "for", "i", ",", "segment", "in", "enumerate", "(", "track", ".", "segments", ")", ":", "query", "=", "idx", ".", "intersection", "(", "segment", ".", "bounds", "(", ")", ",", "objects", "=", "True", ")", "res_siml", "=", "[", "]", "res_diff", "=", "[", "]", "for", "result", "in", "query", ":", "siml", ",", "diff", "=", "segment_similarity", "(", "segment", ",", "result", ".", "object", ")", "res_siml", ".", "append", "(", "siml", ")", "res_diff", ".", "append", "(", "(", "result", ".", "id", ",", "i", ",", "diff", ")", ")", "if", "len", "(", "res_siml", ")", ">", "0", ":", "final_siml", ".", "append", "(", "max", "(", "res_siml", ")", ")", "final_diff", ".", "append", "(", "res_diff", "[", "np", ".", "argmax", "(", "res_siml", ")", "]", ")", "else", ":", "final_siml", ".", "append", "(", "0", ")", "final_diff", ".", "append", "(", "[", "]", ")", "return", "np", ".", "mean", "(", "final_siml", ")", ",", "final_diff" ]
Compares two tracks based on their topology This method compares the given track against this instance. It only verifies if given track is close to this one, not the other way arround Args: track (:obj:`Track`) Returns: Two-tuple with global similarity between tracks and an array the similarity between segments
[ "Compares", "two", "tracks", "based", "on", "their", "topology" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/track.py#L316-L353
3,930
ruipgil/TrackToTrip
tracktotrip/track.py
Track.to_gpx
def to_gpx(self): """Converts track to a GPX format Uses GPXPY library as an intermediate format Returns: A string with the GPX/XML track """ gpx_segments = [] for segment in self.segments: gpx_points = [] for point in segment.points: time = '' if point.time: iso_time = point.time.isoformat().split('.')[0] time = '<time>%s</time>' % iso_time gpx_points.append( u'<trkpt lat="%f" lon="%f">%s</trkpt>' % (point.lat, point.lon, time) ) points = u'\n\t\t\t'.join(gpx_points) gpx_segments.append(u'\t\t<trkseg>\n\t\t\t%s\n\t\t</trkseg>' % points) segments = u'\t\n'.join(gpx_segments) content = [ u'<?xml version="1.0" encoding="UTF-8"?>', u'<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/0" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd" version="1.0" creator="GatherMySteps">', u'\t<trk>', segments, u'\t</trk>', u'</gpx>' ] return u'\n'.join(content)
python
def to_gpx(self): gpx_segments = [] for segment in self.segments: gpx_points = [] for point in segment.points: time = '' if point.time: iso_time = point.time.isoformat().split('.')[0] time = '<time>%s</time>' % iso_time gpx_points.append( u'<trkpt lat="%f" lon="%f">%s</trkpt>' % (point.lat, point.lon, time) ) points = u'\n\t\t\t'.join(gpx_points) gpx_segments.append(u'\t\t<trkseg>\n\t\t\t%s\n\t\t</trkseg>' % points) segments = u'\t\n'.join(gpx_segments) content = [ u'<?xml version="1.0" encoding="UTF-8"?>', u'<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/0" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd" version="1.0" creator="GatherMySteps">', u'\t<trk>', segments, u'\t</trk>', u'</gpx>' ] return u'\n'.join(content)
[ "def", "to_gpx", "(", "self", ")", ":", "gpx_segments", "=", "[", "]", "for", "segment", "in", "self", ".", "segments", ":", "gpx_points", "=", "[", "]", "for", "point", "in", "segment", ".", "points", ":", "time", "=", "''", "if", "point", ".", "time", ":", "iso_time", "=", "point", ".", "time", ".", "isoformat", "(", ")", ".", "split", "(", "'.'", ")", "[", "0", "]", "time", "=", "'<time>%s</time>'", "%", "iso_time", "gpx_points", ".", "append", "(", "u'<trkpt lat=\"%f\" lon=\"%f\">%s</trkpt>'", "%", "(", "point", ".", "lat", ",", "point", ".", "lon", ",", "time", ")", ")", "points", "=", "u'\\n\\t\\t\\t'", ".", "join", "(", "gpx_points", ")", "gpx_segments", ".", "append", "(", "u'\\t\\t<trkseg>\\n\\t\\t\\t%s\\n\\t\\t</trkseg>'", "%", "points", ")", "segments", "=", "u'\\t\\n'", ".", "join", "(", "gpx_segments", ")", "content", "=", "[", "u'<?xml version=\"1.0\" encoding=\"UTF-8\"?>'", ",", "u'<gpx xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.topografix.com/GPX/1/0\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\" version=\"1.0\" creator=\"GatherMySteps\">'", ",", "u'\\t<trk>'", ",", "segments", ",", "u'\\t</trk>'", ",", "u'</gpx>'", "]", "return", "u'\\n'", ".", "join", "(", "content", ")" ]
Converts track to a GPX format Uses GPXPY library as an intermediate format Returns: A string with the GPX/XML track
[ "Converts", "track", "to", "a", "GPX", "format" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/track.py#L367-L398
3,931
ruipgil/TrackToTrip
tracktotrip/track.py
Track.timezone
def timezone(self, timezone=0): """ Sets the timezone of the entire track Args: timezone (int): Timezone hour delta """ tz_dt = timedelta(hours=timezone) for segment in self.segments: for point in segment.points: point.time = point.time + tz_dt return self
python
def timezone(self, timezone=0): tz_dt = timedelta(hours=timezone) for segment in self.segments: for point in segment.points: point.time = point.time + tz_dt return self
[ "def", "timezone", "(", "self", ",", "timezone", "=", "0", ")", ":", "tz_dt", "=", "timedelta", "(", "hours", "=", "timezone", ")", "for", "segment", "in", "self", ".", "segments", ":", "for", "point", "in", "segment", ".", "points", ":", "point", ".", "time", "=", "point", ".", "time", "+", "tz_dt", "return", "self" ]
Sets the timezone of the entire track Args: timezone (int): Timezone hour delta
[ "Sets", "the", "timezone", "of", "the", "entire", "track" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/track.py#L400-L411
3,932
ruipgil/TrackToTrip
tracktotrip/track.py
Track.to_life
def to_life(self): """Converts track to LIFE format """ buff = "--%s\n" % self.segments[0].points[0].time.strftime("%Y_%m_%d") # buff += "--" + day # buff += "UTC+s" # if needed def military_time(time): """ Converts time to military time Args: time (:obj:`datetime.datetime`) Returns: str: Time in the format 1245 (12 hours and 45 minutes) """ return time.strftime("%H%M") def stay(buff, start, end, place): """ Creates a stay representation Args: start (:obj:`datetime.datetime` or str) end (:obj:`datetime.datetime` or str) place (:obj:`Location`) Returns: str """ if not isinstance(start, str): start = military_time(start) if not isinstance(end, str): end = military_time(end) return "%s\n%s-%s: %s" % (buff, start, end, place.label) def trip(buff, segment): """ Creates a trip representation Args: buff (str): buffer segment (:obj:`Segment`) Returns: str: buffer and trip representation """ trip = "%s-%s: %s -> %s" % ( military_time(segment.points[0].time), military_time(segment.points[-1].time), segment.location_from.label, segment.location_to.label ) t_modes = segment.transportation_modes if len(t_modes) == 1: trip = "%s [%s]" % (trip, t_modes[0]['label']) elif len(t_modes) > 1: modes = [] for mode in t_modes: trip_from = military_time(segment.points[mode['from']].time) trip_to = military_time(segment.points[mode['to']].time) modes.append(" %s-%s: [%s]" % (trip_from, trip_to, mode['label'])) trip = "%s\n%s" % (trip, "\n".join(modes)) return "%s\n%s" % (buff, trip) last = len(self.segments) - 1 for i, segment in enumerate(self.segments): if i == 0: buff = stay( buff, "0000", military_time(segment.points[0].time), segment.location_from ) buff = trip(buff, segment) if i is last: buff = stay( buff, military_time(segment.points[-1].time), "2359", segment.location_to ) else: next_seg = self.segments[i+1] buff = stay( buff, military_time(segment.points[-1].time), military_time(next_seg.points[0].time), segment.location_to ) return buff
python
def to_life(self): buff = "--%s\n" % self.segments[0].points[0].time.strftime("%Y_%m_%d") # buff += "--" + day # buff += "UTC+s" # if needed def military_time(time): """ Converts time to military time Args: time (:obj:`datetime.datetime`) Returns: str: Time in the format 1245 (12 hours and 45 minutes) """ return time.strftime("%H%M") def stay(buff, start, end, place): """ Creates a stay representation Args: start (:obj:`datetime.datetime` or str) end (:obj:`datetime.datetime` or str) place (:obj:`Location`) Returns: str """ if not isinstance(start, str): start = military_time(start) if not isinstance(end, str): end = military_time(end) return "%s\n%s-%s: %s" % (buff, start, end, place.label) def trip(buff, segment): """ Creates a trip representation Args: buff (str): buffer segment (:obj:`Segment`) Returns: str: buffer and trip representation """ trip = "%s-%s: %s -> %s" % ( military_time(segment.points[0].time), military_time(segment.points[-1].time), segment.location_from.label, segment.location_to.label ) t_modes = segment.transportation_modes if len(t_modes) == 1: trip = "%s [%s]" % (trip, t_modes[0]['label']) elif len(t_modes) > 1: modes = [] for mode in t_modes: trip_from = military_time(segment.points[mode['from']].time) trip_to = military_time(segment.points[mode['to']].time) modes.append(" %s-%s: [%s]" % (trip_from, trip_to, mode['label'])) trip = "%s\n%s" % (trip, "\n".join(modes)) return "%s\n%s" % (buff, trip) last = len(self.segments) - 1 for i, segment in enumerate(self.segments): if i == 0: buff = stay( buff, "0000", military_time(segment.points[0].time), segment.location_from ) buff = trip(buff, segment) if i is last: buff = stay( buff, military_time(segment.points[-1].time), "2359", segment.location_to ) else: next_seg = self.segments[i+1] buff = stay( buff, military_time(segment.points[-1].time), military_time(next_seg.points[0].time), segment.location_to ) return buff
[ "def", "to_life", "(", "self", ")", ":", "buff", "=", "\"--%s\\n\"", "%", "self", ".", "segments", "[", "0", "]", ".", "points", "[", "0", "]", ".", "time", ".", "strftime", "(", "\"%Y_%m_%d\"", ")", "# buff += \"--\" + day", "# buff += \"UTC+s\" # if needed", "def", "military_time", "(", "time", ")", ":", "\"\"\" Converts time to military time\n\n Args:\n time (:obj:`datetime.datetime`)\n Returns:\n str: Time in the format 1245 (12 hours and 45 minutes)\n \"\"\"", "return", "time", ".", "strftime", "(", "\"%H%M\"", ")", "def", "stay", "(", "buff", ",", "start", ",", "end", ",", "place", ")", ":", "\"\"\" Creates a stay representation\n\n Args:\n start (:obj:`datetime.datetime` or str)\n end (:obj:`datetime.datetime` or str)\n place (:obj:`Location`)\n Returns:\n str\n \"\"\"", "if", "not", "isinstance", "(", "start", ",", "str", ")", ":", "start", "=", "military_time", "(", "start", ")", "if", "not", "isinstance", "(", "end", ",", "str", ")", ":", "end", "=", "military_time", "(", "end", ")", "return", "\"%s\\n%s-%s: %s\"", "%", "(", "buff", ",", "start", ",", "end", ",", "place", ".", "label", ")", "def", "trip", "(", "buff", ",", "segment", ")", ":", "\"\"\" Creates a trip representation\n\n Args:\n buff (str): buffer\n segment (:obj:`Segment`)\n Returns:\n str: buffer and trip representation\n \"\"\"", "trip", "=", "\"%s-%s: %s -> %s\"", "%", "(", "military_time", "(", "segment", ".", "points", "[", "0", "]", ".", "time", ")", ",", "military_time", "(", "segment", ".", "points", "[", "-", "1", "]", ".", "time", ")", ",", "segment", ".", "location_from", ".", "label", ",", "segment", ".", "location_to", ".", "label", ")", "t_modes", "=", "segment", ".", "transportation_modes", "if", "len", "(", "t_modes", ")", "==", "1", ":", "trip", "=", "\"%s [%s]\"", "%", "(", "trip", ",", "t_modes", "[", "0", "]", "[", "'label'", "]", ")", "elif", "len", "(", "t_modes", ")", ">", "1", ":", "modes", "=", "[", "]", "for", "mode", "in", "t_modes", ":", "trip_from", "=", "military_time", "(", "segment", ".", "points", "[", "mode", "[", "'from'", "]", "]", ".", "time", ")", "trip_to", "=", "military_time", "(", "segment", ".", "points", "[", "mode", "[", "'to'", "]", "]", ".", "time", ")", "modes", ".", "append", "(", "\" %s-%s: [%s]\"", "%", "(", "trip_from", ",", "trip_to", ",", "mode", "[", "'label'", "]", ")", ")", "trip", "=", "\"%s\\n%s\"", "%", "(", "trip", ",", "\"\\n\"", ".", "join", "(", "modes", ")", ")", "return", "\"%s\\n%s\"", "%", "(", "buff", ",", "trip", ")", "last", "=", "len", "(", "self", ".", "segments", ")", "-", "1", "for", "i", ",", "segment", "in", "enumerate", "(", "self", ".", "segments", ")", ":", "if", "i", "==", "0", ":", "buff", "=", "stay", "(", "buff", ",", "\"0000\"", ",", "military_time", "(", "segment", ".", "points", "[", "0", "]", ".", "time", ")", ",", "segment", ".", "location_from", ")", "buff", "=", "trip", "(", "buff", ",", "segment", ")", "if", "i", "is", "last", ":", "buff", "=", "stay", "(", "buff", ",", "military_time", "(", "segment", ".", "points", "[", "-", "1", "]", ".", "time", ")", ",", "\"2359\"", ",", "segment", ".", "location_to", ")", "else", ":", "next_seg", "=", "self", ".", "segments", "[", "i", "+", "1", "]", "buff", "=", "stay", "(", "buff", ",", "military_time", "(", "segment", ".", "points", "[", "-", "1", "]", ".", "time", ")", ",", "military_time", "(", "next_seg", ".", "points", "[", "0", "]", ".", "time", ")", ",", "segment", ".", "location_to", ")", "return", "buff" ]
Converts track to LIFE format
[ "Converts", "track", "to", "LIFE", "format" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/track.py#L413-L502
3,933
ruipgil/TrackToTrip
tracktotrip/track.py
Track.from_gpx
def from_gpx(file_path): """ Creates a Track from a GPX file. No preprocessing is done. Arguments: file_path (str): file path and name to the GPX file Return: :obj:`list` of :obj:`Track` """ gpx = gpxpy.parse(open(file_path, 'r')) file_name = basename(file_path) tracks = [] for i, track in enumerate(gpx.tracks): segments = [] for segment in track.segments: segments.append(Segment.from_gpx(segment)) if len(gpx.tracks) > 1: name = file_name + "_" + str(i) else: name = file_name tracks.append(Track(name, segments)) return tracks
python
def from_gpx(file_path): gpx = gpxpy.parse(open(file_path, 'r')) file_name = basename(file_path) tracks = [] for i, track in enumerate(gpx.tracks): segments = [] for segment in track.segments: segments.append(Segment.from_gpx(segment)) if len(gpx.tracks) > 1: name = file_name + "_" + str(i) else: name = file_name tracks.append(Track(name, segments)) return tracks
[ "def", "from_gpx", "(", "file_path", ")", ":", "gpx", "=", "gpxpy", ".", "parse", "(", "open", "(", "file_path", ",", "'r'", ")", ")", "file_name", "=", "basename", "(", "file_path", ")", "tracks", "=", "[", "]", "for", "i", ",", "track", "in", "enumerate", "(", "gpx", ".", "tracks", ")", ":", "segments", "=", "[", "]", "for", "segment", "in", "track", ".", "segments", ":", "segments", ".", "append", "(", "Segment", ".", "from_gpx", "(", "segment", ")", ")", "if", "len", "(", "gpx", ".", "tracks", ")", ">", "1", ":", "name", "=", "file_name", "+", "\"_\"", "+", "str", "(", "i", ")", "else", ":", "name", "=", "file_name", "tracks", ".", "append", "(", "Track", "(", "name", ",", "segments", ")", ")", "return", "tracks" ]
Creates a Track from a GPX file. No preprocessing is done. Arguments: file_path (str): file path and name to the GPX file Return: :obj:`list` of :obj:`Track`
[ "Creates", "a", "Track", "from", "a", "GPX", "file", "." ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/track.py#L505-L530
3,934
ruipgil/TrackToTrip
tracktotrip/track.py
Track.from_json
def from_json(json): """Creates a Track from a JSON file. No preprocessing is done. Arguments: json: map with the keys: name (optional) and segments. Return: A track instance """ segments = [Segment.from_json(s) for s in json['segments']] return Track(json['name'], segments).compute_metrics()
python
def from_json(json): segments = [Segment.from_json(s) for s in json['segments']] return Track(json['name'], segments).compute_metrics()
[ "def", "from_json", "(", "json", ")", ":", "segments", "=", "[", "Segment", ".", "from_json", "(", "s", ")", "for", "s", "in", "json", "[", "'segments'", "]", "]", "return", "Track", "(", "json", "[", "'name'", "]", ",", "segments", ")", ".", "compute_metrics", "(", ")" ]
Creates a Track from a JSON file. No preprocessing is done. Arguments: json: map with the keys: name (optional) and segments. Return: A track instance
[ "Creates", "a", "Track", "from", "a", "JSON", "file", "." ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/track.py#L533-L544
3,935
ruipgil/TrackToTrip
tracktotrip/similarity.py
line
def line(p1, p2): """Creates a line from two points From http://stackoverflow.com/a/20679579 Args: p1 ([float, float]): x and y coordinates p2 ([float, float]): x and y coordinates Returns: (float, float, float): x, y and _ """ A = (p1[1] - p2[1]) B = (p2[0] - p1[0]) C = (p1[0]*p2[1] - p2[0]*p1[1]) return A, B, -C
python
def line(p1, p2): A = (p1[1] - p2[1]) B = (p2[0] - p1[0]) C = (p1[0]*p2[1] - p2[0]*p1[1]) return A, B, -C
[ "def", "line", "(", "p1", ",", "p2", ")", ":", "A", "=", "(", "p1", "[", "1", "]", "-", "p2", "[", "1", "]", ")", "B", "=", "(", "p2", "[", "0", "]", "-", "p1", "[", "0", "]", ")", "C", "=", "(", "p1", "[", "0", "]", "*", "p2", "[", "1", "]", "-", "p2", "[", "0", "]", "*", "p1", "[", "1", "]", ")", "return", "A", ",", "B", ",", "-", "C" ]
Creates a line from two points From http://stackoverflow.com/a/20679579 Args: p1 ([float, float]): x and y coordinates p2 ([float, float]): x and y coordinates Returns: (float, float, float): x, y and _
[ "Creates", "a", "line", "from", "two", "points" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/similarity.py#L55-L69
3,936
ruipgil/TrackToTrip
tracktotrip/similarity.py
intersection
def intersection(L1, L2): """Intersects two line segments Args: L1 ([float, float]): x and y coordinates L2 ([float, float]): x and y coordinates Returns: bool: if they intersect (float, float): x and y of intersection, if they do """ D = L1[0] * L2[1] - L1[1] * L2[0] Dx = L1[2] * L2[1] - L1[1] * L2[2] Dy = L1[0] * L2[2] - L1[2] * L2[0] if D != 0: x = Dx / D y = Dy / D return x, y else: return False
python
def intersection(L1, L2): D = L1[0] * L2[1] - L1[1] * L2[0] Dx = L1[2] * L2[1] - L1[1] * L2[2] Dy = L1[0] * L2[2] - L1[2] * L2[0] if D != 0: x = Dx / D y = Dy / D return x, y else: return False
[ "def", "intersection", "(", "L1", ",", "L2", ")", ":", "D", "=", "L1", "[", "0", "]", "*", "L2", "[", "1", "]", "-", "L1", "[", "1", "]", "*", "L2", "[", "0", "]", "Dx", "=", "L1", "[", "2", "]", "*", "L2", "[", "1", "]", "-", "L1", "[", "1", "]", "*", "L2", "[", "2", "]", "Dy", "=", "L1", "[", "0", "]", "*", "L2", "[", "2", "]", "-", "L1", "[", "2", "]", "*", "L2", "[", "0", "]", "if", "D", "!=", "0", ":", "x", "=", "Dx", "/", "D", "y", "=", "Dy", "/", "D", "return", "x", ",", "y", "else", ":", "return", "False" ]
Intersects two line segments Args: L1 ([float, float]): x and y coordinates L2 ([float, float]): x and y coordinates Returns: bool: if they intersect (float, float): x and y of intersection, if they do
[ "Intersects", "two", "line", "segments" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/similarity.py#L71-L89
3,937
ruipgil/TrackToTrip
tracktotrip/similarity.py
closest_point
def closest_point(a, b, p): """Finds closest point in a line segment Args: a ([float, float]): x and y coordinates. Line start b ([float, float]): x and y coordinates. Line end p ([float, float]): x and y coordinates. Point to find in the segment Returns: (float, float): x and y coordinates of the closest point """ ap = [p[0]-a[0], p[1]-a[1]] ab = [b[0]-a[0], b[1]-a[1]] mag = float(ab[0]**2 + ab[1]**2) proj = dot(ap, ab) if mag ==0 : dist = 0 else: dist = proj / mag if dist < 0: return [a[0], a[1]] elif dist > 1: return [b[0], b[1]] else: return [a[0] + ab[0] * dist, a[1] + ab[1] * dist]
python
def closest_point(a, b, p): ap = [p[0]-a[0], p[1]-a[1]] ab = [b[0]-a[0], b[1]-a[1]] mag = float(ab[0]**2 + ab[1]**2) proj = dot(ap, ab) if mag ==0 : dist = 0 else: dist = proj / mag if dist < 0: return [a[0], a[1]] elif dist > 1: return [b[0], b[1]] else: return [a[0] + ab[0] * dist, a[1] + ab[1] * dist]
[ "def", "closest_point", "(", "a", ",", "b", ",", "p", ")", ":", "ap", "=", "[", "p", "[", "0", "]", "-", "a", "[", "0", "]", ",", "p", "[", "1", "]", "-", "a", "[", "1", "]", "]", "ab", "=", "[", "b", "[", "0", "]", "-", "a", "[", "0", "]", ",", "b", "[", "1", "]", "-", "a", "[", "1", "]", "]", "mag", "=", "float", "(", "ab", "[", "0", "]", "**", "2", "+", "ab", "[", "1", "]", "**", "2", ")", "proj", "=", "dot", "(", "ap", ",", "ab", ")", "if", "mag", "==", "0", ":", "dist", "=", "0", "else", ":", "dist", "=", "proj", "/", "mag", "if", "dist", "<", "0", ":", "return", "[", "a", "[", "0", "]", ",", "a", "[", "1", "]", "]", "elif", "dist", ">", "1", ":", "return", "[", "b", "[", "0", "]", ",", "b", "[", "1", "]", "]", "else", ":", "return", "[", "a", "[", "0", "]", "+", "ab", "[", "0", "]", "*", "dist", ",", "a", "[", "1", "]", "+", "ab", "[", "1", "]", "*", "dist", "]" ]
Finds closest point in a line segment Args: a ([float, float]): x and y coordinates. Line start b ([float, float]): x and y coordinates. Line end p ([float, float]): x and y coordinates. Point to find in the segment Returns: (float, float): x and y coordinates of the closest point
[ "Finds", "closest", "point", "in", "a", "line", "segment" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/similarity.py#L113-L136
3,938
ruipgil/TrackToTrip
tracktotrip/similarity.py
distance_to_line
def distance_to_line(a, b, p): """Closest distance between a line segment and a point Args: a ([float, float]): x and y coordinates. Line start b ([float, float]): x and y coordinates. Line end p ([float, float]): x and y coordinates. Point to compute the distance Returns: float """ return distance(closest_point(a, b, p), p)
python
def distance_to_line(a, b, p): return distance(closest_point(a, b, p), p)
[ "def", "distance_to_line", "(", "a", ",", "b", ",", "p", ")", ":", "return", "distance", "(", "closest_point", "(", "a", ",", "b", ",", "p", ")", ",", "p", ")" ]
Closest distance between a line segment and a point Args: a ([float, float]): x and y coordinates. Line start b ([float, float]): x and y coordinates. Line end p ([float, float]): x and y coordinates. Point to compute the distance Returns: float
[ "Closest", "distance", "between", "a", "line", "segment", "and", "a", "point" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/similarity.py#L138-L148
3,939
ruipgil/TrackToTrip
tracktotrip/similarity.py
distance_similarity
def distance_similarity(a, b, p, T=CLOSE_DISTANCE_THRESHOLD): """Computes the distance similarity between a line segment and a point Args: a ([float, float]): x and y coordinates. Line start b ([float, float]): x and y coordinates. Line end p ([float, float]): x and y coordinates. Point to compute the distance Returns: float: between 0 and 1. Where 1 is very similar and 0 is completely different """ d = distance_to_line(a, b, p) r = (-1/float(T)) * abs(d) + 1 return r if r > 0 else 0
python
def distance_similarity(a, b, p, T=CLOSE_DISTANCE_THRESHOLD): d = distance_to_line(a, b, p) r = (-1/float(T)) * abs(d) + 1 return r if r > 0 else 0
[ "def", "distance_similarity", "(", "a", ",", "b", ",", "p", ",", "T", "=", "CLOSE_DISTANCE_THRESHOLD", ")", ":", "d", "=", "distance_to_line", "(", "a", ",", "b", ",", "p", ")", "r", "=", "(", "-", "1", "/", "float", "(", "T", ")", ")", "*", "abs", "(", "d", ")", "+", "1", "return", "r", "if", "r", ">", "0", "else", "0" ]
Computes the distance similarity between a line segment and a point Args: a ([float, float]): x and y coordinates. Line start b ([float, float]): x and y coordinates. Line end p ([float, float]): x and y coordinates. Point to compute the distance Returns: float: between 0 and 1. Where 1 is very similar and 0 is completely different
[ "Computes", "the", "distance", "similarity", "between", "a", "line", "segment", "and", "a", "point" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/similarity.py#L151-L165
3,940
ruipgil/TrackToTrip
tracktotrip/similarity.py
line_distance_similarity
def line_distance_similarity(p1a, p1b, p2a, p2b, T=CLOSE_DISTANCE_THRESHOLD): """Line distance similarity between two line segments Args: p1a ([float, float]): x and y coordinates. Line A start p1b ([float, float]): x and y coordinates. Line A end p2a ([float, float]): x and y coordinates. Line B start p2b ([float, float]): x and y coordinates. Line B end Returns: float: between 0 and 1. Where 1 is very similar and 0 is completely different """ d1 = distance_similarity(p1a, p1b, p2a, T=T) d2 = distance_similarity(p1a, p1b, p2b, T=T) return abs(d1 + d2) * 0.5
python
def line_distance_similarity(p1a, p1b, p2a, p2b, T=CLOSE_DISTANCE_THRESHOLD): d1 = distance_similarity(p1a, p1b, p2a, T=T) d2 = distance_similarity(p1a, p1b, p2b, T=T) return abs(d1 + d2) * 0.5
[ "def", "line_distance_similarity", "(", "p1a", ",", "p1b", ",", "p2a", ",", "p2b", ",", "T", "=", "CLOSE_DISTANCE_THRESHOLD", ")", ":", "d1", "=", "distance_similarity", "(", "p1a", ",", "p1b", ",", "p2a", ",", "T", "=", "T", ")", "d2", "=", "distance_similarity", "(", "p1a", ",", "p1b", ",", "p2b", ",", "T", "=", "T", ")", "return", "abs", "(", "d1", "+", "d2", ")", "*", "0.5" ]
Line distance similarity between two line segments Args: p1a ([float, float]): x and y coordinates. Line A start p1b ([float, float]): x and y coordinates. Line A end p2a ([float, float]): x and y coordinates. Line B start p2b ([float, float]): x and y coordinates. Line B end Returns: float: between 0 and 1. Where 1 is very similar and 0 is completely different
[ "Line", "distance", "similarity", "between", "two", "line", "segments" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/similarity.py#L167-L180
3,941
ruipgil/TrackToTrip
tracktotrip/similarity.py
line_similarity
def line_similarity(p1a, p1b, p2a, p2b, T=CLOSE_DISTANCE_THRESHOLD): """Similarity between two lines Args: p1a ([float, float]): x and y coordinates. Line A start p1b ([float, float]): x and y coordinates. Line A end p2a ([float, float]): x and y coordinates. Line B start p2b ([float, float]): x and y coordinates. Line B end Returns: float: between 0 and 1. Where 1 is very similar and 0 is completely different """ d = line_distance_similarity(p1a, p1b, p2a, p2b, T=T) a = abs(angle_similarity(normalize(line(p1a, p1b)), normalize(line(p2a, p2b)))) return d * a
python
def line_similarity(p1a, p1b, p2a, p2b, T=CLOSE_DISTANCE_THRESHOLD): d = line_distance_similarity(p1a, p1b, p2a, p2b, T=T) a = abs(angle_similarity(normalize(line(p1a, p1b)), normalize(line(p2a, p2b)))) return d * a
[ "def", "line_similarity", "(", "p1a", ",", "p1b", ",", "p2a", ",", "p2b", ",", "T", "=", "CLOSE_DISTANCE_THRESHOLD", ")", ":", "d", "=", "line_distance_similarity", "(", "p1a", ",", "p1b", ",", "p2a", ",", "p2b", ",", "T", "=", "T", ")", "a", "=", "abs", "(", "angle_similarity", "(", "normalize", "(", "line", "(", "p1a", ",", "p1b", ")", ")", ",", "normalize", "(", "line", "(", "p2a", ",", "p2b", ")", ")", ")", ")", "return", "d", "*", "a" ]
Similarity between two lines Args: p1a ([float, float]): x and y coordinates. Line A start p1b ([float, float]): x and y coordinates. Line A end p2a ([float, float]): x and y coordinates. Line B start p2b ([float, float]): x and y coordinates. Line B end Returns: float: between 0 and 1. Where 1 is very similar and 0 is completely different
[ "Similarity", "between", "two", "lines" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/similarity.py#L182-L195
3,942
ruipgil/TrackToTrip
tracktotrip/similarity.py
bounding_box_from
def bounding_box_from(points, i, i1, thr): """Creates bounding box for a line segment Args: points (:obj:`list` of :obj:`Point`) i (int): Line segment start, index in points array i1 (int): Line segment end, index in points array Returns: (float, float, float, float): with bounding box min x, min y, max x and max y """ pi = points[i] pi1 = points[i1] min_lat = min(pi.lat, pi1.lat) min_lon = min(pi.lon, pi1.lon) max_lat = max(pi.lat, pi1.lat) max_lon = max(pi.lon, pi1.lon) return min_lat-thr, min_lon-thr, max_lat+thr, max_lon+thr
python
def bounding_box_from(points, i, i1, thr): pi = points[i] pi1 = points[i1] min_lat = min(pi.lat, pi1.lat) min_lon = min(pi.lon, pi1.lon) max_lat = max(pi.lat, pi1.lat) max_lon = max(pi.lon, pi1.lon) return min_lat-thr, min_lon-thr, max_lat+thr, max_lon+thr
[ "def", "bounding_box_from", "(", "points", ",", "i", ",", "i1", ",", "thr", ")", ":", "pi", "=", "points", "[", "i", "]", "pi1", "=", "points", "[", "i1", "]", "min_lat", "=", "min", "(", "pi", ".", "lat", ",", "pi1", ".", "lat", ")", "min_lon", "=", "min", "(", "pi", ".", "lon", ",", "pi1", ".", "lon", ")", "max_lat", "=", "max", "(", "pi", ".", "lat", ",", "pi1", ".", "lat", ")", "max_lon", "=", "max", "(", "pi", ".", "lon", ",", "pi1", ".", "lon", ")", "return", "min_lat", "-", "thr", ",", "min_lon", "-", "thr", ",", "max_lat", "+", "thr", ",", "max_lon", "+", "thr" ]
Creates bounding box for a line segment Args: points (:obj:`list` of :obj:`Point`) i (int): Line segment start, index in points array i1 (int): Line segment end, index in points array Returns: (float, float, float, float): with bounding box min x, min y, max x and max y
[ "Creates", "bounding", "box", "for", "a", "line", "segment" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/similarity.py#L197-L215
3,943
ruipgil/TrackToTrip
tracktotrip/similarity.py
segment_similarity
def segment_similarity(A, B, T=CLOSE_DISTANCE_THRESHOLD): """Computes the similarity between two segments Args: A (:obj:`Segment`) B (:obj:`Segment`) Returns: float: between 0 and 1. Where 1 is very similar and 0 is completely different """ l_a = len(A.points) l_b = len(B.points) idx = index.Index() dex = 0 for i in range(l_a-1): idx.insert(dex, bounding_box_from(A.points, i, i+1, T), obj=[A.points[i], A.points[i+1]]) dex = dex + 1 prox_acc = [] for i in range(l_b-1): ti = B.points[i].gen2arr() ti1 = B.points[i+1].gen2arr() bb = bounding_box_from(B.points, i, i+1, T) intersects = idx.intersection(bb, objects=True) n_prox = [] i_prox = 0 a = 0 for x in intersects: a = a + 1 pi = x.object[0].gen2arr() pi1 = x.object[1].gen2arr() prox = line_similarity(ti, ti1, pi, pi1, T) i_prox = i_prox + prox n_prox.append(prox) if a != 0: prox_acc.append(i_prox / a) # prox_acc.append(max(n_prox)) else: prox_acc.append(0) return np.mean(prox_acc), prox_acc
python
def segment_similarity(A, B, T=CLOSE_DISTANCE_THRESHOLD): l_a = len(A.points) l_b = len(B.points) idx = index.Index() dex = 0 for i in range(l_a-1): idx.insert(dex, bounding_box_from(A.points, i, i+1, T), obj=[A.points[i], A.points[i+1]]) dex = dex + 1 prox_acc = [] for i in range(l_b-1): ti = B.points[i].gen2arr() ti1 = B.points[i+1].gen2arr() bb = bounding_box_from(B.points, i, i+1, T) intersects = idx.intersection(bb, objects=True) n_prox = [] i_prox = 0 a = 0 for x in intersects: a = a + 1 pi = x.object[0].gen2arr() pi1 = x.object[1].gen2arr() prox = line_similarity(ti, ti1, pi, pi1, T) i_prox = i_prox + prox n_prox.append(prox) if a != 0: prox_acc.append(i_prox / a) # prox_acc.append(max(n_prox)) else: prox_acc.append(0) return np.mean(prox_acc), prox_acc
[ "def", "segment_similarity", "(", "A", ",", "B", ",", "T", "=", "CLOSE_DISTANCE_THRESHOLD", ")", ":", "l_a", "=", "len", "(", "A", ".", "points", ")", "l_b", "=", "len", "(", "B", ".", "points", ")", "idx", "=", "index", ".", "Index", "(", ")", "dex", "=", "0", "for", "i", "in", "range", "(", "l_a", "-", "1", ")", ":", "idx", ".", "insert", "(", "dex", ",", "bounding_box_from", "(", "A", ".", "points", ",", "i", ",", "i", "+", "1", ",", "T", ")", ",", "obj", "=", "[", "A", ".", "points", "[", "i", "]", ",", "A", ".", "points", "[", "i", "+", "1", "]", "]", ")", "dex", "=", "dex", "+", "1", "prox_acc", "=", "[", "]", "for", "i", "in", "range", "(", "l_b", "-", "1", ")", ":", "ti", "=", "B", ".", "points", "[", "i", "]", ".", "gen2arr", "(", ")", "ti1", "=", "B", ".", "points", "[", "i", "+", "1", "]", ".", "gen2arr", "(", ")", "bb", "=", "bounding_box_from", "(", "B", ".", "points", ",", "i", ",", "i", "+", "1", ",", "T", ")", "intersects", "=", "idx", ".", "intersection", "(", "bb", ",", "objects", "=", "True", ")", "n_prox", "=", "[", "]", "i_prox", "=", "0", "a", "=", "0", "for", "x", "in", "intersects", ":", "a", "=", "a", "+", "1", "pi", "=", "x", ".", "object", "[", "0", "]", ".", "gen2arr", "(", ")", "pi1", "=", "x", ".", "object", "[", "1", "]", ".", "gen2arr", "(", ")", "prox", "=", "line_similarity", "(", "ti", ",", "ti1", ",", "pi", ",", "pi1", ",", "T", ")", "i_prox", "=", "i_prox", "+", "prox", "n_prox", ".", "append", "(", "prox", ")", "if", "a", "!=", "0", ":", "prox_acc", ".", "append", "(", "i_prox", "/", "a", ")", "# prox_acc.append(max(n_prox))", "else", ":", "prox_acc", ".", "append", "(", "0", ")", "return", "np", ".", "mean", "(", "prox_acc", ")", ",", "prox_acc" ]
Computes the similarity between two segments Args: A (:obj:`Segment`) B (:obj:`Segment`) Returns: float: between 0 and 1. Where 1 is very similar and 0 is completely different
[ "Computes", "the", "similarity", "between", "two", "segments" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/similarity.py#L217-L259
3,944
ruipgil/TrackToTrip
tracktotrip/similarity.py
sort_segment_points
def sort_segment_points(Aps, Bps): """Takes two line segments and sorts all their points, so that they form a continuous path Args: Aps: Array of tracktotrip.Point Bps: Array of tracktotrip.Point Returns: Array with points ordered """ mid = [] j = 0 mid.append(Aps[0]) for i in range(len(Aps)-1): dist = distance_tt_point(Aps[i], Aps[i+1]) for m in range(j, len(Bps)): distm = distance_tt_point(Aps[i], Bps[m]) if dist > distm: direction = dot(normalize(line(Aps[i].gen2arr(), Aps[i+1].gen2arr())), normalize(Bps[m].gen2arr())) if direction > 0: j = m + 1 mid.append(Bps[m]) break mid.append(Aps[i+1]) for m in range(j, len(Bps)): mid.append(Bps[m]) return mid
python
def sort_segment_points(Aps, Bps): mid = [] j = 0 mid.append(Aps[0]) for i in range(len(Aps)-1): dist = distance_tt_point(Aps[i], Aps[i+1]) for m in range(j, len(Bps)): distm = distance_tt_point(Aps[i], Bps[m]) if dist > distm: direction = dot(normalize(line(Aps[i].gen2arr(), Aps[i+1].gen2arr())), normalize(Bps[m].gen2arr())) if direction > 0: j = m + 1 mid.append(Bps[m]) break mid.append(Aps[i+1]) for m in range(j, len(Bps)): mid.append(Bps[m]) return mid
[ "def", "sort_segment_points", "(", "Aps", ",", "Bps", ")", ":", "mid", "=", "[", "]", "j", "=", "0", "mid", ".", "append", "(", "Aps", "[", "0", "]", ")", "for", "i", "in", "range", "(", "len", "(", "Aps", ")", "-", "1", ")", ":", "dist", "=", "distance_tt_point", "(", "Aps", "[", "i", "]", ",", "Aps", "[", "i", "+", "1", "]", ")", "for", "m", "in", "range", "(", "j", ",", "len", "(", "Bps", ")", ")", ":", "distm", "=", "distance_tt_point", "(", "Aps", "[", "i", "]", ",", "Bps", "[", "m", "]", ")", "if", "dist", ">", "distm", ":", "direction", "=", "dot", "(", "normalize", "(", "line", "(", "Aps", "[", "i", "]", ".", "gen2arr", "(", ")", ",", "Aps", "[", "i", "+", "1", "]", ".", "gen2arr", "(", ")", ")", ")", ",", "normalize", "(", "Bps", "[", "m", "]", ".", "gen2arr", "(", ")", ")", ")", "if", "direction", ">", "0", ":", "j", "=", "m", "+", "1", "mid", ".", "append", "(", "Bps", "[", "m", "]", ")", "break", "mid", ".", "append", "(", "Aps", "[", "i", "+", "1", "]", ")", "for", "m", "in", "range", "(", "j", ",", "len", "(", "Bps", ")", ")", ":", "mid", ".", "append", "(", "Bps", "[", "m", "]", ")", "return", "mid" ]
Takes two line segments and sorts all their points, so that they form a continuous path Args: Aps: Array of tracktotrip.Point Bps: Array of tracktotrip.Point Returns: Array with points ordered
[ "Takes", "two", "line", "segments", "and", "sorts", "all", "their", "points", "so", "that", "they", "form", "a", "continuous", "path" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/similarity.py#L261-L288
3,945
ruipgil/TrackToTrip
tracktotrip/point.py
distance
def distance(latitude_1, longitude_1, elevation_1, latitude_2, longitude_2, elevation_2, haversine=None): """ Distance between two points """ # If points too distant -- compute haversine distance: if haversine or (abs(latitude_1 - latitude_2) > .2 or abs(longitude_1 - longitude_2) > .2): return haversine_distance(latitude_1, longitude_1, latitude_2, longitude_2) coef = math.cos(latitude_1 / 180. * math.pi) #pylint: disable=invalid-name x = latitude_1 - latitude_2 y = (longitude_1 - longitude_2) * coef distance_2d = math.sqrt(x * x + y * y) * ONE_DEGREE if elevation_1 is None or elevation_2 is None or elevation_1 == elevation_2: return distance_2d return math.sqrt(distance_2d ** 2 + (elevation_1 - elevation_2) ** 2)
python
def distance(latitude_1, longitude_1, elevation_1, latitude_2, longitude_2, elevation_2, haversine=None): # If points too distant -- compute haversine distance: if haversine or (abs(latitude_1 - latitude_2) > .2 or abs(longitude_1 - longitude_2) > .2): return haversine_distance(latitude_1, longitude_1, latitude_2, longitude_2) coef = math.cos(latitude_1 / 180. * math.pi) #pylint: disable=invalid-name x = latitude_1 - latitude_2 y = (longitude_1 - longitude_2) * coef distance_2d = math.sqrt(x * x + y * y) * ONE_DEGREE if elevation_1 is None or elevation_2 is None or elevation_1 == elevation_2: return distance_2d return math.sqrt(distance_2d ** 2 + (elevation_1 - elevation_2) ** 2)
[ "def", "distance", "(", "latitude_1", ",", "longitude_1", ",", "elevation_1", ",", "latitude_2", ",", "longitude_2", ",", "elevation_2", ",", "haversine", "=", "None", ")", ":", "# If points too distant -- compute haversine distance:", "if", "haversine", "or", "(", "abs", "(", "latitude_1", "-", "latitude_2", ")", ">", ".2", "or", "abs", "(", "longitude_1", "-", "longitude_2", ")", ">", ".2", ")", ":", "return", "haversine_distance", "(", "latitude_1", ",", "longitude_1", ",", "latitude_2", ",", "longitude_2", ")", "coef", "=", "math", ".", "cos", "(", "latitude_1", "/", "180.", "*", "math", ".", "pi", ")", "#pylint: disable=invalid-name", "x", "=", "latitude_1", "-", "latitude_2", "y", "=", "(", "longitude_1", "-", "longitude_2", ")", "*", "coef", "distance_2d", "=", "math", ".", "sqrt", "(", "x", "*", "x", "+", "y", "*", "y", ")", "*", "ONE_DEGREE", "if", "elevation_1", "is", "None", "or", "elevation_2", "is", "None", "or", "elevation_1", "==", "elevation_2", ":", "return", "distance_2d", "return", "math", ".", "sqrt", "(", "distance_2d", "**", "2", "+", "(", "elevation_1", "-", "elevation_2", ")", "**", "2", ")" ]
Distance between two points
[ "Distance", "between", "two", "points" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/point.py#L184-L202
3,946
ruipgil/TrackToTrip
tracktotrip/point.py
Point.distance
def distance(self, other): """ Distance between points Args: other (:obj:`Point`) Returns: float: Distance in km """ return distance(self.lat, self.lon, None, other.lat, other.lon, None)
python
def distance(self, other): return distance(self.lat, self.lon, None, other.lat, other.lon, None)
[ "def", "distance", "(", "self", ",", "other", ")", ":", "return", "distance", "(", "self", ".", "lat", ",", "self", ".", "lon", ",", "None", ",", "other", ".", "lat", ",", "other", ".", "lon", ",", "None", ")" ]
Distance between points Args: other (:obj:`Point`) Returns: float: Distance in km
[ "Distance", "between", "points" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/point.py#L57-L65
3,947
ruipgil/TrackToTrip
tracktotrip/point.py
Point.compute_metrics
def compute_metrics(self, previous): """ Computes the metrics of this point Computes and updates the dt, vel and acc attributes. Args: previous (:obj:`Point`): Point before Returns: :obj:`Point`: Self """ delta_t = self.time_difference(previous) delta_x = self.distance(previous) vel = 0 delta_v = 0 acc = 0 if delta_t != 0: vel = delta_x/delta_t delta_v = vel - previous.vel acc = delta_v/delta_t self.dt = delta_t self.dx = delta_x self.acc = acc self.vel = vel return self
python
def compute_metrics(self, previous): delta_t = self.time_difference(previous) delta_x = self.distance(previous) vel = 0 delta_v = 0 acc = 0 if delta_t != 0: vel = delta_x/delta_t delta_v = vel - previous.vel acc = delta_v/delta_t self.dt = delta_t self.dx = delta_x self.acc = acc self.vel = vel return self
[ "def", "compute_metrics", "(", "self", ",", "previous", ")", ":", "delta_t", "=", "self", ".", "time_difference", "(", "previous", ")", "delta_x", "=", "self", ".", "distance", "(", "previous", ")", "vel", "=", "0", "delta_v", "=", "0", "acc", "=", "0", "if", "delta_t", "!=", "0", ":", "vel", "=", "delta_x", "/", "delta_t", "delta_v", "=", "vel", "-", "previous", ".", "vel", "acc", "=", "delta_v", "/", "delta_t", "self", ".", "dt", "=", "delta_t", "self", ".", "dx", "=", "delta_x", "self", ".", "acc", "=", "acc", "self", ".", "vel", "=", "vel", "return", "self" ]
Computes the metrics of this point Computes and updates the dt, vel and acc attributes. Args: previous (:obj:`Point`): Point before Returns: :obj:`Point`: Self
[ "Computes", "the", "metrics", "of", "this", "point" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/point.py#L77-L101
3,948
ruipgil/TrackToTrip
tracktotrip/point.py
Point.from_gpx
def from_gpx(gpx_track_point): """ Creates a point from GPX representation Arguments: gpx_track_point (:obj:`gpxpy.GPXTrackPoint`) Returns: :obj:`Point` """ return Point( lat=gpx_track_point.latitude, lon=gpx_track_point.longitude, time=gpx_track_point.time )
python
def from_gpx(gpx_track_point): return Point( lat=gpx_track_point.latitude, lon=gpx_track_point.longitude, time=gpx_track_point.time )
[ "def", "from_gpx", "(", "gpx_track_point", ")", ":", "return", "Point", "(", "lat", "=", "gpx_track_point", ".", "latitude", ",", "lon", "=", "gpx_track_point", ".", "longitude", ",", "time", "=", "gpx_track_point", ".", "time", ")" ]
Creates a point from GPX representation Arguments: gpx_track_point (:obj:`gpxpy.GPXTrackPoint`) Returns: :obj:`Point`
[ "Creates", "a", "point", "from", "GPX", "representation" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/point.py#L104-L116
3,949
ruipgil/TrackToTrip
tracktotrip/point.py
Point.to_json
def to_json(self): """ Creates a JSON serializable representation of this instance Returns: :obj:`dict`: For example, { "lat": 9.3470298, "lon": 3.79274, "time": "2016-07-15T15:27:53.574110" } """ return { 'lat': self.lat, 'lon': self.lon, 'time': self.time.isoformat() if self.time is not None else None }
python
def to_json(self): return { 'lat': self.lat, 'lon': self.lon, 'time': self.time.isoformat() if self.time is not None else None }
[ "def", "to_json", "(", "self", ")", ":", "return", "{", "'lat'", ":", "self", ".", "lat", ",", "'lon'", ":", "self", ".", "lon", ",", "'time'", ":", "self", ".", "time", ".", "isoformat", "(", ")", "if", "self", ".", "time", "is", "not", "None", "else", "None", "}" ]
Creates a JSON serializable representation of this instance Returns: :obj:`dict`: For example, { "lat": 9.3470298, "lon": 3.79274, "time": "2016-07-15T15:27:53.574110" }
[ "Creates", "a", "JSON", "serializable", "representation", "of", "this", "instance" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/point.py#L118-L133
3,950
ruipgil/TrackToTrip
tracktotrip/point.py
Point.from_json
def from_json(json): """ Creates Point instance from JSON representation Args: json (:obj:`dict`): Must have at least the following keys: lat (float), lon (float), time (string in iso format). Example, { "lat": 9.3470298, "lon": 3.79274, "time": "2016-07-15T15:27:53.574110" } json: map representation of Point instance Returns: :obj:`Point` """ return Point( lat=json['lat'], lon=json['lon'], time=isostr_to_datetime(json['time']) )
python
def from_json(json): return Point( lat=json['lat'], lon=json['lon'], time=isostr_to_datetime(json['time']) )
[ "def", "from_json", "(", "json", ")", ":", "return", "Point", "(", "lat", "=", "json", "[", "'lat'", "]", ",", "lon", "=", "json", "[", "'lon'", "]", ",", "time", "=", "isostr_to_datetime", "(", "json", "[", "'time'", "]", ")", ")" ]
Creates Point instance from JSON representation Args: json (:obj:`dict`): Must have at least the following keys: lat (float), lon (float), time (string in iso format). Example, { "lat": 9.3470298, "lon": 3.79274, "time": "2016-07-15T15:27:53.574110" } json: map representation of Point instance Returns: :obj:`Point`
[ "Creates", "Point", "instance", "from", "JSON", "representation" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/point.py#L136-L155
3,951
ruipgil/TrackToTrip
tracktotrip/location.py
compute_centroid
def compute_centroid(points): """ Computes the centroid of set of points Args: points (:obj:`list` of :obj:`Point`) Returns: :obj:`Point` """ lats = [p[1] for p in points] lons = [p[0] for p in points] return Point(np.mean(lats), np.mean(lons), None)
python
def compute_centroid(points): lats = [p[1] for p in points] lons = [p[0] for p in points] return Point(np.mean(lats), np.mean(lons), None)
[ "def", "compute_centroid", "(", "points", ")", ":", "lats", "=", "[", "p", "[", "1", "]", "for", "p", "in", "points", "]", "lons", "=", "[", "p", "[", "0", "]", "for", "p", "in", "points", "]", "return", "Point", "(", "np", ".", "mean", "(", "lats", ")", ",", "np", ".", "mean", "(", "lons", ")", ",", "None", ")" ]
Computes the centroid of set of points Args: points (:obj:`list` of :obj:`Point`) Returns: :obj:`Point`
[ "Computes", "the", "centroid", "of", "set", "of", "points" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/location.py#L37-L47
3,952
ruipgil/TrackToTrip
tracktotrip/location.py
update_location_centroid
def update_location_centroid(point, cluster, max_distance, min_samples): """ Updates the centroid of a location cluster with another point Args: point (:obj:`Point`): Point to add to the cluster cluster (:obj:`list` of :obj:`Point`): Location cluster max_distance (float): Max neighbour distance min_samples (int): Minimum number of samples Returns: (:obj:`Point`, :obj:`list` of :obj:`Point`): Tuple with the location centroid and new point cluster (given cluster + given point) """ cluster.append(point) points = [p.gen2arr() for p in cluster] # Estimates the epsilon eps = estimate_meters_to_deg(max_distance, precision=6) p_cluster = DBSCAN(eps=eps, min_samples=min_samples) p_cluster.fit(points) clusters = {} for i, label in enumerate(p_cluster.labels_): if label in clusters.keys(): clusters[label].append(points[i]) else: clusters[label] = [points[i]] centroids = [] biggest_centroid_l = -float("inf") biggest_centroid = None for label, n_cluster in clusters.items(): centroid = compute_centroid(n_cluster) centroids.append(centroid) if label >= 0 and len(n_cluster) >= biggest_centroid_l: biggest_centroid_l = len(n_cluster) biggest_centroid = centroid if biggest_centroid is None: biggest_centroid = compute_centroid(points) return biggest_centroid, cluster
python
def update_location_centroid(point, cluster, max_distance, min_samples): cluster.append(point) points = [p.gen2arr() for p in cluster] # Estimates the epsilon eps = estimate_meters_to_deg(max_distance, precision=6) p_cluster = DBSCAN(eps=eps, min_samples=min_samples) p_cluster.fit(points) clusters = {} for i, label in enumerate(p_cluster.labels_): if label in clusters.keys(): clusters[label].append(points[i]) else: clusters[label] = [points[i]] centroids = [] biggest_centroid_l = -float("inf") biggest_centroid = None for label, n_cluster in clusters.items(): centroid = compute_centroid(n_cluster) centroids.append(centroid) if label >= 0 and len(n_cluster) >= biggest_centroid_l: biggest_centroid_l = len(n_cluster) biggest_centroid = centroid if biggest_centroid is None: biggest_centroid = compute_centroid(points) return biggest_centroid, cluster
[ "def", "update_location_centroid", "(", "point", ",", "cluster", ",", "max_distance", ",", "min_samples", ")", ":", "cluster", ".", "append", "(", "point", ")", "points", "=", "[", "p", ".", "gen2arr", "(", ")", "for", "p", "in", "cluster", "]", "# Estimates the epsilon", "eps", "=", "estimate_meters_to_deg", "(", "max_distance", ",", "precision", "=", "6", ")", "p_cluster", "=", "DBSCAN", "(", "eps", "=", "eps", ",", "min_samples", "=", "min_samples", ")", "p_cluster", ".", "fit", "(", "points", ")", "clusters", "=", "{", "}", "for", "i", ",", "label", "in", "enumerate", "(", "p_cluster", ".", "labels_", ")", ":", "if", "label", "in", "clusters", ".", "keys", "(", ")", ":", "clusters", "[", "label", "]", ".", "append", "(", "points", "[", "i", "]", ")", "else", ":", "clusters", "[", "label", "]", "=", "[", "points", "[", "i", "]", "]", "centroids", "=", "[", "]", "biggest_centroid_l", "=", "-", "float", "(", "\"inf\"", ")", "biggest_centroid", "=", "None", "for", "label", ",", "n_cluster", "in", "clusters", ".", "items", "(", ")", ":", "centroid", "=", "compute_centroid", "(", "n_cluster", ")", "centroids", ".", "append", "(", "centroid", ")", "if", "label", ">=", "0", "and", "len", "(", "n_cluster", ")", ">=", "biggest_centroid_l", ":", "biggest_centroid_l", "=", "len", "(", "n_cluster", ")", "biggest_centroid", "=", "centroid", "if", "biggest_centroid", "is", "None", ":", "biggest_centroid", "=", "compute_centroid", "(", "points", ")", "return", "biggest_centroid", ",", "cluster" ]
Updates the centroid of a location cluster with another point Args: point (:obj:`Point`): Point to add to the cluster cluster (:obj:`list` of :obj:`Point`): Location cluster max_distance (float): Max neighbour distance min_samples (int): Minimum number of samples Returns: (:obj:`Point`, :obj:`list` of :obj:`Point`): Tuple with the location centroid and new point cluster (given cluster + given point)
[ "Updates", "the", "centroid", "of", "a", "location", "cluster", "with", "another", "point" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/location.py#L49-L92
3,953
ruipgil/TrackToTrip
tracktotrip/location.py
query_foursquare
def query_foursquare(point, max_distance, client_id, client_secret): """ Queries Squarespace API for a location Args: point (:obj:`Point`): Point location to query max_distance (float): Search radius, in meters client_id (str): Valid Foursquare client id client_secret (str): Valid Foursquare client secret Returns: :obj:`list` of :obj:`dict`: List of locations with the following format: { 'label': 'Coffee house', 'distance': 19, 'types': 'Commerce', 'suggestion_type': 'FOURSQUARE' } """ if not client_id: return [] if not client_secret: return [] if from_cache(FS_CACHE, point, max_distance): return from_cache(FS_CACHE, point, max_distance) url = FOURSQUARE_URL % (client_id, client_secret, point.lat, point.lon, max_distance) req = requests.get(url) if req.status_code != 200: return [] response = req.json() result = [] venues = response['response']['venues'] for venue in venues: name = venue['name'] distance = venue['location']['distance'] categories = [c['shortName'] for c in venue['categories']] result.append({ 'label': name, 'distance': distance, 'types': categories, 'suggestion_type': 'FOURSQUARE' }) # final_results = sorted(result, key=lambda elm: elm['distance']) foursquare_insert_cache(point, result) return result
python
def query_foursquare(point, max_distance, client_id, client_secret): if not client_id: return [] if not client_secret: return [] if from_cache(FS_CACHE, point, max_distance): return from_cache(FS_CACHE, point, max_distance) url = FOURSQUARE_URL % (client_id, client_secret, point.lat, point.lon, max_distance) req = requests.get(url) if req.status_code != 200: return [] response = req.json() result = [] venues = response['response']['venues'] for venue in venues: name = venue['name'] distance = venue['location']['distance'] categories = [c['shortName'] for c in venue['categories']] result.append({ 'label': name, 'distance': distance, 'types': categories, 'suggestion_type': 'FOURSQUARE' }) # final_results = sorted(result, key=lambda elm: elm['distance']) foursquare_insert_cache(point, result) return result
[ "def", "query_foursquare", "(", "point", ",", "max_distance", ",", "client_id", ",", "client_secret", ")", ":", "if", "not", "client_id", ":", "return", "[", "]", "if", "not", "client_secret", ":", "return", "[", "]", "if", "from_cache", "(", "FS_CACHE", ",", "point", ",", "max_distance", ")", ":", "return", "from_cache", "(", "FS_CACHE", ",", "point", ",", "max_distance", ")", "url", "=", "FOURSQUARE_URL", "%", "(", "client_id", ",", "client_secret", ",", "point", ".", "lat", ",", "point", ".", "lon", ",", "max_distance", ")", "req", "=", "requests", ".", "get", "(", "url", ")", "if", "req", ".", "status_code", "!=", "200", ":", "return", "[", "]", "response", "=", "req", ".", "json", "(", ")", "result", "=", "[", "]", "venues", "=", "response", "[", "'response'", "]", "[", "'venues'", "]", "for", "venue", "in", "venues", ":", "name", "=", "venue", "[", "'name'", "]", "distance", "=", "venue", "[", "'location'", "]", "[", "'distance'", "]", "categories", "=", "[", "c", "[", "'shortName'", "]", "for", "c", "in", "venue", "[", "'categories'", "]", "]", "result", ".", "append", "(", "{", "'label'", ":", "name", ",", "'distance'", ":", "distance", ",", "'types'", ":", "categories", ",", "'suggestion_type'", ":", "'FOURSQUARE'", "}", ")", "# final_results = sorted(result, key=lambda elm: elm['distance'])", "foursquare_insert_cache", "(", "point", ",", "result", ")", "return", "result" ]
Queries Squarespace API for a location Args: point (:obj:`Point`): Point location to query max_distance (float): Search radius, in meters client_id (str): Valid Foursquare client id client_secret (str): Valid Foursquare client secret Returns: :obj:`list` of :obj:`dict`: List of locations with the following format: { 'label': 'Coffee house', 'distance': 19, 'types': 'Commerce', 'suggestion_type': 'FOURSQUARE' }
[ "Queries", "Squarespace", "API", "for", "a", "location" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/location.py#L94-L142
3,954
ruipgil/TrackToTrip
tracktotrip/location.py
query_google
def query_google(point, max_distance, key): """ Queries google maps API for a location Args: point (:obj:`Point`): Point location to query max_distance (float): Search radius, in meters key (str): Valid google maps api key Returns: :obj:`list` of :obj:`dict`: List of locations with the following format: { 'label': 'Coffee house', 'types': 'Commerce', 'suggestion_type': 'GOOGLE' } """ if not key: return [] if from_cache(GG_CACHE, point, max_distance): return from_cache(GG_CACHE, point, max_distance) req = requests.get(GOOGLE_PLACES_URL % ( point.lat, point.lon, max_distance, key )) if req.status_code != 200: return [] response = req.json() results = response['results'] # l = len(results) final_results = [] for local in results: final_results.append({ 'label': local['name'], 'distance': Point(local['geometry']['location']['lat'], local['geometry']['location']['lng'], None).distance(point), # 'rank': (l-i)/float(l), 'types': local['types'], 'suggestion_type': 'GOOGLE' }) google_insert_cache(point, final_results) return final_results
python
def query_google(point, max_distance, key): if not key: return [] if from_cache(GG_CACHE, point, max_distance): return from_cache(GG_CACHE, point, max_distance) req = requests.get(GOOGLE_PLACES_URL % ( point.lat, point.lon, max_distance, key )) if req.status_code != 200: return [] response = req.json() results = response['results'] # l = len(results) final_results = [] for local in results: final_results.append({ 'label': local['name'], 'distance': Point(local['geometry']['location']['lat'], local['geometry']['location']['lng'], None).distance(point), # 'rank': (l-i)/float(l), 'types': local['types'], 'suggestion_type': 'GOOGLE' }) google_insert_cache(point, final_results) return final_results
[ "def", "query_google", "(", "point", ",", "max_distance", ",", "key", ")", ":", "if", "not", "key", ":", "return", "[", "]", "if", "from_cache", "(", "GG_CACHE", ",", "point", ",", "max_distance", ")", ":", "return", "from_cache", "(", "GG_CACHE", ",", "point", ",", "max_distance", ")", "req", "=", "requests", ".", "get", "(", "GOOGLE_PLACES_URL", "%", "(", "point", ".", "lat", ",", "point", ".", "lon", ",", "max_distance", ",", "key", ")", ")", "if", "req", ".", "status_code", "!=", "200", ":", "return", "[", "]", "response", "=", "req", ".", "json", "(", ")", "results", "=", "response", "[", "'results'", "]", "# l = len(results)", "final_results", "=", "[", "]", "for", "local", "in", "results", ":", "final_results", ".", "append", "(", "{", "'label'", ":", "local", "[", "'name'", "]", ",", "'distance'", ":", "Point", "(", "local", "[", "'geometry'", "]", "[", "'location'", "]", "[", "'lat'", "]", ",", "local", "[", "'geometry'", "]", "[", "'location'", "]", "[", "'lng'", "]", ",", "None", ")", ".", "distance", "(", "point", ")", ",", "# 'rank': (l-i)/float(l),", "'types'", ":", "local", "[", "'types'", "]", ",", "'suggestion_type'", ":", "'GOOGLE'", "}", ")", "google_insert_cache", "(", "point", ",", "final_results", ")", "return", "final_results" ]
Queries google maps API for a location Args: point (:obj:`Point`): Point location to query max_distance (float): Search radius, in meters key (str): Valid google maps api key Returns: :obj:`list` of :obj:`dict`: List of locations with the following format: { 'label': 'Coffee house', 'types': 'Commerce', 'suggestion_type': 'GOOGLE' }
[ "Queries", "google", "maps", "API", "for", "a", "location" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/location.py#L145-L189
3,955
ruipgil/TrackToTrip
tracktotrip/utils.py
estimate_meters_to_deg
def estimate_meters_to_deg(meters, precision=PRECISION_PERSON): """ Meters to degrees estimation See https://en.wikipedia.org/wiki/Decimal_degrees Args: meters (float) precision (float) Returns: float: meters in degrees approximation """ line = PRECISION_TABLE[precision] dec = 1/float(10 ** precision) return meters / line[3] * dec
python
def estimate_meters_to_deg(meters, precision=PRECISION_PERSON): line = PRECISION_TABLE[precision] dec = 1/float(10 ** precision) return meters / line[3] * dec
[ "def", "estimate_meters_to_deg", "(", "meters", ",", "precision", "=", "PRECISION_PERSON", ")", ":", "line", "=", "PRECISION_TABLE", "[", "precision", "]", "dec", "=", "1", "/", "float", "(", "10", "**", "precision", ")", "return", "meters", "/", "line", "[", "3", "]", "*", "dec" ]
Meters to degrees estimation See https://en.wikipedia.org/wiki/Decimal_degrees Args: meters (float) precision (float) Returns: float: meters in degrees approximation
[ "Meters", "to", "degrees", "estimation" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/utils.py#L21-L34
3,956
ruipgil/TrackToTrip
tracktotrip/utils.py
isostr_to_datetime
def isostr_to_datetime(dt_str): """ Converts iso formated text string into a datetime object Args: dt_str (str): ISO formated text string Returns: :obj:`datetime.datetime` """ if len(dt_str) <= 20: return datetime.datetime.strptime(dt_str, "%Y-%m-%dT%H:%M:%SZ") else: dt_str = dt_str.split(".") return isostr_to_datetime("%sZ" % dt_str[0])
python
def isostr_to_datetime(dt_str): if len(dt_str) <= 20: return datetime.datetime.strptime(dt_str, "%Y-%m-%dT%H:%M:%SZ") else: dt_str = dt_str.split(".") return isostr_to_datetime("%sZ" % dt_str[0])
[ "def", "isostr_to_datetime", "(", "dt_str", ")", ":", "if", "len", "(", "dt_str", ")", "<=", "20", ":", "return", "datetime", ".", "datetime", ".", "strptime", "(", "dt_str", ",", "\"%Y-%m-%dT%H:%M:%SZ\"", ")", "else", ":", "dt_str", "=", "dt_str", ".", "split", "(", "\".\"", ")", "return", "isostr_to_datetime", "(", "\"%sZ\"", "%", "dt_str", "[", "0", "]", ")" ]
Converts iso formated text string into a datetime object Args: dt_str (str): ISO formated text string Returns: :obj:`datetime.datetime`
[ "Converts", "iso", "formated", "text", "string", "into", "a", "datetime", "object" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/utils.py#L36-L48
3,957
ruipgil/TrackToTrip
tracktotrip/classifier.py
Classifier.__learn_labels
def __learn_labels(self, labels): """ Learns new labels, this method is intended for internal use Args: labels (:obj:`list` of :obj:`str`): Labels to learn """ if self.feature_length > 0: result = list(self.labels.classes_) else: result = [] for label in labels: result.append(label) self.labels.fit(result)
python
def __learn_labels(self, labels): if self.feature_length > 0: result = list(self.labels.classes_) else: result = [] for label in labels: result.append(label) self.labels.fit(result)
[ "def", "__learn_labels", "(", "self", ",", "labels", ")", ":", "if", "self", ".", "feature_length", ">", "0", ":", "result", "=", "list", "(", "self", ".", "labels", ".", "classes_", ")", "else", ":", "result", "=", "[", "]", "for", "label", "in", "labels", ":", "result", ".", "append", "(", "label", ")", "self", ".", "labels", ".", "fit", "(", "result", ")" ]
Learns new labels, this method is intended for internal use Args: labels (:obj:`list` of :obj:`str`): Labels to learn
[ "Learns", "new", "labels", "this", "method", "is", "intended", "for", "internal", "use" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/classifier.py#L28-L41
3,958
ruipgil/TrackToTrip
tracktotrip/classifier.py
Classifier.learn
def learn(self, features, labels): """ Fits the classifier If it's state is empty, the classifier is fitted, if not the classifier is partially fitted. See sklearn's SGDClassifier fit and partial_fit methods. Args: features (:obj:`list` of :obj:`list` of :obj:`float`) labels (:obj:`list` of :obj:`str`): Labels for each set of features. New features are learnt. """ labels = np.ravel(labels) self.__learn_labels(labels) if len(labels) == 0: return labels = self.labels.transform(labels) if self.feature_length > 0 and hasattr(self.clf, 'partial_fit'): # FIXME? check docs, may need to pass class=[...] self.clf = self.clf.partial_fit(features, labels) else: self.clf = self.clf.fit(features, labels) self.feature_length = len(features[0])
python
def learn(self, features, labels): labels = np.ravel(labels) self.__learn_labels(labels) if len(labels) == 0: return labels = self.labels.transform(labels) if self.feature_length > 0 and hasattr(self.clf, 'partial_fit'): # FIXME? check docs, may need to pass class=[...] self.clf = self.clf.partial_fit(features, labels) else: self.clf = self.clf.fit(features, labels) self.feature_length = len(features[0])
[ "def", "learn", "(", "self", ",", "features", ",", "labels", ")", ":", "labels", "=", "np", ".", "ravel", "(", "labels", ")", "self", ".", "__learn_labels", "(", "labels", ")", "if", "len", "(", "labels", ")", "==", "0", ":", "return", "labels", "=", "self", ".", "labels", ".", "transform", "(", "labels", ")", "if", "self", ".", "feature_length", ">", "0", "and", "hasattr", "(", "self", ".", "clf", ",", "'partial_fit'", ")", ":", "# FIXME? check docs, may need to pass class=[...]", "self", ".", "clf", "=", "self", ".", "clf", ".", "partial_fit", "(", "features", ",", "labels", ")", "else", ":", "self", ".", "clf", "=", "self", ".", "clf", ".", "fit", "(", "features", ",", "labels", ")", "self", ".", "feature_length", "=", "len", "(", "features", "[", "0", "]", ")" ]
Fits the classifier If it's state is empty, the classifier is fitted, if not the classifier is partially fitted. See sklearn's SGDClassifier fit and partial_fit methods. Args: features (:obj:`list` of :obj:`list` of :obj:`float`) labels (:obj:`list` of :obj:`str`): Labels for each set of features. New features are learnt.
[ "Fits", "the", "classifier" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/classifier.py#L43-L66
3,959
ruipgil/TrackToTrip
tracktotrip/classifier.py
Classifier.predict
def predict(self, features, verbose=False): """ Probability estimates of each feature See sklearn's SGDClassifier predict and predict_proba methods. Args: features (:obj:`list` of :obj:`list` of :obj:`float`) verbose: Boolean, optional. If true returns an array where each element is a dictionary, where keys are labels and values are the respective probabilities. Defaults to False. Returns: Array of array of numbers, or array of dictionaries if verbose i True """ probs = self.clf.predict_proba(features) if verbose: labels = self.labels.classes_ res = [] for prob in probs: vals = {} for i, val in enumerate(prob): label = labels[i] vals[label] = val res.append(vals) return res else: return probs
python
def predict(self, features, verbose=False): probs = self.clf.predict_proba(features) if verbose: labels = self.labels.classes_ res = [] for prob in probs: vals = {} for i, val in enumerate(prob): label = labels[i] vals[label] = val res.append(vals) return res else: return probs
[ "def", "predict", "(", "self", ",", "features", ",", "verbose", "=", "False", ")", ":", "probs", "=", "self", ".", "clf", ".", "predict_proba", "(", "features", ")", "if", "verbose", ":", "labels", "=", "self", ".", "labels", ".", "classes_", "res", "=", "[", "]", "for", "prob", "in", "probs", ":", "vals", "=", "{", "}", "for", "i", ",", "val", "in", "enumerate", "(", "prob", ")", ":", "label", "=", "labels", "[", "i", "]", "vals", "[", "label", "]", "=", "val", "res", ".", "append", "(", "vals", ")", "return", "res", "else", ":", "return", "probs" ]
Probability estimates of each feature See sklearn's SGDClassifier predict and predict_proba methods. Args: features (:obj:`list` of :obj:`list` of :obj:`float`) verbose: Boolean, optional. If true returns an array where each element is a dictionary, where keys are labels and values are the respective probabilities. Defaults to False. Returns: Array of array of numbers, or array of dictionaries if verbose i True
[ "Probability", "estimates", "of", "each", "feature" ]
5537c14ee9748091b5255b658ab528e1d6227f99
https://github.com/ruipgil/TrackToTrip/blob/5537c14ee9748091b5255b658ab528e1d6227f99/tracktotrip/classifier.py#L72-L98
3,960
the-tale/pynames
pynames/from_tables_generator.py
FromCSVTablesGenerator.source_loader
def source_loader(self, source_paths, create_missing_tables=True): """Load source from 3 csv files. First file should contain global settings: * ``native_lagnauge,languages`` header on first row * appropriate values on following rows Example:: native_lagnauge,languages ru,ru ,en Second file should contain templates: * ``template_name,probability,genders,template`` header on first row * appropriate values on following rows (separate values with semicolon ";" in template column) Example:: template_name,probability,genders,template male_1,5,m,prefixes;male_suffixes baby_1,1,m;f,prefixes;descriptive Third file should contain tables with values for template slugs in all languages: * first row should contain slugs with language code after colon for each * appropriate values on following rows. Multiple forms may be specified using semicolon as separator Example:: prefixes:ru,prefixes:en,male_suffixes:ru,male_suffixes:en,descriptive:ru,descriptive:en Бж,Bzh,пра,pra,быстряк;быстряку,fasty дон;дону,don,Иван;Ивану,Ivan,Иванов;Иванову,Ivanov Note: you may use slugs without ":lang_code" suffix in csv header of tables file. Such headers will be treated as headers for native language If tables are missing for some slug then it is automatically created with values equeal to slug itself. So you may use some slugs without specifying tables data for them. Example for apostrophe and space: male_1,5,m,prefixes;';male_suffixes male_full,5,m,first_name; ;last_name """ if not isinstance(source_paths, Iterable) or len(source_paths) < 3: raise TypeError('FromCSVTablesGenerator.source_loader accepts list of 3 paths as argument. Got `%s` instead' % source_paths) self.native_language = '' self.languages = [] self.templates = [] self.tables = {} self.load_settings(source_paths[0]) template_slugs = self.load_templates(source_paths[1]) self.load_tables(source_paths[2]) if create_missing_tables: self.create_missing_tables(template_slugs) self.full_forms_for_languages = set()
python
def source_loader(self, source_paths, create_missing_tables=True): if not isinstance(source_paths, Iterable) or len(source_paths) < 3: raise TypeError('FromCSVTablesGenerator.source_loader accepts list of 3 paths as argument. Got `%s` instead' % source_paths) self.native_language = '' self.languages = [] self.templates = [] self.tables = {} self.load_settings(source_paths[0]) template_slugs = self.load_templates(source_paths[1]) self.load_tables(source_paths[2]) if create_missing_tables: self.create_missing_tables(template_slugs) self.full_forms_for_languages = set()
[ "def", "source_loader", "(", "self", ",", "source_paths", ",", "create_missing_tables", "=", "True", ")", ":", "if", "not", "isinstance", "(", "source_paths", ",", "Iterable", ")", "or", "len", "(", "source_paths", ")", "<", "3", ":", "raise", "TypeError", "(", "'FromCSVTablesGenerator.source_loader accepts list of 3 paths as argument. Got `%s` instead'", "%", "source_paths", ")", "self", ".", "native_language", "=", "''", "self", ".", "languages", "=", "[", "]", "self", ".", "templates", "=", "[", "]", "self", ".", "tables", "=", "{", "}", "self", ".", "load_settings", "(", "source_paths", "[", "0", "]", ")", "template_slugs", "=", "self", ".", "load_templates", "(", "source_paths", "[", "1", "]", ")", "self", ".", "load_tables", "(", "source_paths", "[", "2", "]", ")", "if", "create_missing_tables", ":", "self", ".", "create_missing_tables", "(", "template_slugs", ")", "self", ".", "full_forms_for_languages", "=", "set", "(", ")" ]
Load source from 3 csv files. First file should contain global settings: * ``native_lagnauge,languages`` header on first row * appropriate values on following rows Example:: native_lagnauge,languages ru,ru ,en Second file should contain templates: * ``template_name,probability,genders,template`` header on first row * appropriate values on following rows (separate values with semicolon ";" in template column) Example:: template_name,probability,genders,template male_1,5,m,prefixes;male_suffixes baby_1,1,m;f,prefixes;descriptive Third file should contain tables with values for template slugs in all languages: * first row should contain slugs with language code after colon for each * appropriate values on following rows. Multiple forms may be specified using semicolon as separator Example:: prefixes:ru,prefixes:en,male_suffixes:ru,male_suffixes:en,descriptive:ru,descriptive:en Бж,Bzh,пра,pra,быстряк;быстряку,fasty дон;дону,don,Иван;Ивану,Ivan,Иванов;Иванову,Ivanov Note: you may use slugs without ":lang_code" suffix in csv header of tables file. Such headers will be treated as headers for native language If tables are missing for some slug then it is automatically created with values equeal to slug itself. So you may use some slugs without specifying tables data for them. Example for apostrophe and space: male_1,5,m,prefixes;';male_suffixes male_full,5,m,first_name; ;last_name
[ "Load", "source", "from", "3", "csv", "files", "." ]
da45eaaac3166847bcb2c48cab4571a462660ace
https://github.com/the-tale/pynames/blob/da45eaaac3166847bcb2c48cab4571a462660ace/pynames/from_tables_generator.py#L179-L238
3,961
rtlee9/serveit
examples/pytorch_imagenet_resnet50.py
loader
def loader(): """Load image from URL, and preprocess for Resnet.""" url = request.args.get('url') # read image URL as a request URL param response = requests.get(url) # make request to static image file return response.content
python
def loader(): url = request.args.get('url') # read image URL as a request URL param response = requests.get(url) # make request to static image file return response.content
[ "def", "loader", "(", ")", ":", "url", "=", "request", ".", "args", ".", "get", "(", "'url'", ")", "# read image URL as a request URL param", "response", "=", "requests", ".", "get", "(", "url", ")", "# make request to static image file", "return", "response", ".", "content" ]
Load image from URL, and preprocess for Resnet.
[ "Load", "image", "from", "URL", "and", "preprocess", "for", "Resnet", "." ]
d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc
https://github.com/rtlee9/serveit/blob/d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc/examples/pytorch_imagenet_resnet50.py#L32-L36
3,962
rtlee9/serveit
examples/pytorch_imagenet_resnet50.py
postprocessor
def postprocessor(prediction): """Map prediction tensor to labels.""" prediction = prediction.data.numpy()[0] top_predictions = prediction.argsort()[-3:][::-1] return [labels[prediction] for prediction in top_predictions]
python
def postprocessor(prediction): prediction = prediction.data.numpy()[0] top_predictions = prediction.argsort()[-3:][::-1] return [labels[prediction] for prediction in top_predictions]
[ "def", "postprocessor", "(", "prediction", ")", ":", "prediction", "=", "prediction", ".", "data", ".", "numpy", "(", ")", "[", "0", "]", "top_predictions", "=", "prediction", ".", "argsort", "(", ")", "[", "-", "3", ":", "]", "[", ":", ":", "-", "1", "]", "return", "[", "labels", "[", "prediction", "]", "for", "prediction", "in", "top_predictions", "]" ]
Map prediction tensor to labels.
[ "Map", "prediction", "tensor", "to", "labels", "." ]
d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc
https://github.com/rtlee9/serveit/blob/d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc/examples/pytorch_imagenet_resnet50.py#L48-L52
3,963
rtlee9/serveit
serveit/log_utils.py
get_logger
def get_logger(name): """Get a logger with the specified name.""" logger = logging.getLogger(name) logger.setLevel(getenv('LOGLEVEL', 'INFO')) return logger
python
def get_logger(name): logger = logging.getLogger(name) logger.setLevel(getenv('LOGLEVEL', 'INFO')) return logger
[ "def", "get_logger", "(", "name", ")", ":", "logger", "=", "logging", ".", "getLogger", "(", "name", ")", "logger", ".", "setLevel", "(", "getenv", "(", "'LOGLEVEL'", ",", "'INFO'", ")", ")", "return", "logger" ]
Get a logger with the specified name.
[ "Get", "a", "logger", "with", "the", "specified", "name", "." ]
d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc
https://github.com/rtlee9/serveit/blob/d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc/serveit/log_utils.py#L8-L12
3,964
rtlee9/serveit
serveit/utils.py
make_serializable
def make_serializable(data): """Ensure data is serializable.""" if is_serializable(data): return data # if numpy array convert to list try: return data.tolist() except AttributeError: pass except Exception as e: logger.debug('{} exception ({}): {}'.format(type(e).__name__, e, data)) # try serializing each child element if isinstance(data, dict): return {key: make_serializable(value) for key, value in data.items()} try: return [make_serializable(element) for element in data] except TypeError: # not iterable pass except Exception: logger.debug('Could not serialize {}; converting to string'.format(data)) # last resort: convert to string return str(data)
python
def make_serializable(data): if is_serializable(data): return data # if numpy array convert to list try: return data.tolist() except AttributeError: pass except Exception as e: logger.debug('{} exception ({}): {}'.format(type(e).__name__, e, data)) # try serializing each child element if isinstance(data, dict): return {key: make_serializable(value) for key, value in data.items()} try: return [make_serializable(element) for element in data] except TypeError: # not iterable pass except Exception: logger.debug('Could not serialize {}; converting to string'.format(data)) # last resort: convert to string return str(data)
[ "def", "make_serializable", "(", "data", ")", ":", "if", "is_serializable", "(", "data", ")", ":", "return", "data", "# if numpy array convert to list", "try", ":", "return", "data", ".", "tolist", "(", ")", "except", "AttributeError", ":", "pass", "except", "Exception", "as", "e", ":", "logger", ".", "debug", "(", "'{} exception ({}): {}'", ".", "format", "(", "type", "(", "e", ")", ".", "__name__", ",", "e", ",", "data", ")", ")", "# try serializing each child element", "if", "isinstance", "(", "data", ",", "dict", ")", ":", "return", "{", "key", ":", "make_serializable", "(", "value", ")", "for", "key", ",", "value", "in", "data", ".", "items", "(", ")", "}", "try", ":", "return", "[", "make_serializable", "(", "element", ")", "for", "element", "in", "data", "]", "except", "TypeError", ":", "# not iterable", "pass", "except", "Exception", ":", "logger", ".", "debug", "(", "'Could not serialize {}; converting to string'", ".", "format", "(", "data", ")", ")", "# last resort: convert to string", "return", "str", "(", "data", ")" ]
Ensure data is serializable.
[ "Ensure", "data", "is", "serializable", "." ]
d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc
https://github.com/rtlee9/serveit/blob/d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc/serveit/utils.py#L10-L34
3,965
rtlee9/serveit
serveit/utils.py
json_numpy_loader
def json_numpy_loader(): """Load data from JSON request and convert to numpy array.""" data = request.get_json() logger.debug('Received JSON data of length {:,}'.format(len(data))) return data
python
def json_numpy_loader(): data = request.get_json() logger.debug('Received JSON data of length {:,}'.format(len(data))) return data
[ "def", "json_numpy_loader", "(", ")", ":", "data", "=", "request", ".", "get_json", "(", ")", "logger", ".", "debug", "(", "'Received JSON data of length {:,}'", ".", "format", "(", "len", "(", "data", ")", ")", ")", "return", "data" ]
Load data from JSON request and convert to numpy array.
[ "Load", "data", "from", "JSON", "request", "and", "convert", "to", "numpy", "array", "." ]
d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc
https://github.com/rtlee9/serveit/blob/d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc/serveit/utils.py#L46-L50
3,966
rtlee9/serveit
serveit/utils.py
get_bytes_to_image_callback
def get_bytes_to_image_callback(image_dims=(224, 224)): """Return a callback to process image bytes for ImageNet.""" from keras.preprocessing import image import numpy as np from PIL import Image from io import BytesIO def preprocess_image_bytes(data_bytes): """Process image bytes for ImageNet.""" try: img = Image.open(BytesIO(data_bytes)) # open image except OSError as e: raise ValueError('Please provide a raw image') img = img.resize(image_dims, Image.ANTIALIAS) # model requires 224x224 pixels x = image.img_to_array(img) # convert image to numpy array x = np.expand_dims(x, axis=0) # model expects dim 0 to be iterable across images return x return preprocess_image_bytes
python
def get_bytes_to_image_callback(image_dims=(224, 224)): from keras.preprocessing import image import numpy as np from PIL import Image from io import BytesIO def preprocess_image_bytes(data_bytes): """Process image bytes for ImageNet.""" try: img = Image.open(BytesIO(data_bytes)) # open image except OSError as e: raise ValueError('Please provide a raw image') img = img.resize(image_dims, Image.ANTIALIAS) # model requires 224x224 pixels x = image.img_to_array(img) # convert image to numpy array x = np.expand_dims(x, axis=0) # model expects dim 0 to be iterable across images return x return preprocess_image_bytes
[ "def", "get_bytes_to_image_callback", "(", "image_dims", "=", "(", "224", ",", "224", ")", ")", ":", "from", "keras", ".", "preprocessing", "import", "image", "import", "numpy", "as", "np", "from", "PIL", "import", "Image", "from", "io", "import", "BytesIO", "def", "preprocess_image_bytes", "(", "data_bytes", ")", ":", "\"\"\"Process image bytes for ImageNet.\"\"\"", "try", ":", "img", "=", "Image", ".", "open", "(", "BytesIO", "(", "data_bytes", ")", ")", "# open image", "except", "OSError", "as", "e", ":", "raise", "ValueError", "(", "'Please provide a raw image'", ")", "img", "=", "img", ".", "resize", "(", "image_dims", ",", "Image", ".", "ANTIALIAS", ")", "# model requires 224x224 pixels", "x", "=", "image", ".", "img_to_array", "(", "img", ")", "# convert image to numpy array", "x", "=", "np", ".", "expand_dims", "(", "x", ",", "axis", "=", "0", ")", "# model expects dim 0 to be iterable across images", "return", "x", "return", "preprocess_image_bytes" ]
Return a callback to process image bytes for ImageNet.
[ "Return", "a", "callback", "to", "process", "image", "bytes", "for", "ImageNet", "." ]
d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc
https://github.com/rtlee9/serveit/blob/d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc/serveit/utils.py#L53-L70
3,967
rtlee9/serveit
serveit/server.py
exception_log_and_respond
def exception_log_and_respond(exception, logger, message, status_code): """Log an error and send jsonified respond.""" logger.error(message, exc_info=True) return make_response( message, status_code, dict(exception_type=type(exception).__name__, exception_message=str(exception)), )
python
def exception_log_and_respond(exception, logger, message, status_code): logger.error(message, exc_info=True) return make_response( message, status_code, dict(exception_type=type(exception).__name__, exception_message=str(exception)), )
[ "def", "exception_log_and_respond", "(", "exception", ",", "logger", ",", "message", ",", "status_code", ")", ":", "logger", ".", "error", "(", "message", ",", "exc_info", "=", "True", ")", "return", "make_response", "(", "message", ",", "status_code", ",", "dict", "(", "exception_type", "=", "type", "(", "exception", ")", ".", "__name__", ",", "exception_message", "=", "str", "(", "exception", ")", ")", ",", ")" ]
Log an error and send jsonified respond.
[ "Log", "an", "error", "and", "send", "jsonified", "respond", "." ]
d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc
https://github.com/rtlee9/serveit/blob/d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc/serveit/server.py#L12-L19
3,968
rtlee9/serveit
serveit/server.py
make_response
def make_response(message, status_code, details=None): """Make a jsonified response with specified message and status code.""" response_body = dict(message=message) if details: response_body['details'] = details response = jsonify(response_body) response.status_code = status_code return response
python
def make_response(message, status_code, details=None): response_body = dict(message=message) if details: response_body['details'] = details response = jsonify(response_body) response.status_code = status_code return response
[ "def", "make_response", "(", "message", ",", "status_code", ",", "details", "=", "None", ")", ":", "response_body", "=", "dict", "(", "message", "=", "message", ")", "if", "details", ":", "response_body", "[", "'details'", "]", "=", "details", "response", "=", "jsonify", "(", "response_body", ")", "response", ".", "status_code", "=", "status_code", "return", "response" ]
Make a jsonified response with specified message and status code.
[ "Make", "a", "jsonified", "response", "with", "specified", "message", "and", "status", "code", "." ]
d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc
https://github.com/rtlee9/serveit/blob/d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc/serveit/server.py#L22-L29
3,969
rtlee9/serveit
serveit/server.py
ModelServer._create_prediction_endpoint
def _create_prediction_endpoint( self, to_numpy=True, data_loader=json_numpy_loader, preprocessor=lambda x: x, input_validation=lambda data: (True, None), postprocessor=lambda x: x, make_serializable_post=True): """Create an endpoint to serve predictions. Arguments: - input_validation (fn): takes a numpy array as input; returns True if validation passes and False otherwise - data_loader (fn): reads flask request and returns data preprocessed to be used in the `predict` method - postprocessor (fn): transforms the predictions from the `predict` method """ # copy instance variables to local scope for resource class predict = self.predict logger = self.app.logger # create restful resource class Predictions(Resource): @staticmethod def post(): # read data from API request try: data = data_loader() except Exception as e: return exception_log_and_respond(e, logger, 'Unable to fetch data', 400) try: if hasattr(preprocessor, '__iter__'): for preprocessor_step in preprocessor: data = preprocessor_step(data) else: data = preprocessor(data) # preprocess data data = np.array(data) if to_numpy else data # convert to numpy except Exception as e: return exception_log_and_respond(e, logger, 'Could not preprocess data', 400) # sanity check using user defined callback (default is no check) validation_pass, validation_reason = input_validation(data) if not validation_pass: # if validation fails, log the reason code, log the data, and send a 400 response validation_message = 'Input validation failed with reason: {}'.format(validation_reason) logger.error(validation_message) logger.debug('Data: {}'.format(data)) return make_response(validation_message, 400) try: prediction = predict(data) except Exception as e: # log exception and return the message in a 500 response logger.debug('Data: {}'.format(data)) return exception_log_and_respond(e, logger, 'Unable to make prediction', 500) logger.debug(prediction) try: # preprocess data if hasattr(postprocessor, '__iter__'): for postprocessor_step in postprocessor: prediction = postprocessor_step(prediction) else: prediction = postprocessor(prediction) # cast to serializable types if make_serializable_post: return make_serializable(prediction) else: return prediction except Exception as e: return exception_log_and_respond(e, logger, 'Postprocessing failed', 500) # map resource to endpoint self.api.add_resource(Predictions, '/predictions')
python
def _create_prediction_endpoint( self, to_numpy=True, data_loader=json_numpy_loader, preprocessor=lambda x: x, input_validation=lambda data: (True, None), postprocessor=lambda x: x, make_serializable_post=True): # copy instance variables to local scope for resource class predict = self.predict logger = self.app.logger # create restful resource class Predictions(Resource): @staticmethod def post(): # read data from API request try: data = data_loader() except Exception as e: return exception_log_and_respond(e, logger, 'Unable to fetch data', 400) try: if hasattr(preprocessor, '__iter__'): for preprocessor_step in preprocessor: data = preprocessor_step(data) else: data = preprocessor(data) # preprocess data data = np.array(data) if to_numpy else data # convert to numpy except Exception as e: return exception_log_and_respond(e, logger, 'Could not preprocess data', 400) # sanity check using user defined callback (default is no check) validation_pass, validation_reason = input_validation(data) if not validation_pass: # if validation fails, log the reason code, log the data, and send a 400 response validation_message = 'Input validation failed with reason: {}'.format(validation_reason) logger.error(validation_message) logger.debug('Data: {}'.format(data)) return make_response(validation_message, 400) try: prediction = predict(data) except Exception as e: # log exception and return the message in a 500 response logger.debug('Data: {}'.format(data)) return exception_log_and_respond(e, logger, 'Unable to make prediction', 500) logger.debug(prediction) try: # preprocess data if hasattr(postprocessor, '__iter__'): for postprocessor_step in postprocessor: prediction = postprocessor_step(prediction) else: prediction = postprocessor(prediction) # cast to serializable types if make_serializable_post: return make_serializable(prediction) else: return prediction except Exception as e: return exception_log_and_respond(e, logger, 'Postprocessing failed', 500) # map resource to endpoint self.api.add_resource(Predictions, '/predictions')
[ "def", "_create_prediction_endpoint", "(", "self", ",", "to_numpy", "=", "True", ",", "data_loader", "=", "json_numpy_loader", ",", "preprocessor", "=", "lambda", "x", ":", "x", ",", "input_validation", "=", "lambda", "data", ":", "(", "True", ",", "None", ")", ",", "postprocessor", "=", "lambda", "x", ":", "x", ",", "make_serializable_post", "=", "True", ")", ":", "# copy instance variables to local scope for resource class", "predict", "=", "self", ".", "predict", "logger", "=", "self", ".", "app", ".", "logger", "# create restful resource", "class", "Predictions", "(", "Resource", ")", ":", "@", "staticmethod", "def", "post", "(", ")", ":", "# read data from API request", "try", ":", "data", "=", "data_loader", "(", ")", "except", "Exception", "as", "e", ":", "return", "exception_log_and_respond", "(", "e", ",", "logger", ",", "'Unable to fetch data'", ",", "400", ")", "try", ":", "if", "hasattr", "(", "preprocessor", ",", "'__iter__'", ")", ":", "for", "preprocessor_step", "in", "preprocessor", ":", "data", "=", "preprocessor_step", "(", "data", ")", "else", ":", "data", "=", "preprocessor", "(", "data", ")", "# preprocess data", "data", "=", "np", ".", "array", "(", "data", ")", "if", "to_numpy", "else", "data", "# convert to numpy", "except", "Exception", "as", "e", ":", "return", "exception_log_and_respond", "(", "e", ",", "logger", ",", "'Could not preprocess data'", ",", "400", ")", "# sanity check using user defined callback (default is no check)", "validation_pass", ",", "validation_reason", "=", "input_validation", "(", "data", ")", "if", "not", "validation_pass", ":", "# if validation fails, log the reason code, log the data, and send a 400 response", "validation_message", "=", "'Input validation failed with reason: {}'", ".", "format", "(", "validation_reason", ")", "logger", ".", "error", "(", "validation_message", ")", "logger", ".", "debug", "(", "'Data: {}'", ".", "format", "(", "data", ")", ")", "return", "make_response", "(", "validation_message", ",", "400", ")", "try", ":", "prediction", "=", "predict", "(", "data", ")", "except", "Exception", "as", "e", ":", "# log exception and return the message in a 500 response", "logger", ".", "debug", "(", "'Data: {}'", ".", "format", "(", "data", ")", ")", "return", "exception_log_and_respond", "(", "e", ",", "logger", ",", "'Unable to make prediction'", ",", "500", ")", "logger", ".", "debug", "(", "prediction", ")", "try", ":", "# preprocess data", "if", "hasattr", "(", "postprocessor", ",", "'__iter__'", ")", ":", "for", "postprocessor_step", "in", "postprocessor", ":", "prediction", "=", "postprocessor_step", "(", "prediction", ")", "else", ":", "prediction", "=", "postprocessor", "(", "prediction", ")", "# cast to serializable types", "if", "make_serializable_post", ":", "return", "make_serializable", "(", "prediction", ")", "else", ":", "return", "prediction", "except", "Exception", "as", "e", ":", "return", "exception_log_and_respond", "(", "e", ",", "logger", ",", "'Postprocessing failed'", ",", "500", ")", "# map resource to endpoint", "self", ".", "api", ".", "add_resource", "(", "Predictions", ",", "'/predictions'", ")" ]
Create an endpoint to serve predictions. Arguments: - input_validation (fn): takes a numpy array as input; returns True if validation passes and False otherwise - data_loader (fn): reads flask request and returns data preprocessed to be used in the `predict` method - postprocessor (fn): transforms the predictions from the `predict` method
[ "Create", "an", "endpoint", "to", "serve", "predictions", "." ]
d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc
https://github.com/rtlee9/serveit/blob/d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc/serveit/server.py#L77-L152
3,970
rtlee9/serveit
serveit/server.py
ModelServer.serve
def serve(self, host='127.0.0.1', port=5000): """Serve predictions as an API endpoint.""" from meinheld import server, middleware # self.app.run(host=host, port=port) server.listen((host, port)) server.run(middleware.WebSocketMiddleware(self.app))
python
def serve(self, host='127.0.0.1', port=5000): from meinheld import server, middleware # self.app.run(host=host, port=port) server.listen((host, port)) server.run(middleware.WebSocketMiddleware(self.app))
[ "def", "serve", "(", "self", ",", "host", "=", "'127.0.0.1'", ",", "port", "=", "5000", ")", ":", "from", "meinheld", "import", "server", ",", "middleware", "# self.app.run(host=host, port=port)", "server", ".", "listen", "(", "(", "host", ",", "port", ")", ")", "server", ".", "run", "(", "middleware", ".", "WebSocketMiddleware", "(", "self", ".", "app", ")", ")" ]
Serve predictions as an API endpoint.
[ "Serve", "predictions", "as", "an", "API", "endpoint", "." ]
d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc
https://github.com/rtlee9/serveit/blob/d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc/serveit/server.py#L196-L201
3,971
rtlee9/serveit
examples/keras_boston_neural_net.py
get_model
def get_model(input_dim): """Create and compile simple model.""" model = Sequential() model.add(Dense(100, input_dim=input_dim, activation='sigmoid')) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='SGD') return model
python
def get_model(input_dim): model = Sequential() model.add(Dense(100, input_dim=input_dim, activation='sigmoid')) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='SGD') return model
[ "def", "get_model", "(", "input_dim", ")", ":", "model", "=", "Sequential", "(", ")", "model", ".", "add", "(", "Dense", "(", "100", ",", "input_dim", "=", "input_dim", ",", "activation", "=", "'sigmoid'", ")", ")", "model", ".", "add", "(", "Dense", "(", "1", ")", ")", "model", ".", "compile", "(", "loss", "=", "'mean_squared_error'", ",", "optimizer", "=", "'SGD'", ")", "return", "model" ]
Create and compile simple model.
[ "Create", "and", "compile", "simple", "model", "." ]
d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc
https://github.com/rtlee9/serveit/blob/d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc/examples/keras_boston_neural_net.py#L8-L14
3,972
rtlee9/serveit
examples/keras_boston_neural_net.py
validator
def validator(input_data): """Simple model input validator. Validator ensures the input data array is - two dimensional - has the correct number of features. """ global data # check num dims if input_data.ndim != 2: return False, 'Data should have two dimensions.' # check number of columns if input_data.shape[1] != data.data.shape[1]: reason = '{} features required, {} features provided'.format( data.data.shape[1], input_data.shape[1]) return False, reason # validation passed return True, None
python
def validator(input_data): global data # check num dims if input_data.ndim != 2: return False, 'Data should have two dimensions.' # check number of columns if input_data.shape[1] != data.data.shape[1]: reason = '{} features required, {} features provided'.format( data.data.shape[1], input_data.shape[1]) return False, reason # validation passed return True, None
[ "def", "validator", "(", "input_data", ")", ":", "global", "data", "# check num dims", "if", "input_data", ".", "ndim", "!=", "2", ":", "return", "False", ",", "'Data should have two dimensions.'", "# check number of columns", "if", "input_data", ".", "shape", "[", "1", "]", "!=", "data", ".", "data", ".", "shape", "[", "1", "]", ":", "reason", "=", "'{} features required, {} features provided'", ".", "format", "(", "data", ".", "data", ".", "shape", "[", "1", "]", ",", "input_data", ".", "shape", "[", "1", "]", ")", "return", "False", ",", "reason", "# validation passed", "return", "True", ",", "None" ]
Simple model input validator. Validator ensures the input data array is - two dimensional - has the correct number of features.
[ "Simple", "model", "input", "validator", "." ]
d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc
https://github.com/rtlee9/serveit/blob/d97b5fbe56bec78d6c0193d6fd2ea2a0c1cbafdc/examples/keras_boston_neural_net.py#L22-L39
3,973
OTTOMATIC-IO/pycine
pycine/file.py
read_chd_header
def read_chd_header(chd_file): """ read the .chd header file created when Vision Research software saves the images in a file format other than .cine """ with open(chd_file, "rb") as f: header = { "cinefileheader": cine.CINEFILEHEADER(), "bitmapinfoheader": cine.BITMAPINFOHEADER(), "setup": cine.SETUP(), } f.readinto(header["cinefileheader"]) f.readinto(header["bitmapinfoheader"]) f.readinto(header["setup"]) return header
python
def read_chd_header(chd_file): with open(chd_file, "rb") as f: header = { "cinefileheader": cine.CINEFILEHEADER(), "bitmapinfoheader": cine.BITMAPINFOHEADER(), "setup": cine.SETUP(), } f.readinto(header["cinefileheader"]) f.readinto(header["bitmapinfoheader"]) f.readinto(header["setup"]) return header
[ "def", "read_chd_header", "(", "chd_file", ")", ":", "with", "open", "(", "chd_file", ",", "\"rb\"", ")", "as", "f", ":", "header", "=", "{", "\"cinefileheader\"", ":", "cine", ".", "CINEFILEHEADER", "(", ")", ",", "\"bitmapinfoheader\"", ":", "cine", ".", "BITMAPINFOHEADER", "(", ")", ",", "\"setup\"", ":", "cine", ".", "SETUP", "(", ")", ",", "}", "f", ".", "readinto", "(", "header", "[", "\"cinefileheader\"", "]", ")", "f", ".", "readinto", "(", "header", "[", "\"bitmapinfoheader\"", "]", ")", "f", ".", "readinto", "(", "header", "[", "\"setup\"", "]", ")", "return", "header" ]
read the .chd header file created when Vision Research software saves the images in a file format other than .cine
[ "read", "the", ".", "chd", "header", "file", "created", "when", "Vision", "Research", "software", "saves", "the", "images", "in", "a", "file", "format", "other", "than", ".", "cine" ]
8cc27b762796456e36fffe42df940f232e402974
https://github.com/OTTOMATIC-IO/pycine/blob/8cc27b762796456e36fffe42df940f232e402974/pycine/file.py#L31-L46
3,974
alexflint/process-isolation
process_isolation.py
_load_module
def _load_module(module_name, path): '''A helper function invoked on the server to tell it to import a module.''' # TODO: handle the case that the module is already loaded try: # First try to find a non-builtin, non-frozen, non-special # module using the client's search path fd, filename, info = imp.find_module(module_name, path) except ImportError: # The above will fail for builtin, frozen, or special # modules. We search for those now... fd, filename, info = imp.find_module(module_name) # Now import the module given the info found above try: return imp.load_module(module_name, fd, filename, info) finally: if fd is not None: fd.close()
python
def _load_module(module_name, path): '''A helper function invoked on the server to tell it to import a module.''' # TODO: handle the case that the module is already loaded try: # First try to find a non-builtin, non-frozen, non-special # module using the client's search path fd, filename, info = imp.find_module(module_name, path) except ImportError: # The above will fail for builtin, frozen, or special # modules. We search for those now... fd, filename, info = imp.find_module(module_name) # Now import the module given the info found above try: return imp.load_module(module_name, fd, filename, info) finally: if fd is not None: fd.close()
[ "def", "_load_module", "(", "module_name", ",", "path", ")", ":", "# TODO: handle the case that the module is already loaded", "try", ":", "# First try to find a non-builtin, non-frozen, non-special", "# module using the client's search path", "fd", ",", "filename", ",", "info", "=", "imp", ".", "find_module", "(", "module_name", ",", "path", ")", "except", "ImportError", ":", "# The above will fail for builtin, frozen, or special", "# modules. We search for those now...", "fd", ",", "filename", ",", "info", "=", "imp", ".", "find_module", "(", "module_name", ")", "# Now import the module given the info found above", "try", ":", "return", "imp", ".", "load_module", "(", "module_name", ",", "fd", ",", "filename", ",", "info", ")", "finally", ":", "if", "fd", "is", "not", "None", ":", "fd", ".", "close", "(", ")" ]
A helper function invoked on the server to tell it to import a module.
[ "A", "helper", "function", "invoked", "on", "the", "server", "to", "tell", "it", "to", "import", "a", "module", "." ]
1b09862a5ed63be71049dfa8ad22f7c5fc75745c
https://github.com/alexflint/process-isolation/blob/1b09862a5ed63be71049dfa8ad22f7c5fc75745c/process_isolation.py#L84-L101
3,975
alexflint/process-isolation
process_isolation.py
byvalue
def byvalue(proxy): '''Return a copy of the underlying object for which the argument is a proxy.''' assert isinstance(proxy, Proxy) return proxy.client.execute(ByValueDelegate(proxy))
python
def byvalue(proxy): '''Return a copy of the underlying object for which the argument is a proxy.''' assert isinstance(proxy, Proxy) return proxy.client.execute(ByValueDelegate(proxy))
[ "def", "byvalue", "(", "proxy", ")", ":", "assert", "isinstance", "(", "proxy", ",", "Proxy", ")", "return", "proxy", ".", "client", ".", "execute", "(", "ByValueDelegate", "(", "proxy", ")", ")" ]
Return a copy of the underlying object for which the argument is a proxy.
[ "Return", "a", "copy", "of", "the", "underlying", "object", "for", "which", "the", "argument", "is", "a", "proxy", "." ]
1b09862a5ed63be71049dfa8ad22f7c5fc75745c
https://github.com/alexflint/process-isolation/blob/1b09862a5ed63be71049dfa8ad22f7c5fc75745c/process_isolation.py#L103-L107
3,976
alexflint/process-isolation
process_isolation.py
Client.state
def state(self, state): '''Change the state of the client. This is one of the values defined in ClientStates.''' logger.debug('client changing to state=%s', ClientState.Names[state]) self._state = state
python
def state(self, state): '''Change the state of the client. This is one of the values defined in ClientStates.''' logger.debug('client changing to state=%s', ClientState.Names[state]) self._state = state
[ "def", "state", "(", "self", ",", "state", ")", ":", "logger", ".", "debug", "(", "'client changing to state=%s'", ",", "ClientState", ".", "Names", "[", "state", "]", ")", "self", ".", "_state", "=", "state" ]
Change the state of the client. This is one of the values defined in ClientStates.
[ "Change", "the", "state", "of", "the", "client", ".", "This", "is", "one", "of", "the", "values", "defined", "in", "ClientStates", "." ]
1b09862a5ed63be71049dfa8ad22f7c5fc75745c
https://github.com/alexflint/process-isolation/blob/1b09862a5ed63be71049dfa8ad22f7c5fc75745c/process_isolation.py#L607-L611
3,977
alexflint/process-isolation
process_isolation.py
Client._read_result
def _read_result(self, num_retries): '''Read an object from a channel, possibly retrying if the attempt is interrupted by a signal from the operating system.''' for i in range(num_retries): self._assert_alive() try: return self._result_channel.get() except IOError as ex: if ex.errno == 4: # errno=4 corresponds to "System call interrupted", # which means a signal was recieved before any data # was sent. For now I think it's safe to ignore this # and continue. logger.exception('attempt to read from channel was interrupted by something') sys.exc_clear() else: # Something else went wrong - raise the exception as usual raise ex raise ChannelError('failed to read from channel after %d retries' % num_retries)
python
def _read_result(self, num_retries): '''Read an object from a channel, possibly retrying if the attempt is interrupted by a signal from the operating system.''' for i in range(num_retries): self._assert_alive() try: return self._result_channel.get() except IOError as ex: if ex.errno == 4: # errno=4 corresponds to "System call interrupted", # which means a signal was recieved before any data # was sent. For now I think it's safe to ignore this # and continue. logger.exception('attempt to read from channel was interrupted by something') sys.exc_clear() else: # Something else went wrong - raise the exception as usual raise ex raise ChannelError('failed to read from channel after %d retries' % num_retries)
[ "def", "_read_result", "(", "self", ",", "num_retries", ")", ":", "for", "i", "in", "range", "(", "num_retries", ")", ":", "self", ".", "_assert_alive", "(", ")", "try", ":", "return", "self", ".", "_result_channel", ".", "get", "(", ")", "except", "IOError", "as", "ex", ":", "if", "ex", ".", "errno", "==", "4", ":", "# errno=4 corresponds to \"System call interrupted\",", "# which means a signal was recieved before any data", "# was sent. For now I think it's safe to ignore this", "# and continue.", "logger", ".", "exception", "(", "'attempt to read from channel was interrupted by something'", ")", "sys", ".", "exc_clear", "(", ")", "else", ":", "# Something else went wrong - raise the exception as usual", "raise", "ex", "raise", "ChannelError", "(", "'failed to read from channel after %d retries'", "%", "num_retries", ")" ]
Read an object from a channel, possibly retrying if the attempt is interrupted by a signal from the operating system.
[ "Read", "an", "object", "from", "a", "channel", "possibly", "retrying", "if", "the", "attempt", "is", "interrupted", "by", "a", "signal", "from", "the", "operating", "system", "." ]
1b09862a5ed63be71049dfa8ad22f7c5fc75745c
https://github.com/alexflint/process-isolation/blob/1b09862a5ed63be71049dfa8ad22f7c5fc75745c/process_isolation.py#L697-L716
3,978
alexflint/process-isolation
process_isolation.py
Client.terminate
def terminate(self): '''Stop the server process and change our state to TERMINATING. Only valid if state=READY.''' logger.debug('client.terminate() called (state=%s)', self.strstate) if self.state == ClientState.WAITING_FOR_RESULT: raise ClientStateError('terimate() called while state='+self.strstate) if self.state == ClientState.TERMINATING: raise ClientStateError('terimate() called while state='+self.strstate) elif self.state in ClientState.TerminatedSet: assert not self._server_process.is_alive() return elif self.state == ClientState.READY: # Check that the process itself is still alive self._assert_alive() # Make sure the SIGCHLD signal handler doesn't throw any exceptions self.state = ClientState.TERMINATING # Do not call execute() because that function will check # whether the process is alive and throw an exception if not # TODO: can the queue itself throw exceptions? self._delegate_channel.put(FunctionCallDelegate(_raise_terminate)) # Wait for acknowledgement try: self._read_result(num_retries=5) except ProcessTerminationError as ex: pass except ChannelError as ex: # Was interrupted five times in a row! Ignore for now logger.debug('client failed to read sentinel from channel after 5 retries - will terminate anyway') self.state = ClientState.TERMINATED_CLEANLY
python
def terminate(self): '''Stop the server process and change our state to TERMINATING. Only valid if state=READY.''' logger.debug('client.terminate() called (state=%s)', self.strstate) if self.state == ClientState.WAITING_FOR_RESULT: raise ClientStateError('terimate() called while state='+self.strstate) if self.state == ClientState.TERMINATING: raise ClientStateError('terimate() called while state='+self.strstate) elif self.state in ClientState.TerminatedSet: assert not self._server_process.is_alive() return elif self.state == ClientState.READY: # Check that the process itself is still alive self._assert_alive() # Make sure the SIGCHLD signal handler doesn't throw any exceptions self.state = ClientState.TERMINATING # Do not call execute() because that function will check # whether the process is alive and throw an exception if not # TODO: can the queue itself throw exceptions? self._delegate_channel.put(FunctionCallDelegate(_raise_terminate)) # Wait for acknowledgement try: self._read_result(num_retries=5) except ProcessTerminationError as ex: pass except ChannelError as ex: # Was interrupted five times in a row! Ignore for now logger.debug('client failed to read sentinel from channel after 5 retries - will terminate anyway') self.state = ClientState.TERMINATED_CLEANLY
[ "def", "terminate", "(", "self", ")", ":", "logger", ".", "debug", "(", "'client.terminate() called (state=%s)'", ",", "self", ".", "strstate", ")", "if", "self", ".", "state", "==", "ClientState", ".", "WAITING_FOR_RESULT", ":", "raise", "ClientStateError", "(", "'terimate() called while state='", "+", "self", ".", "strstate", ")", "if", "self", ".", "state", "==", "ClientState", ".", "TERMINATING", ":", "raise", "ClientStateError", "(", "'terimate() called while state='", "+", "self", ".", "strstate", ")", "elif", "self", ".", "state", "in", "ClientState", ".", "TerminatedSet", ":", "assert", "not", "self", ".", "_server_process", ".", "is_alive", "(", ")", "return", "elif", "self", ".", "state", "==", "ClientState", ".", "READY", ":", "# Check that the process itself is still alive", "self", ".", "_assert_alive", "(", ")", "# Make sure the SIGCHLD signal handler doesn't throw any exceptions", "self", ".", "state", "=", "ClientState", ".", "TERMINATING", "# Do not call execute() because that function will check", "# whether the process is alive and throw an exception if not", "# TODO: can the queue itself throw exceptions?", "self", ".", "_delegate_channel", ".", "put", "(", "FunctionCallDelegate", "(", "_raise_terminate", ")", ")", "# Wait for acknowledgement", "try", ":", "self", ".", "_read_result", "(", "num_retries", "=", "5", ")", "except", "ProcessTerminationError", "as", "ex", ":", "pass", "except", "ChannelError", "as", "ex", ":", "# Was interrupted five times in a row! Ignore for now", "logger", ".", "debug", "(", "'client failed to read sentinel from channel after 5 retries - will terminate anyway'", ")", "self", ".", "state", "=", "ClientState", ".", "TERMINATED_CLEANLY" ]
Stop the server process and change our state to TERMINATING. Only valid if state=READY.
[ "Stop", "the", "server", "process", "and", "change", "our", "state", "to", "TERMINATING", ".", "Only", "valid", "if", "state", "=", "READY", "." ]
1b09862a5ed63be71049dfa8ad22f7c5fc75745c
https://github.com/alexflint/process-isolation/blob/1b09862a5ed63be71049dfa8ad22f7c5fc75745c/process_isolation.py#L808-L839
3,979
alexflint/process-isolation
process_isolation.py
Client.cleanup
def cleanup(self): '''Terminate this client if it has not already terminated.''' if self.state == ClientState.WAITING_FOR_RESULT: # There is an ongoing call to execute() # Not sure what to do here logger.warn('cleanup() called while state is WAITING_FOR_RESULT: ignoring') elif self.state == ClientState.TERMINATING: # terminate() has been called but we have not recieved SIGCHLD yet # Not sure what to do here logger.warn('cleanup() called while state is TERMINATING: ignoring') elif self.state in ClientState.TerminatedSet: # We have already terminated # TODO: should we deal with TERMINATED_ASYNC in some special way? logger.debug('cleanup() called while state is TERMINATING: nothing needs to be done') else: logger.debug('cleanup() called while state is %s: attempting to terminate', self.strstate) try: self.terminate() except ProcessTerminationError as ex: # Terminate can throw a ProcessTerminationError if the # process terminated at some point between the last # execute() and the call to terminate() # For now we just ignore this. pass
python
def cleanup(self): '''Terminate this client if it has not already terminated.''' if self.state == ClientState.WAITING_FOR_RESULT: # There is an ongoing call to execute() # Not sure what to do here logger.warn('cleanup() called while state is WAITING_FOR_RESULT: ignoring') elif self.state == ClientState.TERMINATING: # terminate() has been called but we have not recieved SIGCHLD yet # Not sure what to do here logger.warn('cleanup() called while state is TERMINATING: ignoring') elif self.state in ClientState.TerminatedSet: # We have already terminated # TODO: should we deal with TERMINATED_ASYNC in some special way? logger.debug('cleanup() called while state is TERMINATING: nothing needs to be done') else: logger.debug('cleanup() called while state is %s: attempting to terminate', self.strstate) try: self.terminate() except ProcessTerminationError as ex: # Terminate can throw a ProcessTerminationError if the # process terminated at some point between the last # execute() and the call to terminate() # For now we just ignore this. pass
[ "def", "cleanup", "(", "self", ")", ":", "if", "self", ".", "state", "==", "ClientState", ".", "WAITING_FOR_RESULT", ":", "# There is an ongoing call to execute()", "# Not sure what to do here", "logger", ".", "warn", "(", "'cleanup() called while state is WAITING_FOR_RESULT: ignoring'", ")", "elif", "self", ".", "state", "==", "ClientState", ".", "TERMINATING", ":", "# terminate() has been called but we have not recieved SIGCHLD yet", "# Not sure what to do here", "logger", ".", "warn", "(", "'cleanup() called while state is TERMINATING: ignoring'", ")", "elif", "self", ".", "state", "in", "ClientState", ".", "TerminatedSet", ":", "# We have already terminated", "# TODO: should we deal with TERMINATED_ASYNC in some special way?", "logger", ".", "debug", "(", "'cleanup() called while state is TERMINATING: nothing needs to be done'", ")", "else", ":", "logger", ".", "debug", "(", "'cleanup() called while state is %s: attempting to terminate'", ",", "self", ".", "strstate", ")", "try", ":", "self", ".", "terminate", "(", ")", "except", "ProcessTerminationError", "as", "ex", ":", "# Terminate can throw a ProcessTerminationError if the", "# process terminated at some point between the last", "# execute() and the call to terminate()", "# For now we just ignore this.", "pass" ]
Terminate this client if it has not already terminated.
[ "Terminate", "this", "client", "if", "it", "has", "not", "already", "terminated", "." ]
1b09862a5ed63be71049dfa8ad22f7c5fc75745c
https://github.com/alexflint/process-isolation/blob/1b09862a5ed63be71049dfa8ad22f7c5fc75745c/process_isolation.py#L841-L865
3,980
alexflint/process-isolation
process_isolation.py
IsolationContext.start
def start(self): '''Create a process in which the isolated code will be run.''' assert self._client is None logger.debug('IsolationContext[%d] starting', id(self)) # Create the queues request_queue = multiprocessing.Queue() response_queue = multiprocessing.Queue() # Launch the server process server = Server(request_queue, response_queue) # Do not keep a reference to this object! server_process = multiprocessing.Process(target=server.loop) server_process.start() # Create a client to talk to the server self._client = Client(server_process, request_queue, response_queue)
python
def start(self): '''Create a process in which the isolated code will be run.''' assert self._client is None logger.debug('IsolationContext[%d] starting', id(self)) # Create the queues request_queue = multiprocessing.Queue() response_queue = multiprocessing.Queue() # Launch the server process server = Server(request_queue, response_queue) # Do not keep a reference to this object! server_process = multiprocessing.Process(target=server.loop) server_process.start() # Create a client to talk to the server self._client = Client(server_process, request_queue, response_queue)
[ "def", "start", "(", "self", ")", ":", "assert", "self", ".", "_client", "is", "None", "logger", ".", "debug", "(", "'IsolationContext[%d] starting'", ",", "id", "(", "self", ")", ")", "# Create the queues", "request_queue", "=", "multiprocessing", ".", "Queue", "(", ")", "response_queue", "=", "multiprocessing", ".", "Queue", "(", ")", "# Launch the server process", "server", "=", "Server", "(", "request_queue", ",", "response_queue", ")", "# Do not keep a reference to this object!", "server_process", "=", "multiprocessing", ".", "Process", "(", "target", "=", "server", ".", "loop", ")", "server_process", ".", "start", "(", ")", "# Create a client to talk to the server", "self", ".", "_client", "=", "Client", "(", "server_process", ",", "request_queue", ",", "response_queue", ")" ]
Create a process in which the isolated code will be run.
[ "Create", "a", "process", "in", "which", "the", "isolated", "code", "will", "be", "run", "." ]
1b09862a5ed63be71049dfa8ad22f7c5fc75745c
https://github.com/alexflint/process-isolation/blob/1b09862a5ed63be71049dfa8ad22f7c5fc75745c/process_isolation.py#L898-L914
3,981
alexflint/process-isolation
process_isolation.py
IsolationContext.load_module
def load_module(self, module_name, path=None): '''Import a module into this isolation context and return a proxy for it.''' self.ensure_started() if path is None: path = sys.path mod = self.client.call(_load_module, module_name, path) mod.__isolation_context__ = self return mod
python
def load_module(self, module_name, path=None): '''Import a module into this isolation context and return a proxy for it.''' self.ensure_started() if path is None: path = sys.path mod = self.client.call(_load_module, module_name, path) mod.__isolation_context__ = self return mod
[ "def", "load_module", "(", "self", ",", "module_name", ",", "path", "=", "None", ")", ":", "self", ".", "ensure_started", "(", ")", "if", "path", "is", "None", ":", "path", "=", "sys", ".", "path", "mod", "=", "self", ".", "client", ".", "call", "(", "_load_module", ",", "module_name", ",", "path", ")", "mod", ".", "__isolation_context__", "=", "self", "return", "mod" ]
Import a module into this isolation context and return a proxy for it.
[ "Import", "a", "module", "into", "this", "isolation", "context", "and", "return", "a", "proxy", "for", "it", "." ]
1b09862a5ed63be71049dfa8ad22f7c5fc75745c
https://github.com/alexflint/process-isolation/blob/1b09862a5ed63be71049dfa8ad22f7c5fc75745c/process_isolation.py#L921-L928
3,982
smarie/python-autoclass
autoclass/autoprops_.py
_has_annotation
def _has_annotation(annotation, value): """ Returns a function that can be used as a predicate in get_members, that """ def matches_property_name(fun): """ return true if fun is a callable that has the correct annotation with value """ return callable(fun) and hasattr(fun, annotation) \ and getattr(fun, annotation) is value return matches_property_name
python
def _has_annotation(annotation, value): def matches_property_name(fun): """ return true if fun is a callable that has the correct annotation with value """ return callable(fun) and hasattr(fun, annotation) \ and getattr(fun, annotation) is value return matches_property_name
[ "def", "_has_annotation", "(", "annotation", ",", "value", ")", ":", "def", "matches_property_name", "(", "fun", ")", ":", "\"\"\" return true if fun is a callable that has the correct annotation with value \"\"\"", "return", "callable", "(", "fun", ")", "and", "hasattr", "(", "fun", ",", "annotation", ")", "and", "getattr", "(", "fun", ",", "annotation", ")", "is", "value", "return", "matches_property_name" ]
Returns a function that can be used as a predicate in get_members, that
[ "Returns", "a", "function", "that", "can", "be", "used", "as", "a", "predicate", "in", "get_members", "that" ]
097098776c69ebc87bc1aeda6997431b29bd583a
https://github.com/smarie/python-autoclass/blob/097098776c69ebc87bc1aeda6997431b29bd583a/autoclass/autoprops_.py#L218-L225
3,983
smarie/python-autoclass
autoclass/autoprops_.py
_get_getter_fun
def _get_getter_fun(object_type, # type: Type parameter, # type: Parameter private_property_name # type: str ): """ Utility method to find the overridden getter function for a given property, or generate a new one :param object_type: :param property_name: :param private_property_name: :return: """ property_name = parameter.name # -- check overridden getter for this property name overridden_getters = getmembers(object_type, predicate=_has_annotation(__GETTER_OVERRIDE_ANNOTATION, property_name)) if len(overridden_getters) > 0: if len(overridden_getters) > 1: raise DuplicateOverrideError('Getter is overridden more than once for attribute name : ' + property_name) # --use the overridden getter getter_fun = overridden_getters[0][1] # --check its signature s = signature(getter_fun) if not ('self' in s.parameters.keys() and len(s.parameters.keys()) == 1): raise IllegalGetterSignatureException('overridden getter must only have a self parameter, found ' + str(len(s.parameters.items()) - 1) + ' for function ' + str( getter_fun.__qualname__)) # --use the overridden getter ? property_obj = property(getter_fun) else: # -- generate the getter : def autoprops_generated_getter(self): return getattr(self, private_property_name) # -- use the generated getter getter_fun = autoprops_generated_getter try: annotations = getter_fun.__annotations__ except AttributeError: # python 2 pass else: annotations['return'] = parameter.annotation # add type hint to output declaration return getter_fun
python
def _get_getter_fun(object_type, # type: Type parameter, # type: Parameter private_property_name # type: str ): property_name = parameter.name # -- check overridden getter for this property name overridden_getters = getmembers(object_type, predicate=_has_annotation(__GETTER_OVERRIDE_ANNOTATION, property_name)) if len(overridden_getters) > 0: if len(overridden_getters) > 1: raise DuplicateOverrideError('Getter is overridden more than once for attribute name : ' + property_name) # --use the overridden getter getter_fun = overridden_getters[0][1] # --check its signature s = signature(getter_fun) if not ('self' in s.parameters.keys() and len(s.parameters.keys()) == 1): raise IllegalGetterSignatureException('overridden getter must only have a self parameter, found ' + str(len(s.parameters.items()) - 1) + ' for function ' + str( getter_fun.__qualname__)) # --use the overridden getter ? property_obj = property(getter_fun) else: # -- generate the getter : def autoprops_generated_getter(self): return getattr(self, private_property_name) # -- use the generated getter getter_fun = autoprops_generated_getter try: annotations = getter_fun.__annotations__ except AttributeError: # python 2 pass else: annotations['return'] = parameter.annotation # add type hint to output declaration return getter_fun
[ "def", "_get_getter_fun", "(", "object_type", ",", "# type: Type", "parameter", ",", "# type: Parameter", "private_property_name", "# type: str", ")", ":", "property_name", "=", "parameter", ".", "name", "# -- check overridden getter for this property name", "overridden_getters", "=", "getmembers", "(", "object_type", ",", "predicate", "=", "_has_annotation", "(", "__GETTER_OVERRIDE_ANNOTATION", ",", "property_name", ")", ")", "if", "len", "(", "overridden_getters", ")", ">", "0", ":", "if", "len", "(", "overridden_getters", ")", ">", "1", ":", "raise", "DuplicateOverrideError", "(", "'Getter is overridden more than once for attribute name : '", "+", "property_name", ")", "# --use the overridden getter", "getter_fun", "=", "overridden_getters", "[", "0", "]", "[", "1", "]", "# --check its signature", "s", "=", "signature", "(", "getter_fun", ")", "if", "not", "(", "'self'", "in", "s", ".", "parameters", ".", "keys", "(", ")", "and", "len", "(", "s", ".", "parameters", ".", "keys", "(", ")", ")", "==", "1", ")", ":", "raise", "IllegalGetterSignatureException", "(", "'overridden getter must only have a self parameter, found '", "+", "str", "(", "len", "(", "s", ".", "parameters", ".", "items", "(", ")", ")", "-", "1", ")", "+", "' for function '", "+", "str", "(", "getter_fun", ".", "__qualname__", ")", ")", "# --use the overridden getter ?", "property_obj", "=", "property", "(", "getter_fun", ")", "else", ":", "# -- generate the getter :", "def", "autoprops_generated_getter", "(", "self", ")", ":", "return", "getattr", "(", "self", ",", "private_property_name", ")", "# -- use the generated getter", "getter_fun", "=", "autoprops_generated_getter", "try", ":", "annotations", "=", "getter_fun", ".", "__annotations__", "except", "AttributeError", ":", "# python 2", "pass", "else", ":", "annotations", "[", "'return'", "]", "=", "parameter", ".", "annotation", "# add type hint to output declaration", "return", "getter_fun" ]
Utility method to find the overridden getter function for a given property, or generate a new one :param object_type: :param property_name: :param private_property_name: :return:
[ "Utility", "method", "to", "find", "the", "overridden", "getter", "function", "for", "a", "given", "property", "or", "generate", "a", "new", "one" ]
097098776c69ebc87bc1aeda6997431b29bd583a
https://github.com/smarie/python-autoclass/blob/097098776c69ebc87bc1aeda6997431b29bd583a/autoclass/autoprops_.py#L228-L278
3,984
smarie/python-autoclass
autoclass/autoprops_.py
_get_setter_fun
def _get_setter_fun(object_type, # type: Type parameter, # type: Parameter private_property_name # type: str ): """ Utility method to find the overridden setter function for a given property, or generate a new one :param object_type: :param property_name: :param property_type: :param private_property_name: :return: """ # the property will have the same name than the constructor argument property_name = parameter.name overridden_setters = getmembers(object_type, _has_annotation(__SETTER_OVERRIDE_ANNOTATION, property_name)) if len(overridden_setters) > 0: # --check that we only have one if len(overridden_setters) > 1: raise DuplicateOverrideError('Setter is overridden more than once for attribute name : %s' % property_name) # --use the overridden setter setter_fun = overridden_setters[0][1] try: # python 2 setter_fun = setter_fun.im_func except AttributeError: pass # --find the parameter name and check the signature s = signature(setter_fun) p = [attribute_name for attribute_name, param in s.parameters.items() if attribute_name is not 'self'] if len(p) != 1: try: qname = setter_fun.__qualname__ except AttributeError: qname = setter_fun.__name__ raise IllegalSetterSignatureException('overridden setter must have only 1 non-self argument, found ' + '%s for function %s' '' % (len(s.parameters.items()) - 1, qname)) var_name = p[0] else: # --create the setter, equivalent of: # ** Dynamically compile a wrapper with correct argument name ** sig = Signature(parameters=[Parameter('self', kind=Parameter.POSITIONAL_OR_KEYWORD), parameter]) @with_signature(sig) def autoprops_generated_setter(self, **kwargs): setattr(self, private_property_name, kwargs.popitem()[1]) setter_fun = autoprops_generated_setter var_name = property_name return setter_fun, var_name
python
def _get_setter_fun(object_type, # type: Type parameter, # type: Parameter private_property_name # type: str ): # the property will have the same name than the constructor argument property_name = parameter.name overridden_setters = getmembers(object_type, _has_annotation(__SETTER_OVERRIDE_ANNOTATION, property_name)) if len(overridden_setters) > 0: # --check that we only have one if len(overridden_setters) > 1: raise DuplicateOverrideError('Setter is overridden more than once for attribute name : %s' % property_name) # --use the overridden setter setter_fun = overridden_setters[0][1] try: # python 2 setter_fun = setter_fun.im_func except AttributeError: pass # --find the parameter name and check the signature s = signature(setter_fun) p = [attribute_name for attribute_name, param in s.parameters.items() if attribute_name is not 'self'] if len(p) != 1: try: qname = setter_fun.__qualname__ except AttributeError: qname = setter_fun.__name__ raise IllegalSetterSignatureException('overridden setter must have only 1 non-self argument, found ' + '%s for function %s' '' % (len(s.parameters.items()) - 1, qname)) var_name = p[0] else: # --create the setter, equivalent of: # ** Dynamically compile a wrapper with correct argument name ** sig = Signature(parameters=[Parameter('self', kind=Parameter.POSITIONAL_OR_KEYWORD), parameter]) @with_signature(sig) def autoprops_generated_setter(self, **kwargs): setattr(self, private_property_name, kwargs.popitem()[1]) setter_fun = autoprops_generated_setter var_name = property_name return setter_fun, var_name
[ "def", "_get_setter_fun", "(", "object_type", ",", "# type: Type", "parameter", ",", "# type: Parameter", "private_property_name", "# type: str", ")", ":", "# the property will have the same name than the constructor argument", "property_name", "=", "parameter", ".", "name", "overridden_setters", "=", "getmembers", "(", "object_type", ",", "_has_annotation", "(", "__SETTER_OVERRIDE_ANNOTATION", ",", "property_name", ")", ")", "if", "len", "(", "overridden_setters", ")", ">", "0", ":", "# --check that we only have one", "if", "len", "(", "overridden_setters", ")", ">", "1", ":", "raise", "DuplicateOverrideError", "(", "'Setter is overridden more than once for attribute name : %s'", "%", "property_name", ")", "# --use the overridden setter", "setter_fun", "=", "overridden_setters", "[", "0", "]", "[", "1", "]", "try", ":", "# python 2", "setter_fun", "=", "setter_fun", ".", "im_func", "except", "AttributeError", ":", "pass", "# --find the parameter name and check the signature", "s", "=", "signature", "(", "setter_fun", ")", "p", "=", "[", "attribute_name", "for", "attribute_name", ",", "param", "in", "s", ".", "parameters", ".", "items", "(", ")", "if", "attribute_name", "is", "not", "'self'", "]", "if", "len", "(", "p", ")", "!=", "1", ":", "try", ":", "qname", "=", "setter_fun", ".", "__qualname__", "except", "AttributeError", ":", "qname", "=", "setter_fun", ".", "__name__", "raise", "IllegalSetterSignatureException", "(", "'overridden setter must have only 1 non-self argument, found '", "+", "'%s for function %s'", "''", "%", "(", "len", "(", "s", ".", "parameters", ".", "items", "(", ")", ")", "-", "1", ",", "qname", ")", ")", "var_name", "=", "p", "[", "0", "]", "else", ":", "# --create the setter, equivalent of:", "# ** Dynamically compile a wrapper with correct argument name **", "sig", "=", "Signature", "(", "parameters", "=", "[", "Parameter", "(", "'self'", ",", "kind", "=", "Parameter", ".", "POSITIONAL_OR_KEYWORD", ")", ",", "parameter", "]", ")", "@", "with_signature", "(", "sig", ")", "def", "autoprops_generated_setter", "(", "self", ",", "*", "*", "kwargs", ")", ":", "setattr", "(", "self", ",", "private_property_name", ",", "kwargs", ".", "popitem", "(", ")", "[", "1", "]", ")", "setter_fun", "=", "autoprops_generated_setter", "var_name", "=", "property_name", "return", "setter_fun", ",", "var_name" ]
Utility method to find the overridden setter function for a given property, or generate a new one :param object_type: :param property_name: :param property_type: :param private_property_name: :return:
[ "Utility", "method", "to", "find", "the", "overridden", "setter", "function", "for", "a", "given", "property", "or", "generate", "a", "new", "one" ]
097098776c69ebc87bc1aeda6997431b29bd583a
https://github.com/smarie/python-autoclass/blob/097098776c69ebc87bc1aeda6997431b29bd583a/autoclass/autoprops_.py#L281-L338
3,985
smarie/python-autoclass
autoclass/autoprops_.py
getter_override
def getter_override(attribute=None, # type: str f=DECORATED ): """ A decorator to indicate an overridden getter for a given attribute. If the attribute name is None, the function name will be used as the attribute name. :param attribute: the attribute name for which the decorated function is an overridden getter :return: """ return autoprops_override_decorate(f, attribute=attribute, is_getter=True)
python
def getter_override(attribute=None, # type: str f=DECORATED ): return autoprops_override_decorate(f, attribute=attribute, is_getter=True)
[ "def", "getter_override", "(", "attribute", "=", "None", ",", "# type: str", "f", "=", "DECORATED", ")", ":", "return", "autoprops_override_decorate", "(", "f", ",", "attribute", "=", "attribute", ",", "is_getter", "=", "True", ")" ]
A decorator to indicate an overridden getter for a given attribute. If the attribute name is None, the function name will be used as the attribute name. :param attribute: the attribute name for which the decorated function is an overridden getter :return:
[ "A", "decorator", "to", "indicate", "an", "overridden", "getter", "for", "a", "given", "attribute", ".", "If", "the", "attribute", "name", "is", "None", "the", "function", "name", "will", "be", "used", "as", "the", "attribute", "name", "." ]
097098776c69ebc87bc1aeda6997431b29bd583a
https://github.com/smarie/python-autoclass/blob/097098776c69ebc87bc1aeda6997431b29bd583a/autoclass/autoprops_.py#L431-L441
3,986
smarie/python-autoclass
autoclass/utils.py
is_attr_selected
def is_attr_selected(attr_name, # type: str include=None, # type: Union[str, Tuple[str]] exclude=None # type: Union[str, Tuple[str]] ): """decide whether an action has to be performed on the attribute or not, based on its name""" if include is not None and exclude is not None: raise ValueError('Only one of \'include\' or \'exclude\' argument should be provided.') # win time by not doing this # check_var(include, var_name='include', var_types=[str, tuple], enforce_not_none=False) # check_var(exclude, var_name='exclude', var_types=[str, tuple], enforce_not_none=False) if attr_name is 'self': return False if exclude and attr_name in exclude: return False if not include or attr_name in include: return True else: return False
python
def is_attr_selected(attr_name, # type: str include=None, # type: Union[str, Tuple[str]] exclude=None # type: Union[str, Tuple[str]] ): if include is not None and exclude is not None: raise ValueError('Only one of \'include\' or \'exclude\' argument should be provided.') # win time by not doing this # check_var(include, var_name='include', var_types=[str, tuple], enforce_not_none=False) # check_var(exclude, var_name='exclude', var_types=[str, tuple], enforce_not_none=False) if attr_name is 'self': return False if exclude and attr_name in exclude: return False if not include or attr_name in include: return True else: return False
[ "def", "is_attr_selected", "(", "attr_name", ",", "# type: str", "include", "=", "None", ",", "# type: Union[str, Tuple[str]]", "exclude", "=", "None", "# type: Union[str, Tuple[str]]", ")", ":", "if", "include", "is", "not", "None", "and", "exclude", "is", "not", "None", ":", "raise", "ValueError", "(", "'Only one of \\'include\\' or \\'exclude\\' argument should be provided.'", ")", "# win time by not doing this", "# check_var(include, var_name='include', var_types=[str, tuple], enforce_not_none=False)", "# check_var(exclude, var_name='exclude', var_types=[str, tuple], enforce_not_none=False)", "if", "attr_name", "is", "'self'", ":", "return", "False", "if", "exclude", "and", "attr_name", "in", "exclude", ":", "return", "False", "if", "not", "include", "or", "attr_name", "in", "include", ":", "return", "True", "else", ":", "return", "False" ]
decide whether an action has to be performed on the attribute or not, based on its name
[ "decide", "whether", "an", "action", "has", "to", "be", "performed", "on", "the", "attribute", "or", "not", "based", "on", "its", "name" ]
097098776c69ebc87bc1aeda6997431b29bd583a
https://github.com/smarie/python-autoclass/blob/097098776c69ebc87bc1aeda6997431b29bd583a/autoclass/utils.py#L23-L43
3,987
smarie/python-autoclass
autoclass/utils.py
method_already_there
def method_already_there(object_type, method_name, this_class_only=False): """ Returns True if method `method_name` is already implemented by object_type, that is, its implementation differs from the one in `object`. :param object_type: :param method_name: :param this_class_only: :return: """ if this_class_only: return method_name in vars(object_type) # or object_type.__dict__ else: try: method = getattr(object_type, method_name) except AttributeError: return False else: return method is not None and method is not getattr(object, method_name, None)
python
def method_already_there(object_type, method_name, this_class_only=False): if this_class_only: return method_name in vars(object_type) # or object_type.__dict__ else: try: method = getattr(object_type, method_name) except AttributeError: return False else: return method is not None and method is not getattr(object, method_name, None)
[ "def", "method_already_there", "(", "object_type", ",", "method_name", ",", "this_class_only", "=", "False", ")", ":", "if", "this_class_only", ":", "return", "method_name", "in", "vars", "(", "object_type", ")", "# or object_type.__dict__", "else", ":", "try", ":", "method", "=", "getattr", "(", "object_type", ",", "method_name", ")", "except", "AttributeError", ":", "return", "False", "else", ":", "return", "method", "is", "not", "None", "and", "method", "is", "not", "getattr", "(", "object", ",", "method_name", ",", "None", ")" ]
Returns True if method `method_name` is already implemented by object_type, that is, its implementation differs from the one in `object`. :param object_type: :param method_name: :param this_class_only: :return:
[ "Returns", "True", "if", "method", "method_name", "is", "already", "implemented", "by", "object_type", "that", "is", "its", "implementation", "differs", "from", "the", "one", "in", "object", "." ]
097098776c69ebc87bc1aeda6997431b29bd583a
https://github.com/smarie/python-autoclass/blob/097098776c69ebc87bc1aeda6997431b29bd583a/autoclass/utils.py#L88-L106
3,988
planetarypy/pvl
pvl/_collections.py
OrderedMultiDict.extend
def extend(self, *args, **kwargs): """Add key value pairs for an iterable.""" if len(args) > 1: raise TypeError('expected at most 1 arguments, got %d' % len(args)) iterable = args[0] if args else None if iterable: if isinstance(iterable, Mapping) or hasattr(iterable, 'items'): for key, value in iterable.items(): self.append(key, value) elif hasattr(iterable, 'keys'): for key in iterable.keys(): self.append(key, iterable[key]) else: for key, value in iterable: self.append(key, value) for key, value in kwargs.items(): self.append(key, value)
python
def extend(self, *args, **kwargs): if len(args) > 1: raise TypeError('expected at most 1 arguments, got %d' % len(args)) iterable = args[0] if args else None if iterable: if isinstance(iterable, Mapping) or hasattr(iterable, 'items'): for key, value in iterable.items(): self.append(key, value) elif hasattr(iterable, 'keys'): for key in iterable.keys(): self.append(key, iterable[key]) else: for key, value in iterable: self.append(key, value) for key, value in kwargs.items(): self.append(key, value)
[ "def", "extend", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "if", "len", "(", "args", ")", ">", "1", ":", "raise", "TypeError", "(", "'expected at most 1 arguments, got %d'", "%", "len", "(", "args", ")", ")", "iterable", "=", "args", "[", "0", "]", "if", "args", "else", "None", "if", "iterable", ":", "if", "isinstance", "(", "iterable", ",", "Mapping", ")", "or", "hasattr", "(", "iterable", ",", "'items'", ")", ":", "for", "key", ",", "value", "in", "iterable", ".", "items", "(", ")", ":", "self", ".", "append", "(", "key", ",", "value", ")", "elif", "hasattr", "(", "iterable", ",", "'keys'", ")", ":", "for", "key", "in", "iterable", ".", "keys", "(", ")", ":", "self", ".", "append", "(", "key", ",", "iterable", "[", "key", "]", ")", "else", ":", "for", "key", ",", "value", "in", "iterable", ":", "self", ".", "append", "(", "key", ",", "value", ")", "for", "key", ",", "value", "in", "kwargs", ".", "items", "(", ")", ":", "self", ".", "append", "(", "key", ",", "value", ")" ]
Add key value pairs for an iterable.
[ "Add", "key", "value", "pairs", "for", "an", "iterable", "." ]
ed92b284c4208439b033d28c9c176534c0faac0e
https://github.com/planetarypy/pvl/blob/ed92b284c4208439b033d28c9c176534c0faac0e/pvl/_collections.py#L221-L239
3,989
planetarypy/pvl
pvl/_collections.py
OrderedMultiDict.__insert_wrapper
def __insert_wrapper(func): """Make sure the arguments given to the insert methods are correct""" def check_func(self, key, new_item, instance=0): if key not in self.keys(): raise KeyError("%s not a key in label" % (key)) if not isinstance(new_item, (list, OrderedMultiDict)): raise TypeError("The new item must be a list or PVLModule") if isinstance(new_item, OrderedMultiDict): new_item = list(new_item) return func(self, key, new_item, instance) return check_func
python
def __insert_wrapper(func): def check_func(self, key, new_item, instance=0): if key not in self.keys(): raise KeyError("%s not a key in label" % (key)) if not isinstance(new_item, (list, OrderedMultiDict)): raise TypeError("The new item must be a list or PVLModule") if isinstance(new_item, OrderedMultiDict): new_item = list(new_item) return func(self, key, new_item, instance) return check_func
[ "def", "__insert_wrapper", "(", "func", ")", ":", "def", "check_func", "(", "self", ",", "key", ",", "new_item", ",", "instance", "=", "0", ")", ":", "if", "key", "not", "in", "self", ".", "keys", "(", ")", ":", "raise", "KeyError", "(", "\"%s not a key in label\"", "%", "(", "key", ")", ")", "if", "not", "isinstance", "(", "new_item", ",", "(", "list", ",", "OrderedMultiDict", ")", ")", ":", "raise", "TypeError", "(", "\"The new item must be a list or PVLModule\"", ")", "if", "isinstance", "(", "new_item", ",", "OrderedMultiDict", ")", ":", "new_item", "=", "list", "(", "new_item", ")", "return", "func", "(", "self", ",", "key", ",", "new_item", ",", "instance", ")", "return", "check_func" ]
Make sure the arguments given to the insert methods are correct
[ "Make", "sure", "the", "arguments", "given", "to", "the", "insert", "methods", "are", "correct" ]
ed92b284c4208439b033d28c9c176534c0faac0e
https://github.com/planetarypy/pvl/blob/ed92b284c4208439b033d28c9c176534c0faac0e/pvl/_collections.py#L265-L275
3,990
planetarypy/pvl
pvl/_collections.py
OrderedMultiDict._get_index_for_insert
def _get_index_for_insert(self, key, instance): """Get the index of the key to insert before or after""" if instance == 0: # Index method will return the first occurence of the key index = self.keys().index(key) else: occurrence = -1 for index, k in enumerate(self.keys()): if k == key: occurrence += 1 if occurrence == instance: # Found the key and the correct occurence of the key break if occurrence != instance: # Gone through the entire list of keys and the instance number # given is too high for the number of occurences of the key raise ValueError( ( "Cannot insert before/after the %d " "instance of the key '%s' since there are " "only %d occurences of the key" % ( instance, key, occurrence) )) return index
python
def _get_index_for_insert(self, key, instance): if instance == 0: # Index method will return the first occurence of the key index = self.keys().index(key) else: occurrence = -1 for index, k in enumerate(self.keys()): if k == key: occurrence += 1 if occurrence == instance: # Found the key and the correct occurence of the key break if occurrence != instance: # Gone through the entire list of keys and the instance number # given is too high for the number of occurences of the key raise ValueError( ( "Cannot insert before/after the %d " "instance of the key '%s' since there are " "only %d occurences of the key" % ( instance, key, occurrence) )) return index
[ "def", "_get_index_for_insert", "(", "self", ",", "key", ",", "instance", ")", ":", "if", "instance", "==", "0", ":", "# Index method will return the first occurence of the key", "index", "=", "self", ".", "keys", "(", ")", ".", "index", "(", "key", ")", "else", ":", "occurrence", "=", "-", "1", "for", "index", ",", "k", "in", "enumerate", "(", "self", ".", "keys", "(", ")", ")", ":", "if", "k", "==", "key", ":", "occurrence", "+=", "1", "if", "occurrence", "==", "instance", ":", "# Found the key and the correct occurence of the key", "break", "if", "occurrence", "!=", "instance", ":", "# Gone through the entire list of keys and the instance number", "# given is too high for the number of occurences of the key", "raise", "ValueError", "(", "(", "\"Cannot insert before/after the %d \"", "\"instance of the key '%s' since there are \"", "\"only %d occurences of the key\"", "%", "(", "instance", ",", "key", ",", "occurrence", ")", ")", ")", "return", "index" ]
Get the index of the key to insert before or after
[ "Get", "the", "index", "of", "the", "key", "to", "insert", "before", "or", "after" ]
ed92b284c4208439b033d28c9c176534c0faac0e
https://github.com/planetarypy/pvl/blob/ed92b284c4208439b033d28c9c176534c0faac0e/pvl/_collections.py#L277-L301
3,991
planetarypy/pvl
pvl/_collections.py
OrderedMultiDict._insert_item
def _insert_item(self, key, new_item, instance, is_after): """Insert a new item before or after another item""" index = self._get_index_for_insert(key, instance) index = index + 1 if is_after else index self.__items = self.__items[:index] + new_item + self.__items[index:] # Make sure indexing works with new items for new_key, new_value in new_item: if new_key in self: value_list = [val for k, val in self.__items if k == new_key] dict_setitem(self, new_key, value_list) else: dict_setitem(self, new_key, [new_value])
python
def _insert_item(self, key, new_item, instance, is_after): index = self._get_index_for_insert(key, instance) index = index + 1 if is_after else index self.__items = self.__items[:index] + new_item + self.__items[index:] # Make sure indexing works with new items for new_key, new_value in new_item: if new_key in self: value_list = [val for k, val in self.__items if k == new_key] dict_setitem(self, new_key, value_list) else: dict_setitem(self, new_key, [new_value])
[ "def", "_insert_item", "(", "self", ",", "key", ",", "new_item", ",", "instance", ",", "is_after", ")", ":", "index", "=", "self", ".", "_get_index_for_insert", "(", "key", ",", "instance", ")", "index", "=", "index", "+", "1", "if", "is_after", "else", "index", "self", ".", "__items", "=", "self", ".", "__items", "[", ":", "index", "]", "+", "new_item", "+", "self", ".", "__items", "[", "index", ":", "]", "# Make sure indexing works with new items", "for", "new_key", ",", "new_value", "in", "new_item", ":", "if", "new_key", "in", "self", ":", "value_list", "=", "[", "val", "for", "k", ",", "val", "in", "self", ".", "__items", "if", "k", "==", "new_key", "]", "dict_setitem", "(", "self", ",", "new_key", ",", "value_list", ")", "else", ":", "dict_setitem", "(", "self", ",", "new_key", ",", "[", "new_value", "]", ")" ]
Insert a new item before or after another item
[ "Insert", "a", "new", "item", "before", "or", "after", "another", "item" ]
ed92b284c4208439b033d28c9c176534c0faac0e
https://github.com/planetarypy/pvl/blob/ed92b284c4208439b033d28c9c176534c0faac0e/pvl/_collections.py#L303-L314
3,992
planetarypy/pvl
pvl/_collections.py
OrderedMultiDict.insert_after
def insert_after(self, key, new_item, instance=0): """Insert an item after a key""" self._insert_item(key, new_item, instance, True)
python
def insert_after(self, key, new_item, instance=0): self._insert_item(key, new_item, instance, True)
[ "def", "insert_after", "(", "self", ",", "key", ",", "new_item", ",", "instance", "=", "0", ")", ":", "self", ".", "_insert_item", "(", "key", ",", "new_item", ",", "instance", ",", "True", ")" ]
Insert an item after a key
[ "Insert", "an", "item", "after", "a", "key" ]
ed92b284c4208439b033d28c9c176534c0faac0e
https://github.com/planetarypy/pvl/blob/ed92b284c4208439b033d28c9c176534c0faac0e/pvl/_collections.py#L317-L319
3,993
planetarypy/pvl
pvl/_collections.py
OrderedMultiDict.insert_before
def insert_before(self, key, new_item, instance=0): """Insert an item before a key""" self._insert_item(key, new_item, instance, False)
python
def insert_before(self, key, new_item, instance=0): self._insert_item(key, new_item, instance, False)
[ "def", "insert_before", "(", "self", ",", "key", ",", "new_item", ",", "instance", "=", "0", ")", ":", "self", ".", "_insert_item", "(", "key", ",", "new_item", ",", "instance", ",", "False", ")" ]
Insert an item before a key
[ "Insert", "an", "item", "before", "a", "key" ]
ed92b284c4208439b033d28c9c176534c0faac0e
https://github.com/planetarypy/pvl/blob/ed92b284c4208439b033d28c9c176534c0faac0e/pvl/_collections.py#L322-L324
3,994
planetarypy/pvl
pvl/stream.py
ByteStream.peek
def peek(self, n): """Returns buffered bytes without advancing the position. The argument indicates a desired minimal number of bytes; we do at most one raw read to satisfy it. We never return more than self.buffer_size. """ pos = self._read_pos end = pos + n return self.raw[pos:end]
python
def peek(self, n): pos = self._read_pos end = pos + n return self.raw[pos:end]
[ "def", "peek", "(", "self", ",", "n", ")", ":", "pos", "=", "self", ".", "_read_pos", "end", "=", "pos", "+", "n", "return", "self", ".", "raw", "[", "pos", ":", "end", "]" ]
Returns buffered bytes without advancing the position. The argument indicates a desired minimal number of bytes; we do at most one raw read to satisfy it. We never return more than self.buffer_size.
[ "Returns", "buffered", "bytes", "without", "advancing", "the", "position", ".", "The", "argument", "indicates", "a", "desired", "minimal", "number", "of", "bytes", ";", "we", "do", "at", "most", "one", "raw", "read", "to", "satisfy", "it", ".", "We", "never", "return", "more", "than", "self", ".", "buffer_size", "." ]
ed92b284c4208439b033d28c9c176534c0faac0e
https://github.com/planetarypy/pvl/blob/ed92b284c4208439b033d28c9c176534c0faac0e/pvl/stream.py#L126-L134
3,995
planetarypy/pvl
pvl/decoder.py
PVLDecoder.parse_group
def parse_group(self, stream): """Block Name must match Block Name in paired End Group Statement if Block Name is present in End Group Statement. BeginGroupStmt ::= BeginGroupKeywd WSC AssignmentSymbol WSC BlockName StatementDelim """ self.expect_in(stream, self.begin_group_tokens) self.ensure_assignment(stream) name = self.next_token(stream) self.skip_statement_delimiter(stream) statements = self.parse_block(stream, self.has_end_group) self.expect_in(stream, self.end_group_tokens) self.parse_end_assignment(stream, name) self.skip_statement_delimiter(stream) return name.decode('utf-8'), PVLGroup(statements)
python
def parse_group(self, stream): self.expect_in(stream, self.begin_group_tokens) self.ensure_assignment(stream) name = self.next_token(stream) self.skip_statement_delimiter(stream) statements = self.parse_block(stream, self.has_end_group) self.expect_in(stream, self.end_group_tokens) self.parse_end_assignment(stream, name) self.skip_statement_delimiter(stream) return name.decode('utf-8'), PVLGroup(statements)
[ "def", "parse_group", "(", "self", ",", "stream", ")", ":", "self", ".", "expect_in", "(", "stream", ",", "self", ".", "begin_group_tokens", ")", "self", ".", "ensure_assignment", "(", "stream", ")", "name", "=", "self", ".", "next_token", "(", "stream", ")", "self", ".", "skip_statement_delimiter", "(", "stream", ")", "statements", "=", "self", ".", "parse_block", "(", "stream", ",", "self", ".", "has_end_group", ")", "self", ".", "expect_in", "(", "stream", ",", "self", ".", "end_group_tokens", ")", "self", ".", "parse_end_assignment", "(", "stream", ",", "name", ")", "self", ".", "skip_statement_delimiter", "(", "stream", ")", "return", "name", ".", "decode", "(", "'utf-8'", ")", ",", "PVLGroup", "(", "statements", ")" ]
Block Name must match Block Name in paired End Group Statement if Block Name is present in End Group Statement. BeginGroupStmt ::= BeginGroupKeywd WSC AssignmentSymbol WSC BlockName StatementDelim
[ "Block", "Name", "must", "match", "Block", "Name", "in", "paired", "End", "Group", "Statement", "if", "Block", "Name", "is", "present", "in", "End", "Group", "Statement", "." ]
ed92b284c4208439b033d28c9c176534c0faac0e
https://github.com/planetarypy/pvl/blob/ed92b284c4208439b033d28c9c176534c0faac0e/pvl/decoder.py#L402-L421
3,996
planetarypy/pvl
pvl/decoder.py
PVLDecoder.parse_object
def parse_object(self, stream): """Block Name must match Block Name in paired End Object Statement if Block Name is present in End Object Statement StatementDelim. BeginObjectStmt ::= BeginObjectKeywd WSC AssignmentSymbol WSC BlockName StatementDelim """ self.expect_in(stream, self.begin_object_tokens) self.ensure_assignment(stream) name = self.next_token(stream) self.skip_statement_delimiter(stream) statements = self.parse_block(stream, self.has_end_object) self.expect_in(stream, self.end_object_tokens) self.parse_end_assignment(stream, name) self.skip_statement_delimiter(stream) return name.decode('utf-8'), PVLObject(statements)
python
def parse_object(self, stream): self.expect_in(stream, self.begin_object_tokens) self.ensure_assignment(stream) name = self.next_token(stream) self.skip_statement_delimiter(stream) statements = self.parse_block(stream, self.has_end_object) self.expect_in(stream, self.end_object_tokens) self.parse_end_assignment(stream, name) self.skip_statement_delimiter(stream) return name.decode('utf-8'), PVLObject(statements)
[ "def", "parse_object", "(", "self", ",", "stream", ")", ":", "self", ".", "expect_in", "(", "stream", ",", "self", ".", "begin_object_tokens", ")", "self", ".", "ensure_assignment", "(", "stream", ")", "name", "=", "self", ".", "next_token", "(", "stream", ")", "self", ".", "skip_statement_delimiter", "(", "stream", ")", "statements", "=", "self", ".", "parse_block", "(", "stream", ",", "self", ".", "has_end_object", ")", "self", ".", "expect_in", "(", "stream", ",", "self", ".", "end_object_tokens", ")", "self", ".", "parse_end_assignment", "(", "stream", ",", "name", ")", "self", ".", "skip_statement_delimiter", "(", "stream", ")", "return", "name", ".", "decode", "(", "'utf-8'", ")", ",", "PVLObject", "(", "statements", ")" ]
Block Name must match Block Name in paired End Object Statement if Block Name is present in End Object Statement StatementDelim. BeginObjectStmt ::= BeginObjectKeywd WSC AssignmentSymbol WSC BlockName StatementDelim
[ "Block", "Name", "must", "match", "Block", "Name", "in", "paired", "End", "Object", "Statement", "if", "Block", "Name", "is", "present", "in", "End", "Object", "Statement", "StatementDelim", "." ]
ed92b284c4208439b033d28c9c176534c0faac0e
https://github.com/planetarypy/pvl/blob/ed92b284c4208439b033d28c9c176534c0faac0e/pvl/decoder.py#L433-L452
3,997
BetterWorks/django-anonymizer
anonymizer/base.py
DjangoFaker.varchar
def varchar(self, field=None): """ Returns a chunk of text, of maximum length 'max_length' """ assert field is not None, "The field parameter must be passed to the 'varchar' method." max_length = field.max_length def source(): length = random.choice(range(1, max_length + 1)) return "".join(random.choice(general_chars) for i in xrange(length)) return self.get_allowed_value(source, field)
python
def varchar(self, field=None): assert field is not None, "The field parameter must be passed to the 'varchar' method." max_length = field.max_length def source(): length = random.choice(range(1, max_length + 1)) return "".join(random.choice(general_chars) for i in xrange(length)) return self.get_allowed_value(source, field)
[ "def", "varchar", "(", "self", ",", "field", "=", "None", ")", ":", "assert", "field", "is", "not", "None", ",", "\"The field parameter must be passed to the 'varchar' method.\"", "max_length", "=", "field", ".", "max_length", "def", "source", "(", ")", ":", "length", "=", "random", ".", "choice", "(", "range", "(", "1", ",", "max_length", "+", "1", ")", ")", "return", "\"\"", ".", "join", "(", "random", ".", "choice", "(", "general_chars", ")", "for", "i", "in", "xrange", "(", "length", ")", ")", "return", "self", ".", "get_allowed_value", "(", "source", ",", "field", ")" ]
Returns a chunk of text, of maximum length 'max_length'
[ "Returns", "a", "chunk", "of", "text", "of", "maximum", "length", "max_length" ]
2d25bb6e8b5e4230c58031c4b6d10cc536669b3e
https://github.com/BetterWorks/django-anonymizer/blob/2d25bb6e8b5e4230c58031c4b6d10cc536669b3e/anonymizer/base.py#L80-L90
3,998
BetterWorks/django-anonymizer
anonymizer/base.py
DjangoFaker.datetime
def datetime(self, field=None, val=None): """ Returns a random datetime. If 'val' is passed, a datetime within two years of that date will be returned. """ if val is None: def source(): tzinfo = get_default_timezone() if settings.USE_TZ else None return datetime.fromtimestamp(randrange(1, 2100000000), tzinfo) else: def source(): tzinfo = get_default_timezone() if settings.USE_TZ else None return datetime.fromtimestamp(int(val.strftime("%s")) + randrange(-365*24*3600*2, 365*24*3600*2), tzinfo) return self.get_allowed_value(source, field)
python
def datetime(self, field=None, val=None): if val is None: def source(): tzinfo = get_default_timezone() if settings.USE_TZ else None return datetime.fromtimestamp(randrange(1, 2100000000), tzinfo) else: def source(): tzinfo = get_default_timezone() if settings.USE_TZ else None return datetime.fromtimestamp(int(val.strftime("%s")) + randrange(-365*24*3600*2, 365*24*3600*2), tzinfo) return self.get_allowed_value(source, field)
[ "def", "datetime", "(", "self", ",", "field", "=", "None", ",", "val", "=", "None", ")", ":", "if", "val", "is", "None", ":", "def", "source", "(", ")", ":", "tzinfo", "=", "get_default_timezone", "(", ")", "if", "settings", ".", "USE_TZ", "else", "None", "return", "datetime", ".", "fromtimestamp", "(", "randrange", "(", "1", ",", "2100000000", ")", ",", "tzinfo", ")", "else", ":", "def", "source", "(", ")", ":", "tzinfo", "=", "get_default_timezone", "(", ")", "if", "settings", ".", "USE_TZ", "else", "None", "return", "datetime", ".", "fromtimestamp", "(", "int", "(", "val", ".", "strftime", "(", "\"%s\"", ")", ")", "+", "randrange", "(", "-", "365", "*", "24", "*", "3600", "*", "2", ",", "365", "*", "24", "*", "3600", "*", "2", ")", ",", "tzinfo", ")", "return", "self", ".", "get_allowed_value", "(", "source", ",", "field", ")" ]
Returns a random datetime. If 'val' is passed, a datetime within two years of that date will be returned.
[ "Returns", "a", "random", "datetime", ".", "If", "val", "is", "passed", "a", "datetime", "within", "two", "years", "of", "that", "date", "will", "be", "returned", "." ]
2d25bb6e8b5e4230c58031c4b6d10cc536669b3e
https://github.com/BetterWorks/django-anonymizer/blob/2d25bb6e8b5e4230c58031c4b6d10cc536669b3e/anonymizer/base.py#L117-L133
3,999
BetterWorks/django-anonymizer
anonymizer/base.py
DjangoFaker.date
def date(self, field=None, val=None): """ Like datetime, but truncated to be a date only """ return self.datetime(field=field, val=val).date()
python
def date(self, field=None, val=None): return self.datetime(field=field, val=val).date()
[ "def", "date", "(", "self", ",", "field", "=", "None", ",", "val", "=", "None", ")", ":", "return", "self", ".", "datetime", "(", "field", "=", "field", ",", "val", "=", "val", ")", ".", "date", "(", ")" ]
Like datetime, but truncated to be a date only
[ "Like", "datetime", "but", "truncated", "to", "be", "a", "date", "only" ]
2d25bb6e8b5e4230c58031c4b6d10cc536669b3e
https://github.com/BetterWorks/django-anonymizer/blob/2d25bb6e8b5e4230c58031c4b6d10cc536669b3e/anonymizer/base.py#L135-L139