I'm using timer2 in Python to save data from an IMU sensor into a file every 10 miliseconds (100Hz). I tell to timer2 to run the function every 10 ms with the method "apply_interval".
However, the timestamp show that the function is called irregularely and too many times (1 per ms)
I need to save the data at the same precise interval in order to perform machine learning.
I will be happy if you could fix it with me.
Here is the code :
from numpy import linalg as LA
from conf import *
import RTIMU
from ledFunctions import *
import timer2
class IMU():
def retrieve_data(self):
t = time.time()
timestamp = t - self.last_time
self.last_time = t
accel = self.data["accel"]
gyro = self.data["gyro"]
mag = self.data["compass"]
f = open(self.nameFile, "a")
f.write(str(accel[0]) + ";" + str(accel[1]) + ";" + str(accel[2]) + ";" +
str(gyro[0]) + ";" + str(gyro[1]) + ";" + str(gyro[2]) + ";" +
str(mag[0]) + ";" + str(mag[1]) + ";" + str(mag[2]) + ";" + str(timestamp) + "\n")
f.flush()
f.close()
def __init__(self):
self.last_time = time.time()
print("Using settings file " + SETTINGS_FILE + ".ini")
if not os.path.exists(SETTINGS_FILE + ".ini"):
print("Settings file does not exist, will be created")
self.s = RTIMU.Settings(SETTINGS_FILE)
# Create IMU object
self.imu = RTIMU.RTIMU(self.s)
print("IMU Name: " + self.imu.IMUName())
# Init IMU
if (not self.imu.IMUInit()):
print("IMU Init Failed")
sys.exit(1)
else:
print("IMU Init Succeeded")
# This is a good time to set any fusion parameters
self.imu.setSlerpPower(0.02)
self.imu.setGyroEnable(True)
self.imu.setAccelEnable(True)
self.imu.setCompassEnable(True)
# Check if save directory exists
self.imuDir = ifDirExists(imuDirectoryName)
nameFileNum = self.imuDir + "/nb"
# Check if nb file exists
if not (os.path.isfile(nameFileNum)):
fnb = open(nameFileNum, "w")
fnb.write(str(0))
fnb.flush()
fnb.close()
nb = 0
print("nb file does not exist, create it")
else:
fnb = open(nameFileNum, "r")
nb = int(fnb.readline().strip())
fnb.close()
print("nb file exists")
# Create new file with name = nb + 1
nb = nb + 1
self.nameFile = self.imuDir + "/" + str(nb)
try:
f = open(self.nameFile, "a")
# Increment the value in nb file
fnb = open(nameFileNum, "w")
fnb.write(str(nb))
fnb.flush()
fnb.close()
self.lastDisplay = time.time()
self.poll = self.imu.IMUGetPollInterval()
self.deltat = 0
self.now = self.lastDisplay = time.time()
except IOError as e:
print "I/O Error({0}): {1}".format(e.errno, e.strerror)
# Retrieve data when available
if self.imu.IMURead():
self.data = self.imu.getIMUData()
timer2.apply_interval(100, self.retrieve_data)
def loop(self):
# Retrieve data when available
if self.imu.IMURead():
self.data = self.imu.getIMUData()
Thx :)
Related
The script below works fine when running it with (F9). When running it with (F5) it still works (all operations are done fine), but produces the following error:
File "", line 1, in runfile('S:/Fakultaet/MFZ/NWFZ/AGdeHoz/Philipp/Python/Analysis_script.py',wdir='S:/Fakultaet/MFZ/NWFZ/AGdeHoz/Philipp/Python')
File "c:\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py",
line 709, in runfile namespace.pop('file')
KeyError: 'file'
I would like to know what is happening and how to fix it.
I read something about a nesting problem, so I took out the part where I import my own function and also the part where I use it. The error still persists. So it has at least nothing to do with my own function.
from IPython import get_ipython
get_ipython().magic('%reset -f')
import time
tic = time.time()
import sys
import os
import pickle
import CSC_getdata_timestamps
General_folder = 'Data' #where processed data will be saved
filename_extension = '_Sleep_auditory'; #file extension name
Database_name = 'mydata.obj' #name of database
ExpDates = 'ExpDates.obj' #name of object containing all experimental
dates
root = r"S:\Fakultaet\MFZ\NWFZ\AGdeHoz\Philipp" #root directory (data
location)
AnalyType = "LFP" #"SP" Local field potential or Spike analysis
spr = 30000 #sample rate
Cname = "CH"
file_md = open(r"S:\Fakultaet\MFZ\NWFZ\AGdeHoz\Philipp\Data\Database\\" +
Database_name, "rb")
md = pickle.load(file_md)
file_ExpDates =
open(r"S:\Fakultaet\MFZ\NWFZ\AGdeHoz\Philipp\Data\Database\\" +
ExpDates, "rb")
ExpDates = pickle.load(file_ExpDates)
for mi, expdates in enumerate(ExpDates):
day = expdates
mouse = md[expdates]['mouse']
files = md[expdates]['rectime']
filesm = md[expdates]['FRAm'] + md[expdates]['REMm'] + md[expdates]
['SWSm'] + md[expdates]['awakem']
rect = md[expdates]['rect']
filest = [md[expdates]['FRAt'], md[expdates]['REMt'], md[expdates]
['SWSt'], md[expdates]['awaket']]
channels = md[expdates]['channels']
for f, files_m in enumerate(filesm):
try:
if len(files) == 1:
recname = files
else:
tfiles = []
tfilesm = []
for i, file in enumerate(files):
tfiles.append(int(files[i][0:2]) * 60 + int(files[i]
[3:5]))
tfilesm = int(files_m[f][0:2]) * 60 + int(files_m[f]
[3:5])
recidx = [i for i in range(len(tfiles)) if tfiles[i] <
tfilesm]
recname = files[recidx[-1]]
TS0 = (int(rect[0:2]) * 3600 + int(rect[3:5]) * 60 +
int(rect[6:8])) * spr
TS1 = TS0 + (int(filest[f][0][0:2]) * 3600 + int(filest[f][0]
[3:5]) * 60 + int(filest[f][0][6:8])) * spr
TS2 = TS0 + TS1 + (int(filest[f][1][0:2]) * 3600 +
int(filest[f][1][3:5]) * 60 + int(filest[f][1][6:8])) * spr
TS = [TS1, TS2]
savename = files_m[0:2] + "-" + files_m[3:5]
if AnalyType == "LFP":
if not os.path.exists(root + "\Data\Raw" +
filename_extension + "\LFP\\" + day):
os.makedirs(root + "\Data\Raw" + filename_extension +
"\LFP\\" + day)
else:
print("Directory already exists.")
elif AnalyType == "SP":
if not os.path.exists(root + "\Data\Raw" +
filename_extension + "\SP\\" + day):
os.makedirs(root + "\Data\Raw" + filename_extension +
"\SP\\" + day)
else:
print("Directory already exists.")
else:
sys.exit("Invalid type of analysis. Check variable
AnalyType.")
varlist, trseq, trlength, stlength, ntrials, datacscini,
samplefreq, tscsc, tstrig =
CSC_getdata_timestamps.CSC_getdata_timestamps(root, day,
savename, recname, channels, AnalyType, TS)
print(trseq)
except Exception as e:
print('Error: ' + str(e))
print('mi = {}, f = {}'.format(mi, f))
toc = round(time.time() - tic, 2)
print("elapsed time:", toc, "seconds")
The whole script is to combine data from a (1) written database, with a (2) dataframe which includes specifications of a sound played during a (3) recording.
forgive me if it is written very unpythonic, I just recently switched from Matlab.
The script is for a application in Neuroscience.
(Spyder maintainer here) This error is fixed in our latest version (3.3.4). Please update by opening the Anaconda prompt and running there
conda install spyder=3.3.4
I have a python code to parse a 1 TB log file, but the problem is my result is shown after the parsing process is finished. So for that I need to wait for 12 hours, and after 12 hours only then the result is shown. I want to know how can I parse a log file and know the result of the parsing speed every 10 seconds.
This is my code:
import re
import timeit
log_file = '/Users/kiya/Desktop/mysql/ipscan/ip.txt'
output_file ='/Users/kiya/Desktop/mysql/ipscan/k2u.csv'
name_to_check = 'MBX_AUTHENTICATION_FAILED'
class Log_minning:
def __init__(self):
self.counter = 0
def get_userdata(self):
user_att = []
list_usr = []
counterr = 0
with open(log_file, encoding='utf-8') as infile:
for line in infile:
if name_to_check in line:
username = re.search(r'(?<=userName=)(.*)(?=,)', line)
username = username.group()
date = re.search(r"([12]\d{3}(0[1-9]|1[0-2])+"
"(0[1-9]|[12]\d|3[01]))", line)
date = date.group()
time = re.search(r"(\d{9}\+\d{4})", line)
time = time.group()
ip = re.search(
r'(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)'
'{3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])',
line
)
ip = ip.group()
user_att.append(username)
user_att.append(date)
user_att.append(time)
user_att.append(ip)
list_usr.append(user_att)
counterr = counterr + 1
self.counter = counterr
return list_usr
if __name__ == "__main__":
lm = Log_minning()
the_time = timeit.Timer(lm.get_userdata).repeat(1, 1000)
sing_time = min(the_time)/1000
speed = 600 / sing_time * lm.counter
# for line in lm.get_userdata():
# print(line)
print(
"Processing " + str(lm.counter) + " in " + str(the_time) +
"\nThe speed aproximately " + str(speed) + " data in 10 sec"
)
This is the fully scanned seconds
Processing 117 in [6.646515152002394]
I am currently blocked on a point of a program in Python.
I wish to compare in a list, the WindowName event to launch directives.
Example:
import win32api
import pyHook
liste = ["Google", "Task"]
if event.WindowName == liste:
Screenshot ()
return True
else:
return False
Complete code, he work:
def OnMouseEvent(event):
global interval
data = '\n[' + str(time.ctime().split(' ')[3]) + ']' \
+ ' WindowName : ' + str(event.WindowName)
data += '\n\tButton:' + str(event.MessageName)
data += '\n\tClicked in (Position):' + str(event.Position)
data += '\n===================='
global t, start_time, pics_names
"""
Code Edit
"""
t = t + data
if len(t) > 300:
ScreenShot()
"""
Finish
"""
if len(t) > 500:
f = open('Logfile.txt', 'a')
f.write(t)
f.close()
t = ''
if int(time.time() - start_time) == int(interval):
Mail_it(t, pics_names)
start_time = time.time()
t = ''
return True
else:
return False
When i edit the code in """ doesn't work :
t = t + data
liste = ["Google", "Task"]
if event.WindowName == liste:
ScreenShot()
He return :
File "C:\Python26\lib\site-packages\pyHook\HookManager.py", line 324, in MouseSwitch func = self.mouse_funcs.get(msg) TypeError: an integer is required
I test this :
HookManager: func = self.keyboard_funcs.get(msg) to: func=self.keyboard_funcs.get( int(str(msg)) )
But is don't work, i think i note all problem.
Thanks for you help in advance :)
This small scripts makes exactly what I need.
#!/usr/bin/python
import os
import fileinput
import sys
import shutil
import glob
import time
def replaceAll1(files,searchExp,replaceExp):
for line in fileinput.input(files, inplace=1):
if searchExp in line:
line = line.replace(searchExp,replaceExp)
sys.stdout.write(line)
param1 = [1,2,3]
param2 = [1,2,3]
param3 = [1,2,3]
for i in xrange(len(param1)):
for ii in xrange(len(param2)):
for iii in xrange(len(param3)):
os.system("cp -a cold.in input.in")
old_param1 = "param1 = 1"
old_param2 = "param2 = 1"
old_param3 = "param3 = 1"
new_param1 = "param1 = " + str(param1[i])
new_param2 = "param2 = " + str(param2[ii])
new_param3 = "param3 = " + str(param3[iii])
replaceAll1('input.in',old_param1,new_param1)
replaceAll1('input.in',old_param2,new_param2)
replaceAll1('input.in',old_param3,new_param3)
time.sleep(4)
It enters in a configuration file and replaces sequentially the input parameters according to the lists that are accessed by the loop indexes. It is simple a combination of all the three parameters between each other.
# Input file
param1 = 1 # --- Should be [1,2,3]
param2 = 1 # --- Should be [1,2,3]
param3 = 1 # --- Should be [1,2,3]
The problem is that his big brother is not behaving like it. When it loops through the lists, it gets lost in scheme = 2 and puts dissp_scheme = 2 (freezed) when it should be dissp_scheme = 1. I printed out every single variable that goes inside the function replaceAll marked with comments but when I turn on the other calls it mess up everything. Here is the script.
#!/usr/bin/python
import os
import fileinput
import sys
import shutil
import glob
import time
os.chdir(os.getcwd())
# Replaces the input file parameters
def replaceAll(files,searchExp,replaceExp):
for line in fileinput.input(files, inplace=1):
if searchExp in line:
line = line.replace(searchExp,replaceExp)
sys.stdout.write(line)
# Gets a number inside my input file.
def get_parameter(variable,file_name):
f = open(file_name,'r').readlines()
for i in xrange(len(f)):
index = f[i].find(variable)
if index != -1:
pre_found = f[i].split('=')[1]
return pre_found
# Gets the discretization scheme name.
def get_sheme(number):
if number == 1:
return "Simple Centered Scheme"
elif number == 2:
return "Lax-Wendroff Scheme"
elif number == 3:
return "MacCormack Scheme"
elif number == 4:
return "Beam-Warming Scheme"
elif number == 5:
return "Steger-Warming 1st Order Scheme"
elif number == 6:
return "Steger-Warming 2nd Order Scheme"
elif number == 7:
return "Van Leer 1st Order Scheme"
elif number == 8:
return "Van Leer 2nd Order Scheme"
elif number == 9:
return "Roe Scheme"
elif number == 10:
return "AUSM Scheme"
# Gets the dissipation scheme name.
def get_dissip(number):
if number == 1:
return "Pullian Non-Linear dissipation"
elif number == 2:
return "Second difference dissipation"
elif number == 3:
return "Fourth difference dissipation"
elif number == 4:
return "B&W dissipation"
# Generates the density gnuplot scripts.
def gnuplot(variable,pressure_ratio,scheme,dissip_scheme):
#gnuplot('Density',10,get_sheme(3),'Pullian')
# Building name of the output file.
outFileName = variable.lower() + '_ratio' + str(int(pressure_ratio)) + '_' + scheme.replace(" ","") + '_dissp' + dissip_scheme.replace(" ","") + '.tex'
gnuFileName = variable.lower() + '_ratio' + str(int(pressure_ratio)) + '_' + scheme.replace(" ","") + '_dissp' + dissip_scheme.replace(" ","") + '.gnu'
# Build title of the plot
title = 'Analytical vs Numerical | ' + scheme
f = open(gnuFileName,'w')
f.write("set term cairolatex monochrome size 15.0cm, 8cm\n")
f.write('set output "' + outFileName + '"\n')
f.write("set grid\n")
f.write('set xtics font "Times-Roman, 10\n')
f.write('set ytics font "Times-Roman, 10\n')
f.write('set xlabel "x position" center\n')
f.write('set ylabel "' + variable + '" center\n')
f.write('set title "Analytical vs Numerical Results | ' + variable + '" \n')
f.write('set pointsize 0.5\n')
f.write('set key font ",10"\n')
fortran_out_analytical = 'a' + variable.lower() + '.out'
fortran_out_numerical = variable.lower() + 'Output.out'
f.write('plot "' + fortran_out_analytical +'" u 1:2 with linespoints lt -1 lw 1 pt 4 title "Analytical",\\\n')
f.write( '"' + fortran_out_numerical + '" u 1:2 with lines lw 5 title "Numerical"\n')
f.close()
# Generate latex code.
def generate_latex(text_image_file,caption):
latex.write("\\begin{figure}[H]\n")
latex.write(" \centering\n")
latex.write(" \input{" + text_image_file + "}\n")
latex.write(" \caption{"+ caption +"}\n")
latex.write(" \label{fig:digraph}\n")
latex.write("\\end{figure}\n")
latex.write("\n\n")
# -----------------------------------------------------------------------
# Main loop.
# -----------------------------------------------------------------------
pressure_ratios = [5.0]
schemes = [1,2,3]
dissips = [1,2,3]
# Define replace lines for replace all.
scheme_line = "scheme = "
dissip_line = "dissp_scheme = "
# Open Latex export file.
latex = open("bizu.txt",'w')
i = 0
# ratios.
for i in xrange(len(pressure_ratios)):
print "----------------------------------------"
print " + Configuring File for pressure ratio: " + str(pressure_ratios[i])
print "----------------------------------------\n"
# Schemes
for jj in xrange(len(schemes)):
print " + Configuring file for scheme: " + get_sheme(schemes[jj]) + "\n"
for kkk in xrange(len(dissips)):
print " + Configuring file for dissip: " + get_dissip(dissips[kkk])
# We always work with a brand new file.
os.system("rm input.in")
os.system("cp -a cold.in input.in")
# Replace pressures.
p1_line_old = 'p1 = 5.0d0'
rho1_line_old = 'rho1 = 5.0d0'
p1_line_new = 'p1 = ' + str(pressure_ratios[i]) + 'd0'
rho1_line_new = 'rho1 = ' + str(pressure_ratios[i]) + 'd0'
replaceAll('input.in',p1_line_old,p1_line_new)
replaceAll('input.in',rho1_line_old,rho1_line_new)
# Replace discretization scheme.
old_scheme = scheme_line + "1"
new_scheme = scheme_line + str(schemes[jj])
#==========================================================
# This call is messing everything up when scheme turns to 2
#==========================================================
replaceAll('input.in',old_scheme,new_scheme)
# Replace dissipation scheme.
old_dissp_scheme = dissip_line + "1"
new_dissp_scheme = dissip_line + str(dissips[kkk])
print p1_line_old
print new_scheme
print new_dissp_scheme
replaceAll('input.in',old_dissp_scheme, new_dissp_scheme)
time.sleep(3)
# ### Calling program
# os.system("./sod")
#
latex.close()
And the input file that the it works on is:
&PAR_physical
p1 = 5.0d0
p4 = 1.0d0
rho1 = 5.0d0
rho4 = 1.0d0
fgamma = 1.4d0
R_const = 287.0d0
F_Cp = 1004.5
F_Cv = 717.5
/
&PAR_geometry
total_mesh_points = 1001
start_mesh_point = -5.0d0
final_mesh_point = 5.0d0
print_step = 100
/
&PAR_numeric
scheme = 3
iterations = 10000
time_step = 0.0001d0
/
&PAR_dissip
dissp_scheme = 3
dissip_omega = 0.5d0
/
Thank you all !
I have the following code which creates the txt file I require from a shp.file with the data I need. I have a folder called profiles containing a few number of shape files named (profil1.shp, profil2.shp, profil3.shp etc.). I was wondering how to create a loop so that the script creates for each file a txt file with the same name (eg. for profil1.shp create profil1.txt, profil2.shp create profil2.txt and so on).
import ogr, os, sys, osr
os.chdir('..\profiles')
file = open('profil1.txt', 'w')
driver = ogr.GetDriverByName('ESRI Shapefile')
datasource = driver.Open('profil1.shp', 0)
if datasource is None:
print 'Could not open file'
sys.exit(1)
layer = datasource.GetLayer()
feature = layer.GetNextFeature()
while feature:
id = feature.GetFieldAsString('ID')
Distanta = feature.GetFieldAsString('DIST')
Z = feature.GetFieldAsString('Z')
geom = feature.GetGeometryRef()
x = str(geom.GetX())
y = str(geom.GetY())
file.write(id + " " + Distanta + " " + "[X]:" + " " + x + ' ' + '[Y]:' + " " + y + " " + " " + "[Z]" + Z + " " + "\n")
feature.Destroy()
feature = layer.GetNextFeature()
datasource.Destroy()
file.close()
edit: the code is returning a Could not open file.Photo of the folder containing the files and their respective names. Safe to assume I am doing something wrong.
import ogr, os, sys, osr,os.path
os.chdir = ('C:\Users\Andrei\Desktop\profil3')
l = os.listdir('C:\Users\Andrei\Desktop\profil3')
for i in l:
if i.endswith('.shp'):
s1 = s.split('.')[0] + '.txt'
file = open(s1, 'w')
driver = ogr.GetDriverByName('ESRI Shapefile')
datasource = driver.Open(i, 0)
if datasource is None:
print 'Could not open file'
sys.exit(1)
layer = datasource.GetLayer()
feature = layer.GetNextFeature()
while feature:
id = feature.GetFieldAsString('ID')
Distanta = feature.GetFieldAsString('DIST')
Z = feature.GetFieldAsString('Z')
geom = feature.GetGeometryRef()
x = str(geom.GetX())
y = str(geom.GetY())
file.write(id + " " + Distanta + " " + "[X]:" + " " + x + ' ' + '[Y]:' + " " + y + " " + " " + "[Z]" + Z + " " + "\n")
feature.Destroy()
feature = layer.GetNextFeature()
datasource.Destroy()
file.close()
You can use os.listdir() to list the files and folders in the current directory.
This returns a list of all files in the current directory (or the directory given to it as parameter , if no parameter is specified it checks the current directory) .
Then you can check for files with the name ending with .shp using string.endswith() function and then use that to create your new files.
Example of a small portion -
import os , os.path
l = os.listdir()
for i in l:
if i.endswith('.shp'):
s1 = s.split('.')[0] + '.txt'
At the end s1 would contain the file with extension as .txt .
Then you can do your logic on this file, and keep on doing like this.
Full code would look something like -
import ogr, os, sys, osr,os.path
os.chdir('..\profiles')
l = os.listdir()
for i in l:
if i.endswith('.shp'):
s1 = s.split('.')[0] + '.txt'
file = open(s1, 'w')
driver = ogr.GetDriverByName('ESRI Shapefile')
datasource = driver.Open(i, 0)
if datasource is None:
print 'Could not open file'
sys.exit(1)
layer = datasource.GetLayer()
feature = layer.GetNextFeature()
while feature:
id = feature.GetFieldAsString('ID')
Distanta = feature.GetFieldAsString('DIST')
Z = feature.GetFieldAsString('Z')
geom = feature.GetGeometryRef()
x = str(geom.GetX())
y = str(geom.GetY())
file.write(id + " " + Distanta + " " + "[X]:" + " " + x + ' ' + '[Y]:' + " " + y + " " + " " + "[Z]" + Z + " " + "\n")
feature.Destroy()
feature = layer.GetNextFeature()
datasource.Destroy()
file.close()
A better way of openning files, etc is using with statement. Look up its tutorial here.