def main(): date_strftime_format = "%d-%b-%y %H:%M:%S" message_format = "%(asctime)s - %(levelname)s : %(message)s" logging.basicConfig(filename='log.log',level=logging.INFO,format=message_format, datefmt=date_strftime_format) LOGGER = logging.getLogger("main") Motor_control.rotate() Measurement.fun3() Calculations.fun1() plot.fun4() LOGGER.info('Master runs')
def add_measurement(self, request, autostart=True): assert isinstance(request, RequestParser.RequestParser), request measurement = Measurement.Measurement(self.adc, self.manager, self.lock, self.storage, request) self.measurement_list.append(measurement) if autostart: measurement.start() return measurement.id
def _strToPoints(self, unitsStr): """ Converts from a string such as '1em', '2pt', '3in', '5pc', or '20px' into a floating-point value measured in points. """ if unitsStr.endswith("em"): currEmSizeStr = self.__stack[-1]["font_size"] currEmSize = self._strToPoints(currEmSizeStr) units = float(unitsStr[:-2]) return units * currEmSize else: return Measurement.strToPoints(unitsStr)
def _strToPoints( self, unitsStr ): """ Converts from a string such as '1em', '2pt', '3in', '5pc', or '20px' into a floating-point value measured in points. """ if unitsStr.endswith( "em" ): currEmSizeStr = self.__stack[-1]["font_size"] currEmSize = self._strToPoints( currEmSizeStr ) units = float( unitsStr[:-2] ) return units * currEmSize else: return Measurement.strToPoints( unitsStr )
def deserializeRecipesFromFileNameJson(self, file_name): recipes = self.readJson(file_name) recipeList = [] for recipe in recipes: recipe_ingredients = [] ingredients = recipe["ingredients"] for ingredient_name, unit_string, amount in ingredients: unit = Unit.fromString(unit_string) ingredient = Ingredient(ingredient_name) measurement = Measurement(unit, amount) recipe_ingredient = RecipeIngredient(ingredient, measurement) recipe_ingredients.append(recipe_ingredient) recipe = Recipe(1, recipe["name"], recipe["num_servings"], recipe_ingredients, recipe["cuisine"], recipe["mealType"]) recipeList.append(recipe) return recipeList
0.01, 2, 100) wf = WorkerFilter() wf.add_configuration(1, conf) with open("out_600_sec") as f: data = json.load(f) init_time = None last = 0 mea_send = [] old_kalman = old_kalman.KalmanThread(None, old_q) old_kal_map = {} for d in data: if init_time is None: init_time = calendar.timegm(time.gmtime()) - d['t'] d['t'] += init_time mea = Measurement.Measurement(**d) mea_send.append((1, mea)) if not d['site'] in old_kal_map: old_kal_map[d['site']] = old_config.get_empty_kalman_state(run) old_kal_map[d['site']]['site'] = { 'name': d['site'], 'lat': None, 'lon': None } old_kal_map[d['site']]['data_set'].append(d) old_kalman.process_measurement(old_kal_map[d['site']]) if d['t'] > last: last = d['t'] #time.sleep(.8) a = [mea for conf, mea in wf.process_measurements(mea_send)] b = []
from matplotlib import pyplot as plt import Measurement def listdir_nohidden(path): for f in os.listdir(path): if not f.startswith('.'): yield f INPUT_FILE = '/Users/gghosal/Downloads/r3_high_std_err_plants.csv' BARCODE = '/Users/gghosal/Downloads/rep_3_barcode.csv' input_file = open(INPUT_FILE, "rt") barcode = open(BARCODE, 'rt') a = Measurement.MeasurementSystem([]) barcode_lines = barcode.readlines() barcode_lines = [i.rstrip() for i in barcode_lines] barcode_dict = dict([(i.split(",")[1], i.split(",")[0]) for i in barcode_lines]) input_lines = input_file.readlines()[1:] input_lines = [i.rstrip() for i in input_lines] for title in reversed(input_lines): # if title+".png" in os.lis18tdir("/Users/gghosal/Documents/PipelineOutput/NewMeasurementsGraph/"): # print(title) # continue tray, pot = title.split("_")[0], title.split("_")[1] tray_number = barcode_dict[tray] subprocess.call([ sys.executable, "/Users/gghosal/Documents/GitHub/PlantTrayMeasuringPipeline/ImageExtractor.py",
import Measurement as mm import NonlinearEnergy as ny import SpectralPower as sr import Feature as fe # Utils calculates the line length!!! import utils as ll import numpy as np import scipy as sp import scipy.signal as sp import random # Training from sklearn import svm MeasObjCh1 = mm.Measurement('Study_005_channel1.txt') MeasObjCh1.downsample(2) #MeasObjCh2 = mm.Measurement('Study_005_channel2.txt') #MeasObjCh2.downsample(2) #MeasObjCh3 = mm.Measurement('Study_005_channel3.txt') #MeasObjCh3.downsample(2) # Calculating all the relevant features FeatObj1 = fe.Feature(MeasObj1) thetaBandPowerFeature1 = sr.calculateFeatureValue(FeatObj1, 4, 8) alphaBandPowerFeature1 = sr.calculateFeatureValue(FeatObj1, 14, 32) betaBandPowerFeature1 = sr.calculateFeatureValue(FeatObj1, 8, 12) nonlinearEnergyFeature1 = ny.calculateFeatureValue(FeatObj1) lineLengthFeature1 = ll.calculateFeatureValue(FeatObj1, FeatObj1.StepSize.astype(int), FeatObj1.WindowLength.astype(int))
print 'starting testing...' import sys sys.path.append('../Bin') from Adrasteia import * sys.path.append('../Src/Statistics') import Measurement #Measurement.generateBackgroundProfile("DatabaseName",5,8) #reference test, without preprocessing Measurement.timeMultipleDirectMatch(1, 5, 24, 26, "/home/tails/FullDB.fasta", 1) # test with preprocessing Measurement.timeMultiplePreprocessAndMatch(1, 1, 24, 26, "/home/tails/FullDB.fasta", 1) Measurement.timeMultipleDirectMatch(1, 1, 24, 26, "/home/tails/FullDB.fasta", 1) #now run again, this time profiling the results of a filtered search #import profile #profile.run("db = FastaDatabase(\"DatabaseName\")") #profile.run("db.match(\"PIRLGLALNFSVFYYEILNSPERACHLAKRAFDEAIAELDSLNEDSYKDSTLIMQLL\",3)") #profile.run("direct_match(\"DatabaseName\",\"PIRLGLALNFSVFYYEILNSPER\",3)") #import hotshot #import hotshot.stats #prof = hotshot.Profile("hotshot.txt") #prof.runcall(FastaDatabase("DatabaseName")) #prof.runcall(db.match("PIRLGLALNFSVFYYEILNSPERACHLAKRAFDEAIAELDSLNEDSYKDSTLIMQLL",3)) #prof.close() #stats = hotshot.stats.load("hotshot.txt") #stats.strip_dirs() #stats.sort_stats('time', 'calls') #stats.print_stats(20)
from IPython import embed from Ingredient import * from Measurement import * from Recipe import * from RecipeIngredient import * from RecipesSerialization import * from Unit import * from PGDatabaseConnector import * from USDRParser import * from PGDatabase import * if __name__ == '__main__': databaseConnector = PGDatabaseConnector() ingredients = databaseConnector.fetchIngredientsFromSearch("cheese", 10) ris = [] for ingr in ingredients: if not ingr: continue ri = RecipeIngredient(None, ingr, Measurement('cup', 2.0)) ris.append(ri) recipe = Recipe(None, "Cheese soufle", 4, ris, "italian", "dinner") databaseConnector.createRecipe(recipe)
import sys sys.path.append("../../Bin") import Measurement Measurement.generateBackgroundProfile("../Testing/SampleDB.fasta", 500)
import ArduinoCommunicator import Measurement writer = Measurement.MeasurementWriter("measurements.csv") arduino = ArduinoCommunicator.ArduinoCommunicator() arduino.start() i = 0 while True: arduino.setSpeeds([i, i, i, i]) i += 2 data = arduino.readData(blocking=True) dist = data[0] loads = data[1:5] speeds = data[5:] writer.write(dist, speeds, loads) writer.flush() print("dist: %d mm. loads: " % dist, end="") print(loads, end="") print(" speeds: ", end="") print(speeds)
def ExecuteTask(self, MeasurementADTF=None): # decide operation for Message if self.__MessageID == 1: if self.__TaskID == 1: # Alive Request MessageIDAnswer = 2 MessageValueAnswer = byte_struct.pack(0) + '\x00' return self.__CreateAnswer(MessageIDAnswer, MessageValueAnswer) elif self.__TaskID == 2: # Configuration still running request Runlevel = self.__ADTF.GetRunlevel() MessageIDAnswer = 2 if Runlevel == 5: MessageValueAnswer = byte_struct.pack(0) + '\x00' return self.__CreateAnswer(MessageIDAnswer, MessageValueAnswer) else: MessageValueAnswer = byte_struct.pack( 1) + '\x00' + "Configuration is not running anymore" return self.__CreateAnswer(MessageIDAnswer, MessageValueAnswer) elif self.__TaskID == 3: # OnlineAnalysis possible self.__Parameter1 = self.__MessageValueSplit[1] MessageIDAnswer = 2 MessageValueAnswer = byte_struct.pack(0) + '\x00' return (self.__CreateAnswer(MessageIDAnswer, MessageValueAnswer), self.__Parameter1) elif self.__MessageID == 2: if self.__TaskID == 1: # Load Config self.__Parameter1 = self.__MessageValueSplit[1] self.__Parameter2 = self.__MessageValueSplit[2] self.__Result = self.__ADTF.LoadConfiguration( self.__Parameter1) # SetRecordingFolder does not return a value self.__ADTF.SetRecordingFolder(self.__Parameter2) # Add Result, if both were successful, Result should hold value 0 self.__Result = self.__Result + self.__ADTF.StartConfiguration( ) if self.__Result == 0: # successful, generate Message self.__ADTF.PrintLog("Configuration loaded") MessageIDAnswer = 2 MessageValueAnswer = byte_struct.pack(0) + '\x00' return (self.__CreateAnswer(MessageIDAnswer, MessageValueAnswer), self.__Parameter2) else: # execution failed, generate Message self.__ADTF.PrintLog("failed to load configuration") MessageIDAnswer = 2 MessageValueAnswer = byte_struct.pack( 1) + '\x00' + "Unable to open configuration" + '\x00' self.__ADTF.PrintLog(MessageValueAnswer) return (self.__CreateAnswer(MessageIDAnswer, MessageValueAnswer), None) elif self.__TaskID == 2: # Shutdown Config self.__ADTF.StopConfiguration() self.__Result = self.__ADTF.ShutDownConfiguration() if self.__Result == 0: # successful, generate Message self.__ADTF.PrintLog("Configuration unloaded") MessageIDAnswer = 2 MessageValueAnswer = byte_struct.pack(0) + '\x00' return self.__CreateAnswer(MessageIDAnswer, MessageValueAnswer) else: # execution failed, generate Message self.__ADTF.PrintLog("failed to unload configuration") MessageIDAnswer = 2 MessageValueAnswer = byte_struct.pack( 1) + '\x00' + "Unable to close configuration" + '\x00' return self.__CreateAnswer(MessageIDAnswer, MessageValueAnswer) elif self.__TaskID == 3: # Add Comment self.__ADTF.PrintLog("Adding Comment") self.__Parameter1 = self.__MessageValueSplit[1] MeasurementADTF.AddComment(self.__ADTF, None, self.__Parameter1) MessageIDAnswer = 2 MessageValueAnswer = byte_struct.pack(0) + '\x00' return self.__CreateAnswer(MessageIDAnswer, MessageValueAnswer) elif self.__TaskID == 4: # Start Recording self.__Parameter1 = self.__MessageValueSplit[1] self.__Parameter2 = self.__MessageValueSplit[2] self.__Parameter3 = self.__MessageValueSplit[3] self.__Result = self.__ADTF.StartRecording( 0, self.__Parameter1) if self.__Result == 0: # successful, generate Message self.__ADTF.PrintLog("Recording started") MessageIDAnswer = 2 MessageValueAnswer = byte_struct.pack(0) + '\x00' MeasurementADTF = Measurement.Measurement( self.__ADTF, self.__Parameter1, self.__Parameter2, self.__Parameter3) return (self.__CreateAnswer(MessageIDAnswer, MessageValueAnswer), MeasurementADTF) else: # execution failed, generate Message self.__ADTF.PrintLog("unable to start recording") MessageIDAnswer = 2 MessageValueAnswer = byte_struct.pack( 1) + '\x00' + "Unable to start recording" + '\x00' return (self.__CreateAnswer(MessageIDAnswer, MessageValueAnswer), MeasurementADTF) elif self.__TaskID == 5: # Stop Recording self.__Parameter1 = self.__MessageValueSplit[1] self.__Parameter2 = self.__MessageValueSplit[2] self.__Parameter3 = self.__MessageValueSplit[3] MeasurementADTF.AddComment(self.__ADTF, self.__Parameter2, self.__Parameter3) Filename, ShortDescription, LongDescription = MeasurementADTF.GetInformation( ) self.__Result = self.__ADTF.StopRecording( 0, Filename, ShortDescription, LongDescription) if self.__Result == 0: # successful, generate Message self.__ADTF.PrintLog("Recording stopped") MessageIDAnswer = 2 MessageValueAnswer = byte_struct.pack(0) + '\x00' return self.__CreateAnswer(MessageIDAnswer, MessageValueAnswer) else: # execution failed, generate Message self.__ADTF.PrintLog("unable to stop recording") MessageIDAnswer = 2 MessageValueAnswer = byte_struct.pack( 1) + '\x00' + "Unable to stop recording" + '\x00' return (self.__CreateAnswer(MessageIDAnswer, MessageValueAnswer), MeasurementADTF) elif self.__TaskID == 6: # Stop Adapter MessageIDAnswer = 2 MessageValueAnswer = byte_struct.pack(0) + '\x00' return self.__CreateAnswer(MessageIDAnswer, MessageValueAnswer) elif self.__MessageID == 3: # Onlineanalysis self.__Parameter1 = self.__MessageValueSplit[1] MessageIDAnswer = 2 MessageValueAnswer = byte_struct.pack(0) + '\x00' return (self.__CreateAnswer(MessageIDAnswer, MessageValueAnswer), self.__Parameter1)
def getSumYield(hist): result = Measurement() for i in range(hist.GetNbinsX()): result += Measurement(hist.GetBinContent(i+1), hist.GetBinError(i+1)) return result
def getYield(hist,ibin): return Measurement(hist.GetBinContent(ibin),hist.GetBinError(ibin))
import sys sys.path.append("../../Bin") import Measurement Measurement.generateBackgroundProfile("../Testing/SampleDB.fasta",500)
def __parse_data(self, data): # Temporal value. measurement = None # If data is somethind proceed. if (data is not ''): if (data is None): raise Exception('Invalid data.', data) # Split data by new line. splited_response = data.split('\r\n') # If splited array is longer then minimum chunk length proceed. if (len(splited_response) >= self.__min_chunk_len): # Get the second line. Remove new line and split by ',' (CSV). splited_csv = splited_response[self.__min_chunk_len - 1].replace('\r\n', '').split(',') if (len(splited_csv) != 3): raise Exception('Invalid package size.', len(splited_csv)) tmp_key_index = -1 # Find the matching unit. for key_index, key_unit in enumerate(self.__keys_unit): # Match the unit. if (self.__keys_unit[key_unit] in splited_csv[self.__keys_csv['value']]): # Remove units. Remove white spaces. value = splited_csv[self.__keys_csv['value']].replace( self.__keys_unit[key_unit], '').replace(' ', '') # Parse to float. value = "{0:.2f}".format(float(value)) # Set the temporal value. measurement = Measurement.Measurement(value, key_unit) # Break if is valid. break tmp_key_index = key_index if (tmp_key_index == len(self.__keys_unit) - 1): raise Exception('Invalid unit.', splited_response[self.__min_chunk_len - 1]) ## If first element is 'ST' this means stable. #if(splited_csv[self.__keys_csv['state']] == self.__keys_state['stable']): # pass #else: # raise Exception('Unstable value.', splited_response[self.__min_chunk_len - 1]) else: raise Exception('Invalid data length.', len(splited_response)) else: raise Exception('No data.', data) return measurement
def upcastMeasurement(self): # TODO: muss noch gecheckt werden das es auf der instanz erlaubt ist import Measurement mea = Measurement.Measurement_i(self) return mea._this()
import pickle import matplotlib.pyplot as plt import sklearn.linear_model as lm import argparse parser = argparse.ArgumentParser() parser.add_argument('--filename', '-f', type=str, default='trained') parser.add_argument('--Start', '-s', type=int, default=11) parser.add_argument('--End', '-e', type=int, default=15) parser.add_argument('--NUM_CONFIG', '-n', type=int, default=2) args = parser.parse_args() file = args.filename NUM_CONFIG = args.NUM_CONFIG # Uploading the testing data MeasObjCh1 = mm.Measurement('Study_005_channel1.pkg', args.Start, args.End) MeasObjCh1.downsample(2) # Calculating all the relevant features FeatObj1 = fe.Feature(MeasObjCh1) thetaBandPowerFeature1 = sr.calculateFeatureValue(FeatObj1, 4, 8) alphaBandPowerFeature1 = sr.calculateFeatureValue(FeatObj1, 14, 32) betaBandPowerFeature1 = sr.calculateFeatureValue(FeatObj1, 8, 12) nonlinearEnergyFeature1 = ny.calculateFeatureValue(FeatObj1) lineLengthFeature1 = ll.calculateFeatureValue( FeatObj1, FeatObj1.stepSize.astype(int), FeatObj1.windowLength.astype(int)) # Weed out all the bad values here thetaBandPowerFeature1IsNaN = np.where(np.isnan(thetaBandPowerFeature1)) alphaBandPowerFeature1IsNaN = np.where(np.isnan(alphaBandPowerFeature1)) betaBandPowerFeature1IsNaN = np.where(np.isnan(betaBandPowerFeature1))
ser = serial.Serial(port, 115200, timeout=3) if not ser.is_open: print("Could not open port") exit() deltaFreq = (fMax - fMin) / n measurementIndex = 0 command = '^SWEEP,' + str(fMin) + ',' + str(fMax) + ',' + str(n) + '$\n' ser.write(command.encode()) measurements = [] while True: line = ser.readline().decode() if line is None: break lineParts = line.split(',') map(lambda s: s.strip(' \n'), lineParts) x = Measurement.Measurement(fMin + measurementIndex * deltaFreq, lineParts[0], lineParts[1], lineParts[2], lineParts[3]) measurements.append(x) measurementIndex += 1 print("done") ser.close()
Distribution.Distribution(p1, cov1), Distribution.Distribution(p2, cov2) ] p3 = np.matrix('0.5; 0.5') cov3 = np.matrix('5 0; 0 5') init_feat_distr = [Distribution.Distribution(p3, cov3)] data.init(2, 1, init_veh_distr, init_feat_distr) var_pos1 = 2 var_pos2 = 2 var_init = [[var_pos1, var_pos1], [var_pos2, var_pos2]] while count < 1000: meas = me.Measurement(2, 1) v = algorithm.init_veh(2, 1, init_veh_distr, var_init) f = algorithm.init_feat(1, 2, init_feat_distr) algorithm.main_loop(v, f, meas, 7, 1, data) ++count x_pos_est0, y_pos_est0 = data.get_veh_belief(0) x_pos_pred0, y_pos_pred0 = data.get_pred_veh_belief(0) x_pos_est1, y_pos_est1 = data.get_veh_belief(1) x_pos_pred1, y_pos_pred1 = data.get_pred_veh_belief(1) error0 = [ Kalman.rmse(data.get_veh_belief(0)[0][1:], meas.mean_veh[0][0]), Kalman.rmse(data.get_veh_belief(0)[1][1:], meas.mean_veh[0][1]) ] errorP0 = [
def main(): parser = argparse.ArgumentParser() parser.add_argument('--Start', '-s', type=int, default=11) parser.add_argument('--End', '-e', type=int, default=15) parser.add_argument('--Plot', '-p', type=str, default="False") # Append additional data to results.pkg parser.add_argument('--Append', '-a', type=str, default="True") # Using C values instead parser.add_argument('--C', '-c', type=str, default="False") args = parser.parse_args() meas_obj = mm.Measurement("Study_005_channel1.pkg", args.Start, args.End) meas_obj.downsample(2) feat_obj = fe.Feature(meas_obj) feat_label_dict = g.getFeaturesAndLabel(meas_obj, feat_obj) feat_dict = dict( (k, feat_label_dict[k]) for k in feat_key if k in feat_label_dict) data_downsampled = feat_label_dict['data'] label_downsampled = feat_label_dict['label'] # Good practice to check that the correct keys are generated for their value if (g.checkDictForFeat(feat_dict) == False): assert (False) # MIGHT GET RID OF APPEND AND REPLACE IT WITH ANOTHER FUNCTION THAT SIMPLY DELETES EXTRANEOUS INFORMATION? list_of_files = os.listdir('.') trained_files = [] if (str2bool(args.Append) == True and str2bool(args.C) == False): start_gamma = input("Enter the first gamma: ") end_gamma = input("Enter the last gamma: ") inc = 1 for i in np.arange(start_gamma, end_gamma + inc, inc): pattern = "trained_SVM_rbf_gamma_" + str(i) + "_*" for entry in list_of_files: if fnmatch.fnmatch(entry, pattern) and entry.find('C') == -1: x = input(entry) trained_files.append(entry) break load_results = loadFile("results.pkg") results = load_results['results'] gamma_data = load_results['gamma_data'] for i in trained_files: load_svm = loadFile(i) if (load_svm['gamma'] not in gamma_data): temp_feat_dict = copy.deepcopy(feat_dict) result = validate(load_svm, temp_feat_dict) results.append(result) gamma_data.append(load_svm['gamma']) else: print("Skipping") save_data = { 'feat_obj': feat_obj, 'results': results, 'gamma_data': gamma_data } pickle.dump(save_data, open("results.pkg", 'wb')) elif (str2bool(args.Append) == False and str2bool(args.C) == False): pattern = "trained_SVM_rbf_gamma_*" for entry in list_of_files: if fnmatch.fnmatch(entry, pattern) and entry.find('C') == -1: trained_files.append(entry) # Going through all the trained files and analyzing its performance results = [] gamma_data = [] for i in trained_files: load_svm = loadFile(i) temp_feat_dict = copy.deepcopy(feat_dict) result = validate(load_svm, temp_feat_dict) results.append(result) gamma_data.append(load_svm['gamma']) #if (str2bool(args.Plot) == True): # plotResults(data_downsampled, result, label_downsampled, load_data['gamma'], load_data['method']) save_data = { 'feat_obj': feat_obj, 'results': results, 'gamma_data': gamma_data } pickle.dump(save_data, open("results.pkg", 'wb')) else: print("Calculating C") pattern = "trained_SVM_rbf_gamma_*" for entry in list_of_files: if fnmatch.fnmatch(entry, pattern) and entry.find('C') != -1: trained_files.append(entry) x = input(trained_files) results = [] C_data = [] gamma_data = [] for i in trained_files: load_svm = loadFile(i) print(load_svm['gamma']) temp_feat_dict = copy.deepcopy(feat_dict) result = validate(load_svm, temp_feat_dict) results.append(result) C_data.append(load_svm['C']) gamma_data.append(load_svm['gamma']) #if (str2bool(args.Plot) == True): # plotResults(data_downsampled, result, label_downsampled, load_data['gamma'], load_data['method']) save_data = { 'feat_obj': feat_obj, 'results': results, 'gamma_data': gamma_data, 'C_data': C_data } pickle.dump(save_data, open("results_C.pkg", 'wb'))
def start(self): self.state = ChargePlanState.STATE_INIT # main state machine while True: self.printToLogfile("State: " + str(self.state)) ################################################################################################## # STATE_INIT ################################################################################################## if self.state == ChargePlanState.STATE_INIT: try: # load configuration from JSON file with open('config.json') as configFile: self.config = json.load(configFile) # Initialize Wallbox charger = Wallbox.goEcharger( self.config["wallbox"]["IP"], self.config["wallbox"]["absolutMaxCurrent"]) # Uncomment this to use wallbox simulator for developing #charger = Wallbox.goEchargerSimulation(self.config["wallbox"]["IP"], self.config["wallbox"]["absolutMaxCurrent"]) # Initialize Measurement weatherSensorList = list() for measurement in self.config["measurements"]: if measurement["type"] == "Swissmeteo": weatherSensorList.append( Measurement.Swissmeteo(measurement["station"], measurement["modes"])) elif measurement["type"] == "Solarlog": weatherSensorList.append( Measurement.SolarLog(measurement["url"], measurement["username"], measurement["password"], measurement["modes"])) elif measurement["type"] == "Fronius": weatherSensorList.append( Measurement.Fronius(measurement["url"], measurement["deviceID"], measurement["modes"])) elif measurement["type"] == "Smartfox": weatherSensorList.append( Measurement.Smartfox(measurement["ip"], measurement["modes"])) else: self.printToLogfile( "Invalid weatherSensor definition") charger.allowCharging(False) self.allowCharging = False # internal state new_state = ChargePlanState.STATE_NO_CAR except IOError: # probably connection error to wallbox, try again self.printToLogfile("Wallbox IOError") time.sleep(self.config["timing"]["waitAfterErrorSeconds"]) new_state = ChargePlanState.STATE_INIT ################################################################################################## # STATE_NO_CAR ################################################################################################## elif self.state == ChargePlanState.STATE_NO_CAR: try: charger.readStatus() self.printToLogfile("Charger state: " + str(charger.state)) if charger.state == Wallbox.WallboxState.STATE_READY_NO_CAR: self.printToLogfile("Still no car connected, wait.") time.sleep( self.config["timing"]["waitWithoutCarSeconds"]) elif (charger.state == Wallbox.WallboxState.STATE_WAITING_FOR_CAR) or ( charger.state == Wallbox.WallboxState.STATE_CHARGING): self.printToLogfile("Car connected.") self._goal = None self.deadline = None self.maxEnergy = 0 self.limitToMaxEnergy = False new_state = ChargePlanState.STATE_CHARGING elif charger.state == Wallbox.WallboxState.STATE_FINISHED_CAR_STILL_CONNECTED: if self.allowCharging == False: self.printToLogfile( "Car connected but probably not really finished" ) new_state = ChargePlanState.STATE_CHARGING else: self.printToLogfile( "Car connected but already finished") new_state = ChargePlanState.STATE_FINISHED IOerror_count = 0 except IOError: # probably connection error to wallbox, try again IOerror_count = IOerror_count + 1 self.printToLogfile("Wallbox IOError") # if error count is too high, re-init everything if (IOerror_count > self.config["timing"]["connectionMaxRetrys"]): new_state = ChargePlanState.STATE_INIT else: time.sleep( self.config["timing"]["waitAfterErrorSeconds"]) new_state = ChargePlanState.STATE_NO_CAR ################################################################################################## # STATE_CHARGING ################################################################################################## elif self.state == ChargePlanState.STATE_CHARGING: try: charger.readStatus() self.printToLogfile("Wallbox state: " + str(charger.state)) self.power = charger.currentPower self.energy = charger.energy # check state of car and decide on consequences if charger.state == Wallbox.WallboxState.STATE_READY_NO_CAR: # car disconnected new_state = ChargePlanState.STATE_FINISHED elif charger.state == Wallbox.WallboxState.STATE_FINISHED_CAR_STILL_CONNECTED: if self.allowCharging == True: # Car says it's finished during charging, so battery is full new_state = ChargePlanState.STATE_FINISHED else: # Car says it's finished when charging is not allowed, so battery is NOT full. new_state = ChargePlanState.STATE_CHARGING else: new_state = ChargePlanState.STATE_CHARGING # take further actions if state should not be left if new_state == ChargePlanState.STATE_CHARGING: dateObjectNow = datetime.datetime.now() # Get maximum current from weather sensors. If multiple sensors are configured, # try all of them but stop as soon as one of them returns a valid value maxAllowedCurrent = None for weatherSensor in weatherSensorList: if maxAllowedCurrent == None: try: maxAllowedCurrent = weatherSensor.getMaxAllowedCurrent( self.power, self.mode) except IOError: # probably connection error to sensor self.printToLogfile( "WeatherSensor IOError: " + str(weatherSensor)) # Check returned value from weather sensors and react if maxAllowedCurrent == None: self.printToLogfile( "No weathersensor has returned a value.") maxAllowedCurrent = 0 time.sleep( self.config["timing"]["waitWithoutSunSeconds"]) new_state = ChargePlanState.STATE_CHARGING else: # Check if deadline is reached if self.deadline != None: if dateObjectNow > self.deadline: deadlineReached = True else: deadlineReached = False else: deadlineReached = False # Decide on charging depending on deadline and measurements if deadlineReached: # Deadline reached, charge charger.allowCharging(True) self.allowCharging = True # internal state self.printToLogfile( "Charge: deadline reached. Power: " + str(self.power)) charger.setMaxCurrent(self.config["wallbox"] ["absolutMaxCurrent"]) charger.setMaxEnergy(self.limitToMaxEnergy, self.maxEnergy) time.sleep(self.config["timing"] ["waitChargingSeconds"]) new_state = ChargePlanState.STATE_CHARGING else: if maxAllowedCurrent > 0: charger.allowCharging(True) self.allowCharging = True # internal state charger.setMaxCurrent(maxAllowedCurrent) charger.setMaxEnergy( self.limitToMaxEnergy, self.maxEnergy) self.printToLogfile( "Charge: getMaxAllowedCurrent: " + str(maxAllowedCurrent) + " power: " + str(self.power)) time.sleep(self.config["timing"] ["waitChargingSeconds"]) new_state = ChargePlanState.STATE_CHARGING else: charger.allowCharging(False) self.allowCharging = False # internal state self.printToLogfile( "No sun, don't charge, wait.") time.sleep(self.config["timing"] ["waitWithoutSunSeconds"]) new_state = ChargePlanState.STATE_CHARGING IOerror_count = 0 except IOError: # probably connection error to wallbox, try again IOerror_count = IOerror_count + 1 self.printToLogfile("Wallbox IOError") # if error count is too high, re-init everything if (IOerror_count > self.config["timing"]["connectionMaxRetrys"]): new_state = ChargePlanState.STATE_INIT else: time.sleep( self.config["timing"]["waitAfterErrorSeconds"]) new_state = ChargePlanState.STATE_CHARGING ################################################################################################## # STATE_FINISHED ################################################################################################## elif self.state == ChargePlanState.STATE_FINISHED: try: charger.readStatus() charger.setMaxCurrent( self.config["wallbox"]["absolutMaxCurrent"]) self.printToLogfile("Charger state: " + str(charger.state)) if charger.state == Wallbox.WallboxState.STATE_FINISHED_CAR_STILL_CONNECTED: self.printToLogfile( "Charging finished, car still connected") time.sleep( self.config["timing"]["waitAfterFinishedSeconds"]) elif charger.state == Wallbox.WallboxState.STATE_READY_NO_CAR: charger.allowCharging(False) self.allowCharging = False # internal state self.printToLogfile( "Charging finished, car disconnected") new_state = ChargePlanState.STATE_NO_CAR time.sleep( self.config["timing"]["waitWithoutCarSeconds"]) elif charger.state == Wallbox.WallboxState.STATE_WAITING_FOR_CAR or charger.state == Wallbox.WallboxState.STATE_CHARGING: self.printToLogfile( "Car starts charging again, probably pre-Heat") new_state = ChargePlanState.STATE_FINISHED time.sleep( self.config["timing"]["waitAfterFinishedSeconds"]) IOerror_count = 0 except IOError: # probably connection error to wallbox, try again IOerror_count = IOerror_count + 1 self.printToLogfile("Wallbox IOError") # if error count is too high, re-init everything if (IOerror_count > self.config["timing"]["connectionMaxRetrys"]): new_state = ChargePlanState.STATE_INIT else: time.sleep( self.config["timing"]["waitAfterErrorSeconds"]) new_state = ChargePlanState.STATE_FINISHED ################################################################################################## # STATE_ERROR ################################################################################################## elif self.state == ChargePlanState.STATE_ERROR: self.printToLogfile("Statemachine stuck in STATE_ERROR") time.sleep(self.config["timing"]["waitAfterErrorSeconds"]) ################################################################################################## # Undefined states ################################################################################################## else: self.printToLogfile("Error: Invalid state") time.sleep(self.config["timing"]["waitAfterFinishedSeconds"]) self.state = new_state
import Vehicle import numpy as np import Measurement as me import Pdfproduct as pdf import Pdf import math a = np.matrix('0;0') b = np.matrix('0 0;0 0') #veh1 = Vehicle.Vehicle() #[a, b] = veh1.predict(np.matrix('1; 1'), np.matrix('1 0; 0 1'), 1) meas = me.Measurement() meas.vehicle(1, 2) meas.feature(2, 50) m1 = np.matrix('1;1') m2 = np.matrix('1.2;1.2') m3 = np.matrix('1.2;1.2') v1 = np.matrix('100 0;0 100') v2 = np.matrix('100 0;0 100') v3 = np.matrix('0.001 .000;.000 0.001') X, Y, p = 2, 2, .2 a, b = pdf.product(m1, m2, v1, v2) print "Mean is :" print a print "Variance is :" print b Pdf.pdf(m3, v3)
error_square = [[None for i in range(runs)] for j in range(n_v)] error_distance = [[None for i in range(runs)] for j in range(n_v)] error = [None for i in range(n_v)] error_mean_distance = [None for i in range(n_v)] for n_mp in range(1, rangeNmp): data = [None for i in range(runs)] meas = [None for i in range(runs)] error_square = [[None for i in range(runs)] for j in range(n_v)] error_distance = [[None for i in range(runs)] for j in range(n_v)] error = [None for i in range(n_v)] error_mean_distance = [None for i in range(n_v)] for run in range(runs): meas[run] = me.Measurement(n_v, n_f, 1) data[run] = DataCollect.DataCollect(n_v, n_f) data[run].init(n_v, n_f, init_veh_distr, init_feat_distr[:n_f]) v = algorithm.init_veh(n_v, n_f, init_veh_distr, init_feat_distr[:n_f], var_init) print('Nmp = '+str(n_mp)) algorithm.main_loop(v, meas[run], n_mp, 1, data[run]) for k in range(n_v): error_square[k][run] = np.power(data[run].get_veh_belief(k)[:2, :] - meas[run].mean_veh[k], 2) error_distance[k][run] = np.sum(error_square[k][run], axis = 0) for l in range(n_v): error[l] = np.mean(error_square[l], axis = 0) error_mean_distance[l] = np.mean(error_distance[l], axis = 0) collectMSE[l][n_mp-1] = np.mean(error_mean_distance[l][7:], axis=0) print collectMSE
def createMeasurament(self): return Measurement(str(timegm(time.gmtime())), str(random.uniform(0, 10)))
np.random.multivariate_normal(np.array(p2).flatten(), cov2, 1), [4, 1]) init_veh_distr = [ Distribution.Distribution(init_veh_distr1mean, cov1), Distribution.Distribution(init_veh_distr2mean, cov2) ] init_feat_distr = [] for f in range(n_f): init_feat_distr_mean = np.reshape( np.random.multivariate_normal(np.array(p3).flatten(), cov3, 1), [2, 1]) init_feat_distr.append( Distribution.Distribution(init_feat_distr_mean, cov3)) meas[run] = me.Measurement(n_v, n_f, 1, False) dataICP[run] = DataCollect.DataCollect(n_v, n_f) dataICP[run].init(n_v, n_f, init_veh_distr, init_feat_distr[:n_f]) v = algorithm.init_veh(n_v, n_f, init_veh_distr, init_feat_distr[:n_f], var_init) algorithm.main_loop(v, meas[run], N_mp, 1, dataICP[run]) for k in range(n_v): error_square[k][run] = np.power( dataICP[run].get_veh_belief(k)[:2, :] - meas[run].mean_veh[k], 2) error_distance[k][run] = np.sum(error_square[k][run], axis=0) for l in range(n_v): error[l] = np.sqrt(np.mean(error_square[l], axis=0)) error_mean_norm[l] = np.sqrt(np.mean(error_distance[l], axis=0)) with open(
# This script is for testing the operation of the current source controlled by the Itsy-Bitsy M4 # R. Sheehan 23 - 10 - 2020 # import the necessary modules import board import time from analogio import AnalogOut from analogio import AnalogIn import supervisor # for listening to serial ports # Methods for making the measurements are defined in the file Measurement.py import Measurement #Measurement.Ser_Test() #Measurement.First_Script() Measurement.Cuffe_Iface() # This is for the Current Source Measurement #Measurement.AC_Read() # This is trying to read a sine wave #Measurement.AC_Max() # Find the max value being input to some channel #Measurement.Voltage_Divider_Test() #Measurement.Current_Source_Measurement() #Measurement.IO_Simple()
def main(): parser = argparse.ArgumentParser() parser.add_argument('--Normalize', '-n', type=str, default='MeanStd') parser.add_argument('--Kernel', '-k', type=str, default='rbf') parser.add_argument('--Start', '-s', type=int, default=1) parser.add_argument('--End', '-e', type=int, default=5) # This flag determines whether or not multiple trainings will be performed for the # cross validation parser.add_argument('--CrossValid', '-c', type=bool, default=False) parser.add_argument('--GammaMin', type=float, default=0.0) parser.add_argument('--GammaMax', type=float, default=10) parser.add_argument('--Increment', type=float, default=1.0) args = parser.parse_args() # Uploading data from a measurement text file meas_obj = mm.Measurement("Study_005_channel1.pkg", args.Start, args.End) meas_obj.downsample(2) # Calculating all the relevant features using information encompassing all the other # features feat_obj = fe.Feature(meas_obj) feat_label_dict = g.getFeaturesAndLabel(meas_obj, feat_obj) feat_dict = dict( (k, feat_label_dict[k]) for k in feat_key if k in feat_label_dict) label_downsampled = feat_label_dict['label'] # Good practice to check that the correct keys are generated for their value if (g.checkDictForFeat(feat_dict) == False): assert (False) # Storing the temporary means and standard deviations of the features before it is normalized # since a dictionary is mutable ''' tempMean = np.asarray([]) tempStd = np.asarray([]) for i in feat_key: tempMean = np.append(tempMean, np.mean(feat_dict[i])) tempStd = np.append(tempStd, np.std(feat_dict[i])) ''' tempMean = {} tempStd = {} for i in feat_key: tempMean[i] = np.mean(feat_dict[i]) tempStd[i] = np.std(feat_dict[i]) if (args.Normalize == "MeanStd"): g.normFeature(feat_dict, args.Normalize, tempMean, tempStd) elif (args.Normalize == "MinMax"): g.normFeature(feat_dict, args.Normalize) else: assert (False) feat_array = g.convertDictToFeatArray(feat_dict) if (args.CrossValid == True): for i in np.arange(args.GammaMin, args.GammaMax, args.Increment): print("Training test set using " + str(i) + " for gamma") c = train(feat_array, label_downsampled, args.Kernel, i) saveSVM(c, tempMean, tempStd, args.Normalize, args.Kernel, i) else: print("Using default Gamma value") c = train(feat_array, label_downsampled, args.Kernel) saveSVM(c, tempMean, tempStd, args.Normalize, args.Kernel)
p7 = np.matrix('0.5; 0.5') cov7 = np.matrix('5 0; 0 5') init_feat_distr = [ Distribution.Distribution( np.reshape( np.random.multivariate_normal(np.array(p3).flatten(), cov3, 1), [2, 1]), cov3) for i in range(n_f) ] q = 0.00001 # q = 1 var_init = [q, q] v = algorithm.init_veh(n_v, n_f, init_veh_distr, init_feat_distr, var_init) meas = me.Measurement(n_v, n_f, t_s, True) data = DataCollect.DataCollect(n_v, n_f) data.init(n_v, n_f, init_veh_distr, init_feat_distr[:n_f]) v = algorithm.init_veh(n_v, n_f, init_veh_distr, init_feat_distr[:n_f], var_init) # meas.meas_fea[0][1][:, 10:25] = None algorithm.main_loop(v, meas, 1, t_s, data) fontprop = FontProperties() fontprop.set_size('small') # # plt.figure(1, figsize=(9, 5)) # err1 = plt.subplot(211) # plt.plot(error[0][0, :], label='Square error in x')
print 'starting testing...' import sys sys.path.append('../Bin') from Adrasteia import * sys.path.append('../Src/Statistics') import Measurement #Measurement.generateBackgroundProfile("DatabaseName",5,8) #reference test, without preprocessing Measurement.timeMultipleDirectMatch(1,5,24,26,"/home/tails/FullDB.fasta",1) # test with preprocessing Measurement.timeMultiplePreprocessAndMatch(1,1,24,26,"/home/tails/FullDB.fasta",1) Measurement.timeMultipleDirectMatch(1,1,24,26,"/home/tails/FullDB.fasta",1) #now run again, this time profiling the results of a filtered search #import profile #profile.run("db = FastaDatabase(\"DatabaseName\")") #profile.run("db.match(\"PIRLGLALNFSVFYYEILNSPERACHLAKRAFDEAIAELDSLNEDSYKDSTLIMQLL\",3)") #profile.run("direct_match(\"DatabaseName\",\"PIRLGLALNFSVFYYEILNSPER\",3)") #import hotshot #import hotshot.stats #prof = hotshot.Profile("hotshot.txt") #prof.runcall(FastaDatabase("DatabaseName")) #prof.runcall(db.match("PIRLGLALNFSVFYYEILNSPERACHLAKRAFDEAIAELDSLNEDSYKDSTLIMQLL",3)) #prof.close() #stats = hotshot.stats.load("hotshot.txt") #stats.strip_dirs() #stats.sort_stats('time', 'calls') #stats.print_stats(20)
#Start listening on socket s.listen(1) print 'Socket now listening' #Default values config.set('CTRL','state','ready') #Set FFTS state ready with open(configfil, 'wb') as configfile: config.write(configfile) fftSize = int(config.get('USRP','channels')) samp_rate = float(config.get('USRP','bw'))*1e6 interval = 5 #Default interval for switched measurements gain = float(config.get('USRP','gain')) c_freq = float(config.get('USRP','cfreq'))*1e6 window = config.get('USRP', 'fft_window') #Initialize USRP device tb = Measurement(fftSize, samp_rate, interval, gain, c_freq, config, user, window) class Worker(Thread): def __init__(self): Thread.__init__(self) def run(self): #Start measurement tb.measure_start() #Reinit thread so it can be called several times (TODO: Find a better way) Thread.__init__(self) thread = Worker() thread.daemon = False def clientthread(conn): while True:
if AnalysisFinished == True: #MonitoringThread.exit() StopMonitoringThread = True MonitoringThread = None OnlineAnalyser.ResetAnalyser(ADTF) # send answer for Analysis if (AnalysisAnswer != None): CommunicationInterface.Send(ADTF, AnalysisAnswer) AnalysisAnswer = None else: # TDA is not alive anymore ADTF.PrintLog("Stop modules") # if Measurement is running stop it and save collected data if (Measurement != None): Measurement.AddComment( ADTF, "", "Measurement stopped automatically because connection to TDA was lost" ) Filename, ShortDescription, LongDescription = Measurement.GetInformation( ) ADTF.StopRecording(0, Filename, ShortDescription, LongDescription) del Measurement if (OnlineAnalyser != None): del OnlineAnalyser StopAdapter = True ### last send before ADTF gets stopped ##if not (MessageAnswer == None): ## CommunicationInterface.Send(ADTF,MessageAnswer) # wait until last SendMonitoringData is executed ADTF.PrintLog("StopAdapter = " + str(StopAdapter)) ADTF.PrintLog("Wait until last send is done")