I've got a program that takes in input a .txt file with coordinates (t, X, Y). The movement stored in this files is more or less circular, and this program is counting the number of rotation by searching the local optima of X+Y. The last part at the end is to plot the results.
You can find here 2 .txt : https://drive.google.com/file/d/0BxmtjR3C4bYBdnFZUzVwSVlPUlk/view?usp=sharing
Here is the first version :
The Interpolation function is a cubic interpolation that return False if it couldn't Interpolate, or (Frame_final, X_final, Y_final) if it managed to interpolate. The Filtrage function is a first-order lowpass-filter.
def Filtrage(Frame, X, Y, DT = 3, a = 0.1):
"""Fonction réalisant un filtrage passe-bas d'ordre 1 du signal."""
# Initialisation
X_temp, Y_temp = [], []
X_filter, Y_filter = [], []
i = 1
X_temp.append(X[0])
Y_temp.append(Y[0])
# Filtrage par morceau
while i < len(Frame)-1:
while Frame[i] - Frame[i-1] == DT:
Xnew = a * X[i-1] + (1 - a) * X_temp[len(X_temp)-1]
Ynew = a * Y[i-1] + (1 - a) * Y_temp[len(Y_temp)-1]
X_temp.append(Xnew)
Y_temp.append(Ynew)
if i < len(Frame)-1:
i += 1
else:
break
X_filter += X_temp
Y_filter += Y_temp
X_temp, Y_temp = [], []
X_temp.append(X[i])
Y_temp.append(Y[i])
i += 1
return (X_filter, Y_filter)
def Traitement_resultat(Chemin_dossier_res, Trace_Graph):
"""Fonction réalisant le traitement des résultats pour déterminer le nombre de tour
effectué par la trajectoire circulaire.
- Le paramètre Chemin_dossier_res est une str.
- Le paramètre Trace_graph est un booléen
"""
print ("Début du traitement des résultats")
# Listage des fichiers résultats + ouverture du fichier de résultats finaux
Fichiers_res = os.listdir(Chemin_dossier_res)
res = open("Data_Results.txt", "w")
res.write("{} {} {}\n".format("Souris", "Nombre de tours", "Distance parcourue"))
# On boucle sur les fichiers
for i in range(0, len(Fichiers_res)):
# Affiche l'état du traitement
print ("{} / {} : {}".format(str(i+1), str(len(Fichiers_res)), Fichiers_res[i]))
# On ouvre le fichier résultat
results = open("{}/{}".format(Chemin_dossier_res, Fichiers_res[i]), "r")
# On récupère les datas dans 2 listes
X, Y, Frame = [], [], []
c = 0
try:
for ligne in results:
# Permet d'éliminer la première ligne d'en-tête
if c >= 1:
row = ligne.split()
if row[0] == "None":
continue
else:
Frame.append(int(row[0]))
X.append(int(row[1]))
Y.append(int(row[2]))
c += 1
results.close()
except:
print ("Fichier {} eronné ! Il ne sera pas traité.".format(Fichiers_res[i]))
results.close()
continue
if len(X) != len(Y):
print ("Fichier {} eronné ! Il ne sera pas traité.".format(Fichiers_res[i]))
continue
# Inteprolation
Data = Interpolation(Frame, X, Y)
if not Data:
print ("Fichier {} eronné ! Il ne sera pas traité.".format(Fichiers_res[i]))
continue
Frame = Data[0]
X = Data[1]
Y = Data[2]
# Filtrage
Data = Filtrage(Frame, X, Y)
X = Data[0]
Y = Data[1]
Diff_len = len(Frame) - len(X)
if Diff_len != 0:
Frame = Frame[:len(Frame)-Diff_len]
# Suppression des extremum
if len(Frame) > 1000:
for k in range(30):
maxX_ID = X.index(max(X))
del X[maxX_ID]
del Y[maxX_ID]
del Frame[maxX_ID]
minX_ID = X.index(min(X))
del X[minX_ID]
del Y[minX_ID]
del Frame[minX_ID]
maxY_ID = Y.index(max(Y))
del X[maxY_ID]
del Y[maxY_ID]
del Frame[maxY_ID]
minY_ID = Y.index(min(Y))
del X[minY_ID]
del Y[minY_ID]
del Frame[minY_ID]
elif len(Frame) > 100:
for k in range(3):
maxX_ID = X.index(max(X))
del X[maxX_ID]
del Y[maxX_ID]
del Frame[maxX_ID]
minX_ID = X.index(min(X))
del X[minX_ID]
del Y[minX_ID]
del Frame[minX_ID]
maxY_ID = Y.index(max(Y))
del X[maxY_ID]
del Y[maxY_ID]
del Frame[maxY_ID]
minY_ID = Y.index(min(Y))
del X[minY_ID]
del Y[minY_ID]
del Frame[minY_ID]
else:
print ("{} : Moins de 100 pts !!".format(Fichiers_res[i]))
Sum = []
# Conversion en minute et creation de la somme
for k in range(len(Frame)):
Frame[k] = Frame[k] / (30 * 60)
Sum.append(X[k] + Y[k])
if len(X) > 2:
# Détermination de la distance parcourue
Distance = 0
for k in range(len(X)-1):
Distance += sqrt((X[k+1] - X[k])**2 + (Y[k+1] - Y[k])**2)
Segments = range(200, 1400, 200)
Res = []
for k in range(len(Segments)):
if Segments[k] < len(Sum):
Sum_calc = []
length = len(Sum) // Segments[k]
last = len(Sum) % Segments[k]
for j in range(length):
Sum_calc.append(Sum[j:j + Segments[k]])
if last > Segments[k] //2:
Sum_calc.append(X[len(Sum) - last:])
else:
Sum_calc[len(Sum_calc) - 1] = Sum_calc[len(Sum_calc) - 1] + Sum[len(Sum) - last:]
# Initialisation of the counter
K = 0
for j in range(len(Sum_calc)):
Counter = 0
b = 0
Moyenne = np.mean(Sum_calc[j])
while b < len(Sum_calc[j]):
if Sum_calc[j][b] <= Moyenne + 10:
b += 1
continue
else:
Counter += 1
while Sum_calc[j][b] >= Moyenne + 10:
b += 1
try:
Sum_calc[j][b]
except:
break
K += Counter
Res.append(K)
if len(Res) > 4:
Res_F = int(np.mean(sorted(Res)[1:len(Res)-1]))
else:
Res_F = int(np.mean(Res))
# Ajout au fichier résultat des données
res.write(Fichiers_res[i][0:len(Fichiers_res[i])-4] + " " * (25 - len(Fichiers_res[i])) + str(Res_F)
+ " " * (26 - len(str(Res_F))) + str(int(Distance)) + "\n")
if Trace_Graph:
# Création des graphiques
plt.figure(figsize = (15, 11), dpi = 600)
# Ligne 2 / Colonne 1 / Position 1
plt.subplot(2, 1, 1)
plt.plot(X, Y, color = "green", linewidth = 0.3, linestyle="-")
plt.xlim(min(X) - 0.1 * np.mean(X), max(X) + 0.1 * np.mean(X))
plt.ylim(min(Y) - 0.1 * np.mean(Y), max(Y) + 0.1 * np.mean(Y))
plt.xlabel("pixel")
plt.ylabel("pixel")
plt.title("Trajectoire de la souris")
# Ligne 2 / Colonne 1 / Position 2
plt.subplot(2, 1, 2)
plt.plot(Frame, X, color = "blue", linewidth = 0.5, linestyle="-", label = "X")
plt.plot(Frame, Y, color = "red", linewidth = 0.5, linestyle="-", label = "Y")
plt.xlim(0, Frame[len(Frame)-1])
plt.ylim(min(min(X),min(Y)) - 0.1 * min(min(X),min(Y)) , max(max(X),max(Y)) + 0.1 * max(max(X),max(Y)))
plt.legend(loc = 'upper left')
plt.xlabel("Temps (minutes)")
plt.xticks(np.linspace(0, int(Frame[len(Frame)-1]), int(Frame[len(Frame)-1]) + 1))
plt.ylabel("Position")
plt.title("Position X et Y de la souris en fonction du temps")
plt.savefig("{}/{}.png".format("Graphiques", Fichiers_res[i][0:len(Fichiers_res[i])-4]), dpi = 600)
plt.close()
res.close()
print ("Fin du traitement des résultats !")
print ("---------------------------------------")
The second programm is exactly the same, with only 3 minors change : some of the variable are placed in a different file.
File Parameters.py :
# Parameters to adjust the analysis
# Low-pass filtering between 0 and 1
a = 0.1
# Thershold to count the maximum as a local maximum
Moy_Up = 10
# Segments cutting
Segments = range(200, 1400, 200)
Programm File :
from Parameters import *
def Filtrage(Frame, X, Y, DT = 3, a = 0.1):
"""Fonction réalisant un filtrage passe-bas d'ordre 1 du signal."""
# Initialisation
X_temp, Y_temp = [], []
X_filter, Y_filter = [], []
i = 1
X_temp.append(X[0])
Y_temp.append(Y[0])
# Filtrage par morceau
while i < len(Frame)-1:
while Frame[i] - Frame[i-1] == DT:
Xnew = a * X[i-1] + (1 - a) * X_temp[len(X_temp)-1]
Ynew = a * Y[i-1] + (1 - a) * Y_temp[len(Y_temp)-1]
X_temp.append(Xnew)
Y_temp.append(Ynew)
if i < len(Frame)-1:
i += 1
else:
break
X_filter += X_temp
Y_filter += Y_temp
X_temp, Y_temp = [], []
X_temp.append(X[i])
Y_temp.append(Y[i])
i += 1
return (X_filter, Y_filter)
def Traitement_resultat(Chemin_dossier_res, Trace_Graph):
"""Fonction réalisant le traitement des résultats pour déterminer le nombre de tour
effectué par la trajectoire circulaire.
- Le paramètre Chemin_dossier_res est une str.
- Le paramètre Trace_graph est un booléen
"""
print ("Début du traitement des résultats")
# Listage des fichiers résultats + ouverture du fichier de résultats finaux
Fichiers_res = os.listdir(Chemin_dossier_res)
res = open("Data_Results.txt", "w")
res.write("{} {} {}\n".format("Souris", "Nombre de tours", "Distance parcourue"))
# On boucle sur les fichiers
for i in range(0, len(Fichiers_res)):
# Affiche l'état du traitement
print ("{} / {} : {}".format(str(i+1), str(len(Fichiers_res)), Fichiers_res[i]))
# On ouvre le fichier résultat
results = open("{}/{}".format(Chemin_dossier_res, Fichiers_res[i]), "r")
# On récupère les datas dans 2 listes
X, Y, Frame = [], [], []
c = 0
try:
for ligne in results:
# Permet d'éliminer la première ligne d'en-tête
if c >= 1:
row = ligne.split()
if row[0] == "None":
continue
else:
Frame.append(int(row[0]))
X.append(int(row[1]))
Y.append(int(row[2]))
c += 1
results.close()
except:
print ("Fichier {} eronné ! Il ne sera pas traité.".format(Fichiers_res[i]))
results.close()
continue
if len(X) != len(Y):
print ("Fichier {} eronné ! Il ne sera pas traité.".format(Fichiers_res[i]))
continue
# Inteprolation
Data = Interpolation(Frame, X, Y)
if not Data:
print ("Fichier {} eronné ! Il ne sera pas traité.".format(Fichiers_res[i]))
continue
Frame = Data[0]
X = Data[1]
Y = Data[2]
# Filtrage
Data = Filtrage(Frame, X, Y, a)
X = Data[0]
Y = Data[1]
Diff_len = len(Frame) - len(X)
if Diff_len != 0:
Frame = Frame[:len(Frame)-Diff_len]
# Suppression des extremum
if len(Frame) > 1000:
for k in range(30):
maxX_ID = X.index(max(X))
del X[maxX_ID]
del Y[maxX_ID]
del Frame[maxX_ID]
minX_ID = X.index(min(X))
del X[minX_ID]
del Y[minX_ID]
del Frame[minX_ID]
maxY_ID = Y.index(max(Y))
del X[maxY_ID]
del Y[maxY_ID]
del Frame[maxY_ID]
minY_ID = Y.index(min(Y))
del X[minY_ID]
del Y[minY_ID]
del Frame[minY_ID]
elif len(Frame) > 100:
for k in range(3):
maxX_ID = X.index(max(X))
del X[maxX_ID]
del Y[maxX_ID]
del Frame[maxX_ID]
minX_ID = X.index(min(X))
del X[minX_ID]
del Y[minX_ID]
del Frame[minX_ID]
maxY_ID = Y.index(max(Y))
del X[maxY_ID]
del Y[maxY_ID]
del Frame[maxY_ID]
minY_ID = Y.index(min(Y))
del X[minY_ID]
del Y[minY_ID]
del Frame[minY_ID]
else:
print ("{} : Moins de 100 pts !!".format(Fichiers_res[i]))
Sum = []
# Conversion en minute et creation de la somme
for k in range(len(Frame)):
Frame[k] = Frame[k] / (30 * 60)
Sum.append(X[k] + Y[k])
if len(X) > 2:
# Détermination de la distance parcourue
Distance = 0
for k in range(len(X)-1):
Distance += sqrt((X[k+1] - X[k])**2 + (Y[k+1] - Y[k])**2)
Res = []
for k in range(len(Segments)):
if Segments[k] < len(Sum):
Sum_calc = []
length = len(Sum) // Segments[k]
last = len(Sum) % Segments[k]
for j in range(length):
Sum_calc.append(Sum[j:j + Segments[k]])
if last > Segments[k] //2:
Sum_calc.append(X[len(Sum) - last:])
else:
Sum_calc[len(Sum_calc) - 1] = Sum_calc[len(Sum_calc) - 1] + Sum[len(Sum) - last:]
# Initialisation of the counter
K = 0
for j in range(len(Sum_calc)):
Counter = 0
b = 0
Moyenne = np.mean(Sum_calc[j])
while b < len(Sum_calc[j]):
if Sum_calc[j][b] <= Moyenne + Moy_Up:
b += 1
continue
else:
Counter += 1
while Sum_calc[j][b] >= Moyenne + Moy_Up:
b += 1
try:
Sum_calc[j][b]
except:
break
K += Counter
Res.append(K)
if len(Res) > 4:
Res_F = int(np.mean(sorted(Res)[1:len(Res)-1]))
else:
Res_F = int(np.mean(Res))
# Ajout au fichier résultat des données
res.write(Fichiers_res[i][0:len(Fichiers_res[i])-4] + " " * (25 - len(Fichiers_res[i])) + str(Res_F)
+ " " * (26 - len(str(Res_F))) + str(int(Distance)) + "\n")
if Trace_Graph:
# Création des graphiques
plt.figure(figsize = (15, 11), dpi = 600)
# Ligne 2 / Colonne 1 / Position 1
plt.subplot(2, 1, 1)
plt.plot(X, Y, color = "green", linewidth = 0.3, linestyle="-")
plt.xlim(min(X) - 0.1 * np.mean(X), max(X) + 0.1 * np.mean(X))
plt.ylim(min(Y) - 0.1 * np.mean(Y), max(Y) + 0.1 * np.mean(Y))
plt.xlabel("pixel")
plt.ylabel("pixel")
plt.title("Trajectoire de la souris")
# Ligne 2 / Colonne 1 / Position 2
plt.subplot(2, 1, 2)
plt.plot(Frame, X, color = "blue", linewidth = 0.5, linestyle="-", label = "X")
plt.plot(Frame, Y, color = "red", linewidth = 0.5, linestyle="-", label = "Y")
plt.xlim(0, Frame[len(Frame)-1])
plt.ylim(min(min(X),min(Y)) - 0.1 * min(min(X),min(Y)) , max(max(X),max(Y)) + 0.1 * max(max(X),max(Y)))
plt.legend(loc = 'upper left')
plt.xlabel("Temps (minutes)")
plt.xticks(np.linspace(0, int(Frame[len(Frame)-1]), int(Frame[len(Frame)-1]) + 1))
plt.ylabel("Position")
plt.title("Position X et Y de la souris en fonction du temps")
plt.savefig("{}/{}.png".format("Graphiques", Fichiers_res[i][0:len(Fichiers_res[i])-4]), dpi = 600)
plt.close()
res.close()
print ("Fin du traitement des résultats !")
print ("---------------------------------------")
I don't know why placing this parameters in a different file is changing my result. When I placed print (a), print (Moy_Up) and print (Segments) in the second program just before they are called, they have the right value (respectively, a, 10 and range(200, 1400, 200)).
But with the first program I get :
Souris Nombre de tours Distance parcourue
1 48 13062
2 44 12927
And with the second one :
Souris Nombre de tours Distance parcourue
1 77 40328
2 76 39235
Moreover, if in the second version I change back :
- Data = Filtrage(Frame, X, Y, a) by Data = Filtrage(Frame, X, Y)
- Moy_Up by 10
and place back the Segments line, my results come back to those of the first version (quite normal).
Thanks for reading this long post, I tried to be explicit with my problem. I just can't understand why placing this variable in a separate file change my result even thou if I print them, they have the right value.
EDIT : I've used the compare module of Notepad, there is nothing else that differs.