Best Python code snippet using pypom_form_python
Monitor.py
Source:Monitor.py
1#!/usr/bin/env python32# -*- coding: utf-8 -*-3"""4Created on Fri Jun 22 10:52:06 20185@author: zhangzhanming6"""7import pandas as pd8import time9import threading10import datetime11class AccDataBase(object):12 feature_columns = ['START_TIME', 'STOP_TIME', 'MEAN_VM', 'STD_VM', 'MAX_VM', 'DOM_FREQ_VM',13 'DOM_FREQ_POWER_RATIO_VM', 'HIGHEND_FREQ_POWER_RATIO_VM', 'RANGE_VM',14 'ACTIVE_SAMPLE_PERC_VM', 'NUMBER_OF_ACTIVATIONS_VM',15 'ACTIVATION_INTERVAL_VAR_VM', 'MEDIAN_X_ANGLE', 'MEDIAN_Y_ANGLE',16 'MEDIAN_Z_ANGLE', 'RANGE_X_ANGLE', 'RANGE_Y_ANGLE', 'RANGE_Z_ANGLE']17 18 #initialized feature data with column names19 def __init__(self, monitor=None):20 self.monitor = monitor21 self.featuredata = pd.DataFrame(columns = self.feature_columns)22 self.annotationdata = pd.DataFrame()23 self.rawdata = pd.DataFrame()24 def set_data_set(self, featuredata=None, annotationdata=None, rawdata=None):25 if isinstance(featuredata, str):26 self.featuredata = pd.read_csv(featuredata)27 if isinstance(annotationdata, str):28 self.annotationdata = pd.read_csv(annotationdata)29 if isinstance(rawdata, str):30 self.rawdata = pd.read_csv(rawdata)31 if isinstance(featuredata, pd.DataFrame):32 self.featuredata = featuredata33 if isinstance(annotationdata, pd.DataFrame):34 self.annotationdata = annotationdata35 if isinstance(rawdata, pd.DataFrame):36 self.rawdata = rawdata37 def set_monitor(self, monitor):38 self.monitor = monitor39 def update(self, feature_update=None, annotation_update=None,40 raw_update=None):41 self.__append_data(feature_update, annotation_update, raw_update)42# =============================================================================43# self.__delete_data(feature_update is not None,44# annotation_update is not None,45# raw_update is not None)46# =============================================================================47 def __append_data(self, feature_update, annotation_update, raw_update):48 if feature_update is not None:49 if self.featuredata.shape[0] == 0:50 self.featuredata = feature_update51 else: 52 self.featuredata = pd.concat([self.featuredata, feature_update]).reset_index(drop=True)53 if annotation_update is not None:54 if self.annotationdata.shape[0] == 0:55 self.annotationdata = annotation_update56 else:57 self.annotationdata = pd.concat([self.annotationdata, annotation_update]).reset_index(drop=True)58 if raw_update is not None:59 if self.rawdata.shape[0] == 0:60 self.rawdata = raw_update61 else:62 self.rawdata = pd.concat([self.rawdata, raw_update]).reset_index(drop=True)63 def __delete_data(self, update_feature, update_annotation, update_raw):64 if update_feature:65 if len(self.featuredata.index):66 pass67class RepeatedTimer(object):68 def __init__(self, interval, function, *args, **kwargs):69 self.count = 070 self._timer = None71 self.interval = interval72 self.function = function73 self.args = args74 self.kwargs = kwargs75 self.is_running = False76 self.next_call = time.time()77 self.start()78 def _run(self):79 self.is_running = False80 self.start()81 self.function(self.count, *self.args, **self.kwargs)82 self.count += 183 def start(self):84 if not self.is_running:85 self.next_call += self.interval86 self._timer = threading.Timer(self.next_call - time.time(), self._run)87 self._timer.start()88 self.is_running = True89 def stop(self):90 self._timer.cancel()91 self.is_running = False92class Monitor(object):93 def __init__(self, refresh=12.5, sampling_rate=80, feature_time = 12.8,test=True):94 self.__observers = []95 self.feature_time = feature_time96 self.refresh = refresh97 self.sampling_rate = sampling_rate98 self.test = test99 def register_observer(self, observer):100 self.__observers.append(observer)101 def notify_observers(self, feature_update=None, annotation_update=None,102 raw_update=None):103 for observer in self.__observers:104 observer.update(feature_update=feature_update,105 annotation_update=annotation_update,106 raw_update=raw_update)107 def listen(self):108 if self.test:109 featuredata = pd.read_csv('/Users/zhangzhanming/Desktop/mHealth/Data/SPADES_2/Derived/Preprocessed/2015/10/08/14/ActigraphGT9X-PostureAndActivity-NA.TAS1E23150066-PostureAndActivity.2015-10-08-14-00-00-000-M0400.feature.csv')110 featuredata = featuredata.values.tolist()111 featuredata.sort(key=lambda x: x[1])112 #raw_annotation = pd.read_csv('/Users/zhangzhanming/Desktop/mHealth/Data/SPADES_2/MasterSynced/2015/10/08/14/splitted.annotation.csv')113 #classmapping = pd.read_csv('/Users/zhangzhanming/Desktop/mHealth/Data/SPADES_2/MasterSynced/2015/10/08/14/class_mapping.csv')114 #annotationdata = pd.merge(raw_annotation, classmapping, left_on='LABEL_NAME', right_on='activity', how='inner')115 #annotationdata = annotationdata.drop('activity', axis=1)116 annotationdata = pd.read_csv('/Users/zhangzhanming/Desktop/mHealth/Data/SPADES_2/Derived/Preprocessed/2015/10/08/14/SPADESInLab.alvin-SPADESInLab.2015-10-08-14-10-41-252-M0400.class.csv')117 annotationdata = annotationdata.values.tolist()118 # sort annotation data by end time, just like in real situations119 annotationdata.sort(key=lambda x: x[1])120 rawdata = pd.read_csv('/Users/zhangzhanming/Desktop/mHealth/Data/SPADES_2/Derived/Preprocessed/2015/10/08/14/ActigraphGT9X-AccelerationCalibrated-NA.TAS1E23150066-AccelerationCalibrated.2015-10-08-14-00-00-000-M0400.sensor.csv')121# =============================================================================122# last_time = pd.to_datetime(rawdata.iloc[0,0])123# feature_interval = datetime.timedelta(seconds=self.feature_time)124# threshold_time = last_time+feature_interval125# feature_count = 0126# 127# =============================================================================128 def __get_new_data(count, refresh, sampling_rate, rawdata):129 feature_update = []130 raw_update = rawdata[int(count*refresh/1000*sampling_rate):131 int((count+1)*refresh/1000*sampling_rate)]132 currtime = pd.to_datetime(raw_update.iloc[-1,0])133# =============================================================================134# nonlocal threshold_time135# if currtime > threshold_time:136# nonlocal feature_count137# feature_update = featuredata.iloc[feature_count:feature_count+1,:]138# threshold_time = currtime + feature_interval139# feature_count += 1140# =============================================================================141 annotation_update = []142 while currtime >= pd.to_datetime(annotationdata[0][1]):143 annotation_update.append(annotationdata.pop(0))144 feature_update.append(featuredata.pop(0))145 146 feature_update=pd.DataFrame(feature_update, 147 columns=['START_TIME', 'STOP_TIME', 'MEAN_VM', 'STD_VM', 'MAX_VM', 'DOM_FREQ_VM',148 'DOM_FREQ_POWER_RATIO_VM', 'HIGHEND_FREQ_POWER_RATIO_VM', 'RANGE_VM',149 'ACTIVE_SAMPLE_PERC_VM', 'NUMBER_OF_ACTIVATIONS_VM',150 'ACTIVATION_INTERVAL_VAR_VM', 'MEDIAN_X_ANGLE', 'MEDIAN_Y_ANGLE',151 'MEDIAN_Z_ANGLE', 'RANGE_X_ANGLE', 'RANGE_Y_ANGLE', 'RANGE_Z_ANGLE'])152 annotation_update=pd.DataFrame(annotation_update, 153 columns=['START_TIME', 'STOP_TIME', 'posture', 'four_classes', 'MDCAS',154 'indoor_outdoor', 'activity', 'activity_intensity', 'hand_gesture'])155 self.notify_observers(raw_update = raw_update, 156 feature_update = feature_update,157 annotation_update = annotation_update)158 self.rt = RepeatedTimer(self.refresh/1000, __get_new_data, self.refresh,159 self.sampling_rate, rawdata)160# =============================================================================161# try:162# time.sleep(64)163# finally:164# self.rt.stop()165# 166# =============================================================================167if __name__ == '__main__':168 monitor = Monitor()169 db = AccDataBase(monitor)170 monitor.register_observer(db)...
contrib.py
Source:contrib.py
...8 }9 update_query = {10 '$set': {field: field_value}11 }12 self.raw_update(filter_query, update_query)13 def add(self, field, object_pk, field_value, cnt_field=None):14 filter_query = {15 '_id': ObjectId(object_pk),16 field: {'$ne': field_value}17 }18 update_query = {19 '$addToSet': {field: field_value}20 }21 if cnt_field:22 update_query['$inc'] = {cnt_field: 1}23 self.raw_update(filter_query, update_query)24 def batch_add(self, field, object_pks, field_value, cnt_field=None):25 filter_query = {26 '_id': {'$in': map(lambda pk: ObjectId(pk), object_pks)},27 field: {'$ne': field_value}28 }29 update_query = {30 '$addToSet': {field: field_value}31 }32 if cnt_field:33 update_query['$inc'] = {cnt_field: 1}34 self.raw_update(filter_query, update_query)35 def remove(self, field, object_pk, field_value, cnt_field=None):36 filter_query = {37 '_id': ObjectId(object_pk),38 field: field_value39 }40 update_query = {41 '$pull': {field: field_value}42 }43 if cnt_field:44 update_query['$inc'] = {cnt_field: -1}45 self.raw_update(filter_query, update_query)46 def find_and_modify(self, collection_name, query={}, update=None, upsert=False, **kwargs):47 database_wrapper = connections['default']48 collection = database_wrapper.get_collection(collection_name)49 result = collection.find_and_modify(query, update=update, upsert=upsert, **kwargs)50 return result51 def pymongo_aggregate(self, collection_name, match=None, group=None, project=None, unwind=None, sort=None, extra=None):52 database_wrapper = connections['default']53 collection = database_wrapper.get_collection(collection_name)54 query = []55 if match:56 query.append({"$match": match})57 if project:58 query.append({"$project": project})59 if unwind:60 query.append({"$unwind": unwind})61 if group:62 query.append({"$group": group})63 if sort:64 query.append({"$sort": sort})65 if extra:66 query.append(extra)67 r = collection.aggregate(query)68 if r["ok"] == 1:69 return r["result"]70 else:71 return []72 def unset(self, field, object_pk):73 filter_query = {74 '_id': ObjectId(object_pk),75 }76 update_query = {77 '$unset': {field: 1}78 }79 self.raw_update(filter_query, update_query) 80 def batch_remove(self, field, object_pks, field_value, cnt_field=None):81 filter_query = {82 '_id': {'$in': map(lambda pk: ObjectId(pk), object_pks)},83 field: field_value84 }85 update_query = {86 '$pull': {field: field_value}87 }88 if cnt_field:89 update_query['$inc'] = {cnt_field: -1}90 self.raw_update(filter_query, update_query)91 def get_by_items(self, field, items):92 filter_query = {93 field: {94 '$all': items,95 '$size': len(items)96 }97 }98 return self.raw_query(filter_query)99 def clean_multiple_objects(self, **query):100 objs = self.filter(**query)101 obj = objs[0]102 for each_obj in objs[1:]:103 each_obj.delete()104 return obj...
gradient_descent.py
Source:gradient_descent.py
1import logging2import numpy as np3logger = logging.getLogger(__name__)4class GradientDescent(object):5 def __init__(self, learning_rate, inertia=0.0, annealing=np.Inf, last_learning_rate=None):6 self.last_learning_rate = last_learning_rate7 self.learning_rate = learning_rate8 self.inertia = inertia9 self.annealing = annealing10 self.last_update = None11 self.n_iter = 012 def transform(self, grad):13 """Individuals must be on the LAST dimensions"""14 p_anneal = self.n_iter / self.annealing15 if self.last_learning_rate is None:16 learning_rate = self.learning_rate / (1.0 + p_anneal)17 else:18 g = (self.last_learning_rate / self.annealing) ** min(p_anneal, 1.0)19 learning_rate = self.learning_rate * g20 self.n_iter += 1.021 raw_update = learning_rate * grad.mean(axis=-1)22 if self.last_update is None:23 self.last_update = raw_update24 self.last_update = (self.inertia * self.last_update +25 (1 - self.inertia) * raw_update)26 return self.last_update27 def __str__(self):28 return ("<GradientDescent"29 " learning_rate={learning_rate}"30 " last_learning_rate={last_learning_rate}"31 " inertia={inertia}"...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!