def update(self, dt): super().update(dt) if self.alive: self.barrel.baseX, self.barrel.baseY = functions.convert( (self.body.position[0], self.body.position[1])) self.barrel.endX = self.barrel.baseX - (92 / 2) * math.cos( self.barrel.currAngle) self.barrel.endY = self.barrel.baseY + (92 / 2) * math.sin( self.barrel.currAngle) characterPos = functions.convert(self.player.body.position) self.barrel.delta_x = characterPos[0] - self.barrel.baseX self.barrel.delta_y = characterPos[1] - self.barrel.baseY self.barrel.currAngle = math.pi - ( math.atan2(self.barrel.delta_y, self.barrel.delta_x) + (20 * (math.pi / 180))) if self.barrel.cooldown <= 0: self.barrel.createProjectile(0) self.barrel.cooldown = random.uniform(2, 2.75) dir_path = os.path.dirname(os.path.realpath(__file__)) gunSound = pygame.mixer.Sound( os.path.join(dir_path, "sounds/sound_effects/Gun8.wav")) gunSound.set_volume(0.15) pygame.mixer.Channel(2).play(gunSound) self.barrel.cooldown -= dt else: self.remove()
def main(): path = sys.argv[1] with open(path) as f: config = json.load(f) # unzip the zip file zip_ref = zipfile.ZipFile(config['zip'], 'r') zip_ref.extractall(config['src']) zip_ref.close() if os.path.exists(config['src']): functions.convert(config['src'], config['des'])
def __init__(self, screen, space, entities, pos, speed, parentVelocity, angle, radius, mass, friendly=False): self.mass = mass self.radius = radius super().__init__(screen, space, entities) self.body = pymunk.Body(self.mass, pymunk.moment_for_circle(self.mass, 0, radius)) self.body.entity_ref = self self.body.position = functions.convert(pos) self.body.velocity = (speed * math.cos(angle), parentVelocity[1] - speed * math.sin(angle)) self.shape = pymunk.Circle(self.body, radius) if friendly: self.shape.collision_type = 1 self.image = playerBulletImg else: self.shape.collision_type = 2 self.image = pygame.transform.scale(enemyBulletImg, (radius * 2, radius * 2)) self.space = space self.space.add(self.body, self.shape)
def update(self, dt): #if pygame.key.get_pressed()[pygame.K_s]: # if self.currAngle < self.maxAngle: # self.currAngle += math.pi / 48 #if pygame.key.get_pressed()[pygame.K_w]: # if self.currAngle > self.minAngle: # self.currAngle -= math.pi / 48 self.centreX, self.centreY = functions.convert( (self.parent.body.position[0] - 4, self.parent.body.position[1] + 24)) mouseX, mouseY = pygame.mouse.get_pos() try: angle = math.atan( (mouseY - self.centreY) / (mouseX - self.centreX)) if angle >= self.minAngle and angle <= self.maxAngle: self.currAngle = angle elif angle < self.minAngle: self.currAngle = self.minAngle else: self.currAngle = self.maxAngle except: angle = math.pi / 2 if pygame.mouse.get_pressed()[0] and self.cooldown <= 0: self.createProjectile() self.cooldown = 0.25 self.cooldown -= dt self.endX = self.centreX + self.length * math.cos(self.currAngle + 0.40489) self.endY = self.centreY + self.length * math.sin(self.currAngle + 0.40489)
def read_score(file,gene_convert): score_pair=dict() list_pair=[] f=open(file,'r') for line in f: line=line.rstrip().split("\t") gene1=functions.convert(line[0],gene_convert) gene2=functions.convert(line[1],gene_convert) pair=gene1+":"+gene2 score=float(line[2]) if gene1==gene2: continue score_pair[pair]=score list_pair.append(pair) f.close() return score_pair,list_pair
def read_score(file, gene_convert): score_pair = dict() list_pair = [] f = open(file, 'r') for line in f: line = line.rstrip().split("\t") gene1 = functions.convert(line[0], gene_convert) gene2 = functions.convert(line[1], gene_convert) pair = gene1 + ":" + gene2 score = float(line[2]) if gene1 == gene2: continue score_pair[pair] = score list_pair.append(pair) f.close() return score_pair, list_pair
def formHistogram(outputFile, listOfDirs): global maximum, minimum, numBins, volts, eSqH, binSize, listofBins, shifts fs = None listOfBins = functions.createZeroedList(numBins) print("Number of files opened: " + str(len(listOfDirs))) print("Min = " + str(minimum) + " Max = " + str(maximum)) binSize = (maximum - minimum) / (numBins - 1) print("BinSize = " + str(binSize)) #go through file sorting data for i in range(0, len(listOfDirs), 1): success = True try: fs = open(listOfDirs[i], "r") #print("Successfully opened "+listOfDirs[i]) except: print("FAILED to open " + listOfDirs[i]) success = False if success == True: #read file for line in fs: cell = line.split(",") cell[4] = functions.convert(float(cell[4]), volts, eSqH) - shifts[i] if cell[4] < 14 and cell[4] > 0: listOfBins[int(float(cell[4] - minimum) / (binSize))] += 1 fs.close() print("Finished.") #Estimate peaks baseAvg = functions.findBaselineAvg(listOfBins, numBins) peakData = functions.findpeaks(listOfBins, numBins, binSize, baseAvg) print("Calculating peaks...") peakXVal = [] for peak in peakData: peakXVal.append(peak * binSize) print("Peaks found at ") print(peakXVal) #write results to a file fs = open(outputFile, "w") j = 0 halfBin = binSize / 2 for i in range(0, numBins, 1): try: if peakData[j] == i: fs.write((str((i * binSize) + minimum + halfBin)) + "," + str(listOfBins[i]) + ",10000," + str(baseAvg[i]) + "\n") j += 1 else: fs.write((str((i * binSize) + minimum + halfBin)) + "," + str(listOfBins[i]) + ",0," + str(baseAvg[i]) + "\n") except: fs.write((str((i * binSize) + minimum + halfBin)) + "," + str(listOfBins[i]) + ",0," + str(baseAvg[i]) + "\n") print("Output File created.") fs.close()
def read_score(file,gene_convert,sem_sim): score_pair=dict() f=open(file,'r') for line in f: line=line.rstrip().split("\t") gene1=functions.convert(line[0],gene_convert) gene2=functions.convert(line[1],gene_convert) pair=gene1+":"+gene2 score=float(line[2]) if gene1==gene2: continue if sem_sim.has_key(gene1) and sem_sim[gene1].has_key(gene2): score_pair[pair]=score if sem_sim.has_key(gene2) and sem_sim[gene2].has_key(gene1): score_pair[pair]=score f.close() return score_pair
def read_score(file, gene_convert, sem_sim): score_pair = dict() f = open(file, 'r') for line in f: line = line.rstrip().split("\t") gene1 = functions.convert(line[0], gene_convert) gene2 = functions.convert(line[1], gene_convert) pair = gene1 + ":" + gene2 score = float(line[2]) if gene1 == gene2: continue if sem_sim.has_key(gene1) and sem_sim[gene1].has_key(gene2): score_pair[pair] = score if sem_sim.has_key(gene2) and sem_sim[gene2].has_key(gene1): score_pair[pair] = score f.close() return score_pair
def main(): create_database(['104', '732']) request = YandexApiRequest(Request.GET_STOP_INFO, '732') # request = YandexApiRequest(Request.GET_LINE, '732') # TODO doesnt work request.run() print(convert(request.obtained_data))
def formHistogram(outputFile, listOfDirs): global maximum, minimum, numBins, volts, eSqH, binSize, listofBins, shifts fs = None listOfBins = functions.createZeroedList(numBins) print("Number of files opened: "+str(len(listOfDirs))) print("Min = "+str(minimum)+" Max = "+str(maximum)) binSize = (maximum-minimum)/(numBins-1) print("BinSize = "+str(binSize)) #go through file sorting data for i in range(0, len(listOfDirs), 1): success = True try: fs = open(listOfDirs[i], "r") #print("Successfully opened "+listOfDirs[i]) except: print("FAILED to open "+listOfDirs[i]) success = False if success == True: #read file for line in fs: cell = line.split(",") cell[4] = functions.convert(float(cell[4]), volts, eSqH) - shifts[i] if cell[4] < 14 and cell[4] > 0: listOfBins[int(float(cell[4]-minimum)/(binSize))] += 1 fs.close() print("Finished.") #Estimate peaks baseAvg = functions.findBaselineAvg(listOfBins, numBins) peakData = functions.findpeaks(listOfBins, numBins, binSize, baseAvg) print("Calculating peaks...") peakXVal = [] for peak in peakData: peakXVal.append(peak*binSize) print("Peaks found at ") print(peakXVal) #write results to a file fs = open(outputFile, "w") j = 0 halfBin = binSize/2 for i in range(0, numBins, 1): try: if peakData[j] == i: fs.write((str((i*binSize)+minimum+halfBin))+","+str(listOfBins[i])+",10000,"+str(baseAvg[i])+"\n") j+=1 else: fs.write((str((i*binSize)+minimum+halfBin))+","+str(listOfBins[i])+",0,"+str(baseAvg[i])+"\n") except: fs.write((str((i*binSize)+minimum+halfBin))+","+str(listOfBins[i])+",0,"+str(baseAvg[i])+"\n") print("Output File created.") fs.close()
def printList(self): initial = 0 for list in self.attrName: self.attributes[initial] = func.convert(self.attributes[initial], "float") vartype = type(self.attributes[initial]).__name__ print("{:15}{}".format(list, self.attributes[initial] ) if vartype == "str" else "{:15}{:.2f}". format(list, self.attributes[initial])) initial += 1
def draw(self): x, y = functions.convert(self.body.position) if self.thrusting: self.imageIndex += 0.5 self.screen.blit(self.sheetThrust[int(self.imageIndex % 4)], (x - 100, y - 120)) functions.rotate(self.screen, self.armImg, (x - 4, y - 24), (96, 96), -math.degrees(self.target.currAngle)) else: self.imageIndex += 0.25 self.screen.blit(self.sheetIdle[int(self.imageIndex % 2)], (x - 100, y - 120)) functions.rotate(self.screen, self.armImg, (x - 4, y - 24), (96, 96), -math.degrees(self.target.currAngle)) self.screen.blit(self.shldrImg, (x - 100, y - 120))
def findMinAndMax( ): # function to find the minimum and maximum across all of the data, and find how much each file needs to be shifted to reach 0 global minimum, maximum, shifts fs = None print("Reading in data...") # read through files and find minimum/maximum data for i, directory in enumerate(listOfDirs): data = [] success = True try: fs = open(directory, "r") #print("Successfully opened "+directory) except: print("FAILED to open " + directory) success = False if success == True: #read file for line in fs: cell = line.split(",") cell[4] = functions.convert(float(cell[4]), volts, eSqH) if cell[4] < 0.3: data.append(cell[4]) if float(cell[4]) > maximum: maximum = float(cell[4]) elif float(cell[4]) < minimum: minimum = float(cell[4]) if len(data) > 20: avgData = 0 for item in data: avgData += item avgData = avgData / len(data) shifts.append(avgData) else: shifts.append(-10000) avgShift = 0 count = 0 for item in shifts: if item != -10000: avgShift += item count += 1 avgShift = avgShift / count for i, item in enumerate(shifts): if item == -10000: shifts[i] = avgShift
def findMinAndMax(): # function to find the minimum and maximum across all of the data, and find how much each file needs to be shifted to reach 0 global minimum, maximum, shifts fs = None print("Reading in data...") # read through files and find minimum/maximum data for i, directory in enumerate(listOfDirs): data = [] success = True try: fs = open(directory, "r") #print("Successfully opened "+directory) except: print("FAILED to open "+directory) success = False if success == True: #read file for line in fs: cell = line.split(",") cell[4] = functions.convert(float(cell[4]), volts, eSqH) if cell[4] < 0.3: data.append(cell[4]) if float(cell[4]) > maximum: maximum = float(cell[4]) elif float(cell[4]) < minimum: minimum = float(cell[4]) if len(data) > 20: avgData = 0 for item in data: avgData += item avgData = avgData/len(data) shifts.append(avgData) else: shifts.append(-10000) avgShift = 0 count = 0 for item in shifts: if item != -10000: avgShift += item count += 1 avgShift = avgShift/count for i, item in enumerate(shifts): if item == -10000: shifts[i] = avgShift
def throws(self): # total sum of 3 throws is going to deducted score = 0 # total sum of 3 throws (temporary) pre_score = 0 print(f'Ход {self.name}:') if checkout(self.score_remain - pre_score): print( f'Остаток: {self.score_remain} = {checkout(self.score_remain)} \n' ) else: print(f'Остаток: {self.score_remain}\n') for attempt in range(3): # receives a list of [k, v] where k - multiply and v - sector throw = convert(input(f'Дротик #{attempt + 1}: ')) # throw score throw_score = throw[0] * throw[1] pre_score += throw_score if not self.is_end_of_turn(pre_score): if checkout(self.score_remain - pre_score): print( f'Остаток: {self.score_remain - pre_score} = {checkout(self.score_remain - pre_score)}\n' ) else: print(f'Остаток: {self.score_remain - pre_score}\n') score = pre_score if attempt == 2 else 0 else: if self.is_checkout(throw, (self.score_remain - pre_score)): print('Победа!') score = pre_score self.score_remain -= score self.end = True break print(f'Перебор!\nОстаток: {self.score_remain}\n') break self.score_remain -= score if not self.end: print(f'Игрок {self.name} выбил {score}\n\n')
def handle_source(json_data): text = fs.convert((json_data['message'].encode())) socketio.emit('echo', {'echo': str(text)})
count += 1 avgShift = avgShift / count for i, item in enumerate(shifts): if item == -10000: shifts[i] = avgShift findMinAndMax() print("Reading in Data...") data = [] for i, directory in enumerate(listOfDirs): fs = open(directory, "r") for line in fs: column = line.split(",") data.append( functions.convert(float(column[4]), volts, eSqH) - shifts[i]) # <-----TOM make this one point to the correct column fs.close() binSize = (maximum - minimum) / (numBins - 1) print("Sorting data...") for item in data: if item < 14 and item > 0: listOfBins[int(float(item - minimum) / binSize)] += 1 print("Writing data...") fs = open("somefile.csv", "w") halfBin = binSize / 2 for i, item in enumerate(listOfBins): fs.write(str((i * binSize) + halfBin + minimum) + "," + str(item) + "\n") print("Finished.") fs.close()
def formHistogram(outputFile, listOfDirs): global maximum, minimum, numBins, volts, eSqH, binSize, listofBins, listOfMinimums global prevcell, listdiff, listdata,value, runningavg, base, markers,k, total, count2, l, kref fs = None listOfBins = functions.createZeroedList(numBins) listOfMinimums = functions.createZeroedList(len(listOfDirs)) print("Reading in data...") # read through files and fine minimum/maximum data count = 0 for directory in listOfDirs: success = True try: fs = open(directory, "r") #print("Successfully opened "+directory) except: print("FAILED to open "+directory) success = False if success == True: #read file minTemp = 0 maxTemp = 0 for line in fs: cell = line.split(",") cell[4] = functions.convert(float(cell[4]), volts, eSqH) if float(cell[4]) > maximum: maximum = float(cell[4]) elif float(cell[4]) < minimum: minimum = float(cell[4]) #if float(cell[4]) > maxTemp: # maxTemp = float(cell[4]) #elif float(cell[4]) < minTemp: # minTemp = float(cell[4]) #listOfMinimums[count] = abs(minTemp) count = count + 1 print("Number of files opened: "+str(len(listOfDirs))) #print("Min = "+str(minimum)+" Max = "+str(maximum)) binSize = (maximum-minimum)/(numBins-1) print("BinSize = "+str(binSize)) #go through file sorting data for i in range(0, len(listOfDirs), 1): success = True try: fs = open(listOfDirs[i], "r") #print("Successfully opened "+listOfDirs[i]) except: print("FAILED to open "+listOfDirs[i]) success = False if success == True: #read file listdiff = [] runningavg = [] markers = [] listData = [] for x,line in enumerate(fs): listData.append(cell[4]) cell = line.split(",") cell[4] = functions.convert(float(cell[4]), volts, eSqH) #cell[4] = float(cell[4]) + float(listOfMinimums[i]) #listOfBins[int(float(cell[4]+abs(minimum))/(binSize))] += 1 if x >= stepsize: listdiff.append((cell[4] - listData[x-stepsize]) / ( stepsize*4*10e-7 ) ) #fopen=open("c:\\Users\\Thomas\\Desktop\\QCNLab\\qcnlab\\New folder\\outputFile_" + str(i)+ ".csv","w") count=len(listdiff) for x in range (3,count-2,1): #fopen.write(str((listdiff[x-2]+listdiff[x-1]+listdiff[x]+listdiff[x+1]+listdiff[x+2])/5) + "\n") runningavg.append((listdiff[x-2]+listdiff[x-1]+listdiff[x]+listdiff[x+1]+listdiff[x+2])/5) #functions.writeList(listdiff,fopen) avg = sum(runningavg)/count base = ((avg)+min(runningavg))/2 for x in range (1,count-5,1): if runningavg[x]<base: markers.append(x) count=len(markers) k=markers[0]+1 for x in range (0,count,1): if k!=markers[x]-1: count2 = 0 total = 0 #print("HI") for l in range (markers[x-1]+1,markers[x],1): count2 = count2 + 1 total = listData[l]+total #print("HELLO") if count2>0: #print("HOWDY") value.append(total/count2) k=markers[x] #fopen.close() fs.close() minimum = 0 maximum = max(value) print(maximum) binSize = (maximum)/(numBins-1) count=len(value) print(count) print(binSize) for i in range (0, count, 1): if value[i]>=0: listOfBins[int(value[i]/binSize)] += 1 #print(int(value[i]/binsize)) else: print("ERROR") #fopen=open("c:\\Users\\Thomas\\Desktop\\QCNLab\\qcnlab\\New folder\\outputFile.csv","w") fopen = open(outputFile, "w") for i in range (0,numBins,1): fopen.write(str((i*binSize)+(binSize/2))+","+str(listOfBins[i])+"\n") fopen.close() print("Finished.") ''' #Estimate peaks baseAvg = functions.findBaselineAvg(listOfBins, numBins) peakData = functions.findpeaks(listOfBins, numBins, binSize, baseAvg) print("Calculating peaks...") peakXVal = [] for peak in peakData: peakXVal.append(peak*binSize) print("Peaks found at ") print(peakXVal) ''' '''
def formHistogram(outputFile, listOfDirs): global maximum, minimum, numBins, volts, eSqH, binSize, listofBins, condData, timeData, stripSize, numSteps, steps, shifts, numEmpty listOfBins = functions.createZeroedList(numBins) print("Number of files opened: " + str(len(listOfDirs))) print("Min = " + str(minimum) + " Max = " + str(maximum)) binSize = (maximum - minimum) / (numBins - 1) print("BinSize = " + str(binSize)) print("Finding steps...") #go through file sorting data for i in range(0, len(listOfDirs), 1): success = True try: fs = open(listOfDirs[i], "r") #print("Successfully opened "+directory) except: print("FAILED to open " + listOfDirs[i]) success = False if success == True: #read file Empty = True for line in fs: cell = line.split(",") condData.append( functions.convert(float(cell[4]), volts, eSqH) - shifts[i]) timeData.append(float(cell[3])) marker = 0 while marker < stripSize: steps = [] while marker < (2500 - stripSize): stripDataX = [] stripDataY = [] for i in range(marker, marker + stripSize, 1): stripDataX.append(timeData[i]) stripDataY.append(condData[i]) b = functions.regressionFindB(stripDataX, stripDataY) sumY = 0 if abs(b) < 10000: # Fudge Factor numSteps += 1 for item in stripDataY: sumY += item sumY = sumY / stripSize steps.append(sumY) if len(steps) > 0: steps = functions.removeDuplicates(steps) steps = functions.removeMinMaxValues( steps, max(condData)) Empty = False marker += stripSize for item in steps: listOfBins[int(float(item + abs(minimum)) / (binSize))] += 1 marker += 1 if Empty == True: numEmpty += 1 condData = [] timeData = [] fs.close() print("Number of files where steps could not be detected: " + str(numEmpty)) #Estimate peaks baseAvg = functions.findBaselineAvg(listOfBins, numBins) peakData = functions.findpeaks(listOfBins, numBins, binSize, baseAvg) print("Calculating peaks...") peakXVal = [] for peak in peakData: peakXVal.append(peak * binSize) print("Peaks found at ") print(peakXVal) #write results to a file fs = open(outputFile, "w") j = 0 halfBin = binSize / 2 for i in range(0, numBins, 1): try: if peakData[j] == i: fs.write((str((i * binSize) - abs(minimum) + halfBin)) + "," + str(listOfBins[i]) + ",20," + str(baseAvg[i]) + "\n") j += 1 else: fs.write((str((i * binSize) - abs(minimum) + halfBin)) + "," + str(listOfBins[i]) + ",0," + str(baseAvg[i]) + "\n") except: fs.write((str((i * binSize) - abs(minimum) + halfBin)) + "," + str(listOfBins[i]) + ",0," + str(baseAvg[i]) + "\n") print("Output File created.") fs.close()
def formHistogram(outputFile, listOfDirs): global maximum, minimum, numBins, volts, eSqH, binSize, listofBins, condData, steps, timeData, shifts, numEmpty fs = None listOfBins = functions.createZeroedList(numBins) print("Number of files opened: " + str(len(listOfDirs))) print("Min = " + str(minimum) + " Max = " + str(maximum)) binSize = (maximum - minimum) / (numBins - 1) print("BinSize = " + str(binSize)) #go through file sorting data for i in range(0, len(listOfDirs), 1): success = True try: fs = open(listOfDirs[i], "r") #print("Successfully opened "+directory) except: print("FAILED to open " + listOfDirs[i]) success = False if success == True: #read file for line in fs: cell = line.split(",") condData.append(functions.convert(float(cell[4]), volts, eSqH)) timeData.append(float(cell[3])) recursiveStepSearch(0, len(condData) - 1, condData, timeData) steps = functions.removeDuplicates(steps) steps = functions.removeMinMaxValues(steps, max(condData)) if len(steps) == 0: numEmpty += 1 for item in steps: listOfBins[int(float(item + abs(minimum)) / (binSize))] += 1 steps = [] condData = [] timeData = [] fs.close() print("Number of files where steps could not be detected: " + str(numEmpty)) #Estimate peaks baseAvg = functions.findBaselineAvg(listOfBins, numBins) peakData = functions.findpeaks(listOfBins, numBins, binSize, baseAvg) print("Calculating peaks...") peakXVal = [] for peak in peakData: peakXVal.append(peak * binSize) print("Peaks found at ") print(peakXVal) #write results to a file fs = open(outputFile, "w") j = 0 halfBin = binSize / 2 for i in range(0, numBins, 1): try: if peakData[j] == i: fs.write((str((i * binSize) - abs(minimum) + halfBin)) + "," + str(listOfBins[i]) + ",20," + str(baseAvg[i]) + "\n") j += 1 else: fs.write((str((i * binSize) - abs(minimum) + halfBin)) + "," + str(listOfBins[i]) + ",0," + str(baseAvg[i]) + "\n") except: fs.write((str((i * binSize) - abs(minimum) + halfBin)) + "," + str(listOfBins[i]) + ",0," + str(baseAvg[i]) + "\n") print("Output File created.") fs.close()
def formHistogram(outputFile, listOfDirs): valueList = [] stepListVoltageValues = [] fs = None z = 3 something = int(z / 3) j = 0 Top = 0 Bottom = 0 global numBins, volts, eSqH, binSize, listOfBins listOfBins = functions.createZeroedList(numBins) for count, directory in enumerate(listOfDirs): success = True try: fs = open(directory, "r") #print("Successfully opened "+directory) except: print("FAILED to open " + directory) success = False if success == True: valueList = [] for line in fs: cell = line.split(",") print(cell[4] + " " + str(float(cell[4]))) cell[4] = functions.convert(float(cell[4]), volts, eSqH) - shifts[count] if cell[4] > 0.3 and cell[4] < 14: valueList.append(float(cell[4])) for i in range(z, len(valueList), 1): #Start loop for at top value search Top = None if ((valueList[i] - valueList[i - something]) > ((valueList[i - something] - valueList[i - 2 * something]))): Bottom = None Top = valueList[i - something] j = i - something #store index for last value assessed for z in range( j, len(valueList), 1 ): #now carry on only looking for a much smaller increment size to give bottom of step if (((valueList[z] - valueList[z - something]) <= ((valueList[z - something] - valueList[z - 2 * something])) * (1 - 0))): Bottom = valueList[z] #store last index assessed break #break out of loop so we can calc the step try: if (((Top - Bottom) >= 0.2 * eSqH)): #filtering out the accidental mini steps stepListVoltageValues.append(Top) else: #print("false step between " + str(j) +"-" + str(i)) #explaining where we miscalc a step to then assess for debug print("nboithing") except: print("Nout") #minimum = min(stepListVoltageValues) #analyzing for the max and min range of step sizes, #maximum = max(stepListVoltageValues) binSize = (maximum - minimum) / ( numBins - 1 ) #To be done after data set has been full analyzed, so once while loop is ended print("min = " + str(minimum) + " max = " + str(maximum) + " BinSize = " + str(binSize)) print(functions.printList(stepListVoltageValues)) for i in range(0, len(stepListVoltageValues), 1): listOfBins[int( (stepListVoltageValues[i] - minimum) / binSize)] += 1 #print(int((stepListVoltageValues[i]-minimum)/binSize)) fs = open(outputFile, "w") #make the histo halfBin = binSize / 2 for i, item in enumerate(listOfBins): fs.write((str((i * binSize) + halfBin)) + "," + str(item) + "\n") print("Output File created.") fs.close()
def formHistogram(outputFile, listOfDirs): valueList = [] stepListVoltageValues = [] fs = None z=3 something = int(z/3) j=0 Top=0 Bottom=0 global numBins, volts, eSqH, binSize, listOfBins listOfBins = functions.createZeroedList(numBins) for count, directory in enumerate(listOfDirs): success = True try: fs = open(directory, "r") #print("Successfully opened "+directory) except: print("FAILED to open "+directory) success = False if success == True: valueList = [] for line in fs: cell = line.split(",") print(cell[4]+" "+str(float(cell[4]))) cell[4] = functions.convert(float(cell[4]), volts, eSqH) - shifts[count] if cell[4] > 0.3 and cell[4] < 14: valueList.append(float(cell[4])) for i in range (z, len(valueList), 1):#Start loop for at top value search Top=None if((valueList[i]-valueList[i-something])>((valueList[i-something]-valueList[i-2*something]))): Bottom = None Top = valueList[i-something] j=i-something #store index for last value assessed for z in range (j, len(valueList), 1): #now carry on only looking for a much smaller increment size to give bottom of step if (((valueList[z]-valueList[z-something])<=((valueList[z-something]-valueList[z-2*something]))*(1-0))): Bottom = valueList[z] #store last index assessed break #break out of loop so we can calc the step try: if(((Top-Bottom) >= 0.2*eSqH)): #filtering out the accidental mini steps stepListVoltageValues.append(Top) else: #print("false step between " + str(j) +"-" + str(i)) #explaining where we miscalc a step to then assess for debug print("nboithing") except: print("Nout") #minimum = min(stepListVoltageValues) #analyzing for the max and min range of step sizes, #maximum = max(stepListVoltageValues) binSize = (maximum - minimum)/(numBins-1) #To be done after data set has been full analyzed, so once while loop is ended print("min = "+str(minimum)+" max = "+str(maximum)+" BinSize = "+str(binSize)) print(functions.printList(stepListVoltageValues)) for i in range (0, len(stepListVoltageValues), 1): listOfBins[int((stepListVoltageValues[i]-minimum)/binSize)] +=1 #print(int((stepListVoltageValues[i]-minimum)/binSize)) fs = open(outputFile, "w") #make the histo halfBin = binSize/2 for i, item in enumerate(listOfBins): fs.write((str((i*binSize)+halfBin))+","+str(item)+"\n") print("Output File created.") fs.close()
def draw(self): functions.rotate(self.screen, self.texture, functions.convert(self.body.position), (9, self.height//2), math.degrees(self.body.angle))
def main(): functions.clear() if not functions.exists(): try: functions.intro() except KeyboardInterrupt: functions.clear() import sys sys.exit(0) functions.clear() try: password = getpass.getpass("Password: "******"Invalid password") import sys sys.exit(1) while True: functions.clear() print("Vault") print( "Use Ctrl+C to return back to this menu, or use it here to exit.\n" ) try: action = input( "1: Encrypt a file\n2: Download and encrypt a file from a URL\n3: Decrypt a file\n4: Change password\nSelect an option: " ) except: break action = action.strip().lstrip() try: action = int(action) except: continue if action not in [1, 2, 3, 4]: continue if action == 1: functions.clear() try: path = input("Please specify a file to encrypt: ") except KeyboardInterrupt: continue if functions.validate(path) and not path.endswith(".png"): functions.clear() try: response = input( "This file may be a valid image file. Convert to .png (y/n)? " ) except KeyboardInterrupt: continue if response.lower().lstrip().strip() == "y": temp = path path = functions.convert(path) delete = input("Delete original file (y/n)? ") if delete.lower().lstrip().strip() == "y": if not windows: temp = temp.replace("\\", "") try: directory = os.path.dirname(os.path.realpath(temp)) os.chdir(directory) os.remove(temp) except: pass functions.clear() try: save = functions.encodepath(path) if save == None: continue except Exception as e: print(str(e)) try: input("Invalid file path. Press enter to continue. ") except KeyboardInterrupt: continue continue else: try: input( "File encrypted.\nEncrypted file path: {}\nPress enter to continue. " .format(save)) except KeyboardInterrupt: continue continue elif action == 2: functions.clear() try: url = input("Please specify a file URL to download: ") except KeyboardInterrupt: continue name = os.path.split(url)[-1] base = os.path.dirname(os.path.realpath(__file__)) path = os.path.join(base, name) functions.clear() try: r = requests.get(url, stream=True) with open(path, 'wb') as out_file: shutil.copyfileobj(r.raw, out_file) except: try: input("Invalid URL: {}\nPress enter to continue. ".format( url)) except KeyboardInterrupt: continue continue if functions.validate(path) and not path.endswith(".png"): functions.clear() try: response = input( "This file may be a valid image file. Convert to .png (y/n)? " ) except KeyboardInterrupt: continue if response.lower().lstrip().strip() == "y": temp = path path = functions.convert(path) delete = input("Delete original file (y/n)? ") if delete.lower().lstrip().strip() == "y": if not windows: temp = temp.replace("\\", "") try: directory = os.path.dirname(os.path.realpath(temp)) os.chdir(directory) os.remove(temp) except: pass functions.clear() try: save = functions.encodepath(path) except: try: input("Invalid file path. Press enter to continue. ") except KeyboardInterrupt: continue continue else: try: input( "File encrypted.\nEncrypted file path: {}\nPress enter to continue. " .format(save)) except KeyboardInterrupt: continue continue elif action == 3: functions.clear() try: path = input("Please specify a .enc file to decrypt: ") except KeyboardInterrupt: continue functions.clear() try: save = functions.decodepath(path) except Exception as e: print(str(e)) try: input("Invalid file path. Press enter to continue. ") except KeyboardInterrupt: continue continue else: try: input( "File decrypted.\nDecrypted file path: {}\nPress enter to continue. " .format(save)) except KeyboardInterrupt: continue continue elif action == 4: try: functions.clear() current = input("Current password: "******"Passwords do not match. Press enter to continue. ") continue new = input("New password: "******"You will need to decrypt all files with your old password. Your new password will not be able to decrypt already encrypted files.\nRetype your password to confirm: " ) if confirm != new: input("Passwords do not match. Press enter to continue. ") continue functions.writepw(new) functions.clear() input("Password changed to: {}.\nPress enter to continue. ". format(new)) except KeyboardInterrupt: continue functions.clear()
def formHistogram(outputFile, listOfDirs): global maximum, minimum, numBins, volts, eSqH, binSize, listofBins, condData, steps, timeData, shifts, numEmpty fs = None listOfBins = functions.createZeroedList(numBins) print("Number of files opened: "+str(len(listOfDirs))) print("Min = "+str(minimum)+" Max = "+str(maximum)) binSize = (maximum-minimum)/(numBins-1) print("BinSize = "+str(binSize)) #go through file sorting data for i in range(0, len(listOfDirs), 1): success = True try: fs = open(listOfDirs[i], "r") #print("Successfully opened "+directory) except: print("FAILED to open "+listOfDirs[i]) success = False if success == True: #read file for line in fs: cell = line.split(",") condData.append(functions.convert(float(cell[4]), volts, eSqH)) timeData.append(float(cell[3])) recursiveStepSearch(0, len(condData)-1, condData, timeData) steps = functions.removeDuplicates(steps) steps = functions.removeMinMaxValues(steps, max(condData)) if len(steps) == 0: numEmpty+=1 for item in steps: listOfBins[int(float(item+abs(minimum))/(binSize))] += 1 steps = [] condData = [] timeData =[] fs.close() print("Number of files where steps could not be detected: "+str(numEmpty)) #Estimate peaks baseAvg = functions.findBaselineAvg(listOfBins, numBins) peakData = functions.findpeaks(listOfBins, numBins, binSize, baseAvg) print("Calculating peaks...") peakXVal = [] for peak in peakData: peakXVal.append(peak*binSize) print("Peaks found at ") print(peakXVal) #write results to a file fs = open(outputFile, "w") j = 0 halfBin = binSize/2 for i in range(0, numBins, 1): try: if peakData[j] == i: fs.write((str((i*binSize)-abs(minimum)+halfBin))+","+str(listOfBins[i])+",20,"+str(baseAvg[i])+"\n") j+=1 else: fs.write((str((i*binSize)-abs(minimum)+halfBin))+","+str(listOfBins[i])+",0,"+str(baseAvg[i])+"\n") except: fs.write((str((i*binSize)-abs(minimum)+halfBin))+","+str(listOfBins[i])+",0,"+str(baseAvg[i])+"\n") print("Output File created.") fs.close()
def test_convert(): assert convert(1.00) == 'um real' assert convert(1000.54) == 'mil reais e cinquenta e quatro centavos' assert convert(1000.10) == 'mil reais e dez centavos' assert convert(100000.10) == 'cem mil reais e dez centavos' assert convert(0.10) == 'dez centavos' assert convert(0.01) == 'um centavo' assert convert(0.99) == 'noventa e nove centavos' assert convert(1.00) == 'um real' assert convert(10.00) == 'dez reais' assert convert( 999000.01) == 'novecentos e noventa e nove mil reais e um centavo' assert convert(100000.01) == 'cem mil reais e um centavo' assert convert(60000.11) == 'sessenta mil reais e onze centavos' assert convert(1000.00) == 'mil reais' assert convert(1000000.00) == 'um milhão de reais' assert convert( 1130200.24 ) == 'um milhão cento e trinta mil duzentos reais e vinte e quatro centavos' assert convert(5000000.00) == 'cinco milhões de reais' assert convert(50000000.00) == 'cinquenta milhões de reais' assert convert(53128040.32) == 'cinquenta e três milhões cento e vinte e oito mil e quarenta reais ' \ 'e trinta e dois centavos' assert convert(99999999.99) == 'noventa e nove milhões novecentos e noventa e nove mil novecentos e ' \ 'noventa e nove reais e noventa e nove centavos' assert convert(999999999.99) == 'novecentos e noventa e nove milhões novecentos e noventa e nove mil novecentos e ' \ 'noventa e nove reais e noventa e nove centavos'
def formHistogram(outputFile, listOfDirs): global maximum, minimum, numBins, volts, eSqH, binSize, listofBins, condData, timeData, stripSize, numSteps, steps, shifts, numEmpty listOfBins = functions.createZeroedList(numBins) print("Number of files opened: "+str(len(listOfDirs))) print("Min = "+str(minimum)+" Max = "+str(maximum)) binSize = (maximum-minimum)/(numBins-1) print("BinSize = "+str(binSize)) print("Finding steps...") #go through file sorting data for i in range(0, len(listOfDirs), 1): success = True try: fs = open(listOfDirs[i], "r") #print("Successfully opened "+directory) except: print("FAILED to open "+listOfDirs[i]) success = False if success == True: #read file Empty = True for line in fs: cell = line.split(",") condData.append(functions.convert(float(cell[4]), volts, eSqH) - shifts[i]) timeData.append(float(cell[3])) marker = 0 while marker < stripSize: steps = [] while marker < (2500-stripSize): stripDataX = [] stripDataY = [] for i in range (marker, marker+stripSize, 1): stripDataX.append(timeData[i]) stripDataY.append(condData[i]) b = functions.regressionFindB(stripDataX, stripDataY) sumY = 0 if abs(b) < 10000: # Fudge Factor numSteps += 1 for item in stripDataY: sumY += item sumY = sumY/stripSize steps.append(sumY) if len(steps) > 0: steps = functions.removeDuplicates(steps) steps = functions.removeMinMaxValues(steps, max(condData)) Empty = False marker += stripSize for item in steps: listOfBins[int(float(item+abs(minimum))/(binSize))] += 1 marker += 1 if Empty == True: numEmpty+=1 condData = [] timeData =[] fs.close() print("Number of files where steps could not be detected: "+str(numEmpty)) #Estimate peaks baseAvg = functions.findBaselineAvg(listOfBins, numBins) peakData = functions.findpeaks(listOfBins, numBins, binSize, baseAvg) print("Calculating peaks...") peakXVal = [] for peak in peakData: peakXVal.append(peak*binSize) print("Peaks found at ") print(peakXVal) #write results to a file fs = open(outputFile, "w") j = 0 halfBin = binSize/2 for i in range(0, numBins, 1): try: if peakData[j] == i: fs.write((str((i*binSize)-abs(minimum)+halfBin))+","+str(listOfBins[i])+",20,"+str(baseAvg[i])+"\n") j+=1 else: fs.write((str((i*binSize)-abs(minimum)+halfBin))+","+str(listOfBins[i])+",0,"+str(baseAvg[i])+"\n") except: fs.write((str((i*binSize)-abs(minimum)+halfBin))+","+str(listOfBins[i])+",0,"+str(baseAvg[i])+"\n") print("Output File created.") fs.close()
# -*- coding: utf-8 -*- ########################################################################## # Project: COMP6004 - Machine learning pipeline for data analysis # File: 03-featureExtraction.py # Author: Diego Bueno - [email protected] # Date: 20/04/2021 # Description: Applying feature extraction to step03 of ML pipeline. # ########################################################################## # Maintenance # Author: # Date: # Description: A # ##########################################################################> import numpy as np import pandas as pd from functions import openfile from functions import savefile from functions import convert from sklearn.feature_selection import VarianceThreshold from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression from sklearn.neighbors import LocalOutlierFactor from sklearn.metrics import mean_absolute_error #calling the function to Load data pre-reading on task 1 print("\nReading the step02 file\n") db = openfile('data/step02.csv') print("\nChecking the current shape of the data:") rows, columns = db.shape
#correlations in heatmap are aligned with feature selection results: with unknown name and intervention variables being the most strongly correlated with the outcome #we need to select non-repeated features according to the scores #how many features should we select? Just try the methods with different number of features and select best? #think we can check feature importance and impact of using different features when testing classifiers, but maybe not as a pre-step given that the choice will be biased by the specific method being used and is only possible for tree based classifiers #Top features from correlation are also high in PCA importance rank #final features: keep common_name_85 and common_colour_98; keep age and bins but never use together (same for weekend and weekday_sin) #classifiers: linear SVC, random forest, logistic regression with multinomial distribution #df = func.balance_dataset(df, 'OutcomeType') #print(df['OutcomeType'].value_counts()) #OAO baseline classification #func.OAO_classif(df.drop(['AnimalID'],axis=1), 'OutcomeType') #OAA baseline classification cols = func.convert(features_select_K, 'list') X = df[cols] y = df['OutcomeType'] func.OAA_classif(X, y) #baseline classification: compare onevsone, onevsall and all&one approaches with each other and Andreia's baseline (starting with all variables and imbalanced sample); use not only accuracy but also confusion matrix and multiclass AUC #no proved best approach for imbalanced multiclass problems, depends on the dataset #gradient boosting seems promising; SVC slightly better with OAO approach; LR also not too bad; rest really bad #balancing the classes reduces the overall accuracy for all OAA models, but improves classification of smaller classes (Died and Euthanasia), since precision is simillar in both circumstancies both recall improves generally with a balanced dataset #Gradient boosting seems to be the only OAA classifier worth keeping #OAO SVC benefits highly from balancing the dataset #predict_proba to return probabilities (cross validation already included in this function); probability = True in model definition #with log-loss, it seems better to not balance the dataset #dataset unbalanced towards neutered/spayed, which is more than double 'intact' animals (might need to omit this variable); gender balance is fine
from functions import convert print('Digite "s" para sair.') while True: number = input('Digite um número:\n').strip() if number == 's': break print(convert(number), '\n')
def __init__(self, screen, space, entities, pos): super().__init__(screen, space, entities) self.pos = functions.convert((pos[0] - 96, pos[1] + 96)) self.index = 0 self.screen = screen
def draw(self): functions.rotate(self.screen, self.bodyImg, functions.convert(self.body.position), (132 / 2, 192 / 2), math.degrees(self.body.angle)) self.barrel.draw()
def __str__(self): return convert(self.dice_dict)
def draw(self): self.screen.blit( self.image, functions.convert((self.body.position[0] - self.radius, self.body.position[1] + self.radius)))
def data(): mazeArray = pfun.convert().tolist() return jsonify({'results': mazeArray})
os.chdir(current_path) # Check if output directory exists if os.path.exists(OUTPUT): print("Output directory already exists") exit() # Create directories os.makedirs("{}/img_bmp".format(OUTPUT)) os.makedirs("{}/img_potrace".format(OUTPUT)) os.makedirs("{}/quote_pdf".format(OUTPUT)) os.makedirs("{}/grid_pdf".format(OUTPUT)) # Create bmp files of the images for i in range(1, IMAGES + 1): functions.convert('data/img/{i}.png'.format(i=i), '{out}/img_bmp/{i}.bmp'.format(out=OUTPUT, i=i)) functions.negate('{out}/img_bmp/{i}.bmp'.format(out=OUTPUT, i=i)) # Create pdf of the images by running potrace for i in range(1, IMAGES + 1): functions.potrace('{out}/img_bmp/{i}.bmp'.format(out=OUTPUT, i=i), '{out}/img_potrace/{i}.pdf'.format(out=OUTPUT, i=i)) # Create the quotes pdf line_no = 1 with open("data/quotes", 'r') as quotes_file: for quote in quotes_file: print("Creating quote {}".format(line_no)) if settings.args.preview: functions.quote_pdf(quote, settings.args.font, "{out}/quote_pdf/{i}.pdf".format(out=OUTPUT, i=line_no)) else: