Im new at coding and I'm doing a code for algorithms with a request matrix. I'm seeing this error when I add the code with the ***.
Context: I'm doing an input menu with options so the user can with number 1 create a matrix in excel and so python read it. With number 2 now I want to implement Dijkstra Algorithm.
I had search about this type of error and I know that happen because I'm putting a int and not a str, but I can't find the way to change it and so can read my dijkstra algorithm function requesting the initial node and the goal node.
Maybe I have some error in the parameters or variables. I'm not too good with functions calling. (Just starting to code)
import openpyxl as opxl
import sys
cant_nodos = 3
# FUNCIONES
## Crea la cantidad de nodos a utilizar y la restricción de un mínimo de nodos.
def min_nodos():
cant_nodos = int(input("Ingresar cantidad de nodos a utilizar (mínimo 6)"))
while(cant_nodos < 6):
print("ERROR: Elija mínimo 6 nodos y que sea entero positivo. ")
cant_nodos = int(input("Ingresar cantidad de nodos a utilizar (mínimo 6)"))
return cant_nodos
## Crea un menu
def crear_menu():
menu=int(input("Elija una opción \n 1.Crear parámetros \n 2.Aplicar Dijkstra \n 3.Aplicar Kruskal \n 4.Salir"))
if menu == 1:
min_nodos()
elif menu == 2:
*** empezar = str(input("Elija un nodo de inicio: "))
terminar = str(input("Elija un nodo de termino: ")) ***
dijkstra(cant_nodos, empezar, terminar)
elif menu == 3:
kruskal()
elif menu == 4:
sys.exit()
else:
print("\n ERROR: Elija una opción válida.")
crear_menu()
## Crea la matriz en excel
def crear_matriz_adyacente(cant_nodos):
libro = opxl.Workbook()
pagina = libro.active
pagina.title = "matriz_de_adyacencia"
lista_nodos = []
cont = 0
while(cont < cant_nodos):
contador = str(cont+1)
nodo = str(input("Ingrese nodo " +contador+ ":"))
if nodo not in lista_nodos:
lista_nodos.append(nodo)
pagina.cell(row = 1, column = cont+2, value = nodo)
pagina.cell(row = cont+2, column = 1, value = nodo)
cont = cont+1
else:
print("ERROR: Nodo existente, escoja otro: ")
for fila in range(len(lista_nodos)):
for columna in range(len(lista_nodos)):
if fila == columna:
valor = 0
elif columna > fila:
valor = int(input("Ingrese valor de nodo " +lista_nodos[fila]+" con el nodo " +lista_nodos[columna]+ ":"))
while(valor < 0):
print("ERROR: Valor negativo. Ingrese un valor positivo")
valor = int(input("Ingrese valor de nodo " +lista_nodos[fila]+" con el nodo " +lista_nodos[columna]+ ":"))
pagina.cell(row = fila+2, column = columna+2, value = valor)
pagina.cell(row = columna+2, column = fila+2, value = valor)
libro.save("matriz_adyacente.xlsx")
return crear_menu()
## Abre la matriz para utilizarla
def abrir_matriz_adyacente(matriz_adyacente):
excel = opxl.load_workbook(matriz_adyacente)
lista_excel = []
pagina = excel.active
maximo_columna = pagina.max_column
maximo_fila = pagina.max_row
for fila in range(1, maximo_fila+1):
lista_fila = []
for columna in range(1, maximo_columna+1):
espacio = pagina.cell(row = fila, column = columna)
lista_fila.append(espacio.value)
lista_excel.append(lista_fila)
return lista_excel
def dijkstra(cant_nodos, empezar, terminar):
lista_excel = abrir_matriz_adyacente(cant_nodos)
camino_mas_corto = {}
pista_predecedor = {}
nodos_no_explorados = lista_excel
inf = 9999999
pista_camino = []
for nodo in nodos_no_explorados:
camino_mas_corto[nodo] = inf
camino_mas_corto[empezar] = 0
while nodos_no_explorados:
nodo_min_distancia = None
for nodo in nodos_no_explorados:
if nodo_min_distancia is None:
nodo_min_distancia = nodo
elif camino_mas_corto[nodo] < camino_mas_corto[nodo_min_distancia]:
nodo_min_distancia = nodo
opciones_caminos = lista_excel[nodo_min_distancia].items()
for nodo_hijo, peso in opciones_caminos:
if peso + camino_mas_corto[nodo_min_distancia] < camino_mas_corto[nodo_hijo]:
camino_mas_corto[nodo_hijo] = peso + camino_mas_corto[nodo_min_distancia]
pista_predecedor[nodo_hijo] = nodo_min_distancia
nodos_no_explorados.pop(nodo_min_distancia)
nodo_actual = terminar
while nodo_actual != empezar:
try:
pista_camino.insert(0, nodo_actual)
nodo_actual = pista_predecedor[nodo_actual]
except KeyError:
print("El camino no existe")
break
pista_camino(0, empezar)
if camino_mas_corto[terminar] != inf:
print("Distancia más corta es: " + str(camino_mas_corto[terminar]))
print("Camino optimo es: " + str(pista_camino))
crear_menu()
crear_matriz_adyacente(cant_nodos)
lista_excel = abrir_matriz_adyacente("matriz_adyacente.xlsx")
print(lista_excel)
Thank you!
The issue is
lista_excel = abrir_matriz_adyacente(cant_nodos) # cant_nodos = 3
which calls
excel = opxl.load_workbook(matriz_adyacente) # matriz_adyacente = cant_nodos = 3
opxl.load_workbook expects string/bytes as an argument, not an int. Your program isn't that straightforward to understand, so I'm not sure why you're passing an int to the function. Just replace it with the filename of the sheet that you want to open.
I don't understand a specific behavior of Python when I'm trying the refresh a list.
I try to figure it out because I'm stuck in a small project.
Here's the code (it's not the project's code but its just a code to show you the problem):
import time
time_1 = [1,2,3,4,5]
time_change = time_1
def test():
global time_change
time_change.remove(1)
print(time_change)
time.sleep(10)
time_change = time_1
print(time_change)
# What I would like to have is time_change = time_1
test()
# -*- coding: utf-8 -*-
# Les imports
import time
import random
# ===========================================================
# Déclarations des variables globales.
event_type_architecture = " Le secteur de l'architecture. "
event_type_graphisme = " Le secteur du graphisme. "
event_type_developper = " Le secteur du développement. "
event_type_garage = " Le secteur de l'automobile. "
event_type_charpentier = " Le secteur de la charpentrie. "
event_type_avocat = " Le secteur des avocats. "
event_type_medecin = " Le secteur des médecins "
# event_nb_secteurs HERE !
event_tt = [random.randint(1,6)]
event_nb_secteurs = list(event_tt)
types_secteurs = [event_type_architecture , event_type_developper , event_type_graphisme
, event_type_garage , event_type_charpentier, event_type_avocat , event_type_medecin ]
types_secteurs_change = list(types_secteurs)
event_types_secteurs = random.choice(types_secteurs_change)
# ===========================================================
# different events
def event_crise_economique():
print(" \n Une crise économique sévira dans " + str(event_nb_secteurs) +
# I want event_nb_secteurs to go to his initial value which is random.randint(1,6) but doesn't work always print same event_nb_secteurs number.
" secteurs qui sont....\n")
print(event_nb_secteurs)
time.sleep(5)
event_types()
def event_coupure_electricite():
print(" \n Une coupure de courant va arriver dans " + str(event_nb_secteurs) +
" secteurs qui sont.... \n")
time.sleep(5)
event_types()
def event_types():
global types_secteurs_change
global event_nb_secteurs
if event_nb_secteurs == 0 :
quit
for ev_types in range(event_nb_secteurs[0]) :
event_types_secteurs = random.choice(types_secteurs)
if event_types_secteurs in types_secteurs_change :
print(event_types_secteurs)
types_secteurs_change.remove(event_types_secteurs)
event_nb_secteurs = list(event_tt)
types_secteurs_change = list(types_secteurs)
# Le vrai event :
def event ():
random_event = False
nb_hasard = random.randint(9,11)
if nb_hasard >= 9 :
print("\n Oh non ! un évènement va arriver !")
event_random = random.randint(1,4)
if event_random == 1 :
event_coupure_electricite()
elif event_random == 2 :
event_crise_economique()
else :
print("C bon y a r")
for i in range(10):
event()
As I said you need to focus only on event_nb_secteurs which is the problem.
I would like it to go its initial value that is : random.randint(1,6)
But it doesn't work
By doing time_change = time_1, the variable time_change points to the same list pointed by time_1, that's just an alias, you mostly want to make a copy of it
time_1 = [1, 2, 3, 4, 5]
time_change = list(time_1)
def test(): # no need to pause the code, it runs the same without
global time_change
time_change.remove(1)
print(time_change)
time_change = list(time_1)
print(time_change)
>> test()
[2, 3, 4, 5]
[1, 2, 3, 4, 5]
How can I convert a number with a decimal part to the simple precision system of the IEEE-754 in python in such a way that I enter the number and I throw the standard sign, exponent and mantissa?
Example Input: 10.27
Example Output: 0 10000011 01001000101000111101011
Sign-Exponent-Mantissa
Here is my attempt to solve the problem.
# Conversion de Decimal a Binario con parte fraccionaria
def float_bin(num, dig=23):
# split() separa la parte entera de la parte decimal
# Despues de separarlas las asignas a dos variables distintas
ent, dec = str(num).split(".")
# Convert both whole number and decimal
# Cambia el tipo de dato de un string a un entero
ent = int(ent)
dec = int(dec)
# Convierte la parte entera a su respectivo forma binaria el "Ob" es removido con el metodo strip
res = bin(ent).lstrip("0b") + "."
# Itera el numero de veces dependiendo de numero de posiciones decimales que se buscan
for x in range(dig):
# Multiplica la parte fraccionaria por 2 y se separa la parte entera de la parte decimal para repetir el proceso
ent, dec = str((decimal_conv(dec)) * 2).split(".")
# Se convierte la parte fraccionaria a un entero de nuevo
dec = int(dec)
# Keep adding the integer parts
# receive to the result variable
res += ent
return res
# Function converts the value passed as
# parameter to it's decimal representation
def decimal_conv(num10):
while num10 > 1:
num10 /= 10
return num10
# Take the user input for
# the floating point number
n = input("Ingrese su numero de punto flotante : \n")
# Take user input for the number of
# decimal places user want result as
p = int(input("Ingrese el numero de posiciones decimales para el resultado: \n"))
print(float_bin(n, dig=p))
while True:
ParteSigno = input("Ingresa el signo: ")
ParteEntera = list(input("Ingresa la parte entera: "))
ParteDecimal = list(input("Ingresa la parte decimal: "))
if (ParteSigno == '-'):
signo = 1
else:
signo = 0
Recorrido = []
Topepunto = 0
sacador = 0
saca = 0
cont = 0
if '1' in (ParteEntera):
Topepunto = len(ParteEntera) - 1
ExpPar = 127 + Topepunto
ExpBina = bin(ExpPar)
ExpobinList = []
mantisalncom = ParteEntera + ParteDecimal
mantisalncom.reverse()
parte = mantisalncom.pop()
mantisalncom.reverse()
while len(mantisalncom) < 23:
mantisalncom.extend("0")
for i in ExpBina:
ExpobinList.append(i) #El metodo append añade un elemento a la lista
ExpobinList = (ExpobinList[2:])
if len(ExpobinList) < 8:
ExpobinList.reverse()
while len(ExpobinList) <= 8:
ExpobinList.extend('0')
ExpobinList.reverse()
else:
mantisalncom = ParteEntera + ParteDecimal
ParteDecimal.reverse()
mantisalncom.reverse()
while cont == 0:
parte = mantisalncom.pop()
if parte == '0' and cont == 0:
cont = 0
elif parte == '1' and cont == 0:
cont = cont + 1
mantisalncom.reverse()
while len(mantisalncom) < 23:
mantisalncom.extend('0')
while len(ParteDecimal) > 0:
Reco = ParteDecimal.pop()
if (Reco == '0' and sacador == 0):
Recorrido.extend(Reco)
sacador = 0
else:
sacador = sacador + 1
Topepunto = len(Recorrido) + 1
Topepunto = Topepunto * (-1)
ExpPar = 127 + Topepunto
ExpBina = bin(ExpPar)
ExpobinList = []
for i in ExpBina:
ExpobinList.append(i)
ExpobinList = (ExpobinList[2:])
if len(ExpobinList) < 8:
ExpobinList.reverse()
while len(ExpobinList) < 8:
ExpobinList.extend('0')
ExpobinList.reverse()
print("\n\nSigno\t\tExponente\t\t\t\t\t\t\t\tMantisa\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t")
print("", signo, "", ExpobinList, mantisalncom)
From your description ucyos answer is what you are looking for:
def float_to_bin(num):
bits, = struct.unpack('!I', struct.pack('!f', num))
return "{:032b}".format(bits)
print(float_to_bin(10.27))
# 01000001001001000101000111101100
Here is an example for ieee745 32b-format:
def ieee745(N): # ieee-745 bits (max 32 bit)
a = int(N[0]) # sign, 1 bit
b = int(N[1:9],2) # exponent, 8 bits
c = int("1"+N[9:], 2)# fraction, len(N)-9 bits
return (-1)**a * c /( 1<<( len(N)-9 - (b-127) ))
N = "110000011010010011" # str of ieee-745 bits
print( ieee745(N) ) # --> -20.59375
What you want to do is use struct pack and unpack with !f >f or <f.
This code works for 32 bit floating point numbers.
for reference look at: https://docs.python.org/3/library/struct.html
Character
Byte order
Size
Alignment
#
native
native
native
=
native
standard
none
<
little-endian
standard
none
>
big-endian
standard
none
!
network (big-endian)
standard
none
For 64 bit floating point numbers use d in stead of f
import struct
def from_bytes(b):
return struct.unpack('!f', b)[0]
def from_hex(h):
return from_bytes(bytes.fromhex(h))
def from_bin(b):
return struct.unpack('!f', int(b, 2).to_bytes(4, byteorder='big'))[0]
def to_bytes(f):
return struct.pack('!f', f)
def to_hex(f):
return to_bytes(f).hex()
def to_bin(f):
return '{:032b}'.format(struct.unpack('>I', struct.pack('!f', f))[0])
assert from_hex('c4725c44') == -969.441650390625
assert from_bin('11000100011100100101110001000100') == -969.441650390625
assert from_bytes(b'\xc4r\\D') == -969.441650390625
assert to_hex(-969.441650390625) == 'c4725c44'
assert to_bin(-969.441650390625) == '11000100011100100101110001000100'
assert to_bytes(-969.441650390625) == b'\xc4r\\D'
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.
You define Filtrage as follows:
def Filtrage(Frame, X, Y, DT = 3, a = 0.1):
Note the fourth parameter is DT, but when you call it:
Data = Filtrage(Frame, X, Y, a)
...you're passing a as the fourth parameter. You probably mean to do:
Data = Filtrage(Frame, X, Y, a=a)
I have some problem that I can't get out of.
I'm trying to use the API twitter to get some tweets with a certain query.
I make a count with the api.rate_limit_status()['resources']['search']['/search/tweets']['remaining'] not to have the error code 429 of "Too Many Requests".
But before the end I have another error code 88 that means " The request limit for this resource has been reached for the current rate limit window."
So, I'd like to know what's the return limits per request or per 15 minutes ?
does i have some errors in my code ?
My code is here :
# coding: utf8
import chardet
import tweepy
import csv
import unidecode
import codecs
import datetime
import time
code_dep = "80"
motcle = "inondations"
#optionnel
motcle1 = "Pas-de-Calais"
#Entrer Twitter API informations
consumer_key = 'XXX'
consumer_secret = 'XXX'
access_token = 'XXX'
access_secret = 'XXX'
#connexion à l'API twitter avec les codes ci-dessus
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
#Création d'un fichier de sortie au format .csv
with codecs.open("D:/VIGNERON/test_inondations_80.csv", "w" , encoding='iso8859-1') as csvfile:
file = csv.writer(csvfile, delimiter=';',quotechar='|', quoting=csv.QUOTE_MINIMAL, dialect='excel')
file.writerow(['Nom_utilisateur'] + ['Date'] + ['Evenement'] + ['Source'] + ['Commune'] + ['Insee'] +['Localisation'] + ['Contenu'] + ['media']+ ['X'] + ['Y'] + ['Confiance'])
# Lecture de mes fichier commune, le traite regroupant les commuene sans caractères spéciaux
# Le brut regroupant les communes avec caractères spéciaux
# Les recherche par mot clé passent outre les accents
r = csv.reader(open("D:/VIGNERON/BD_Commune_traite.csv", "r"),delimiter = ';')
r1 = csv.reader(open("D:/VIGNERON/BD_Commune_brut.csv", "r"),delimiter = ';')
# Récuperation des communes brutes et placement dans un liste
com_brut_in_dep = []
for row in r1 :
if row[4] == code_dep :
com_brut_in_dep.append(str(row[0]))
# Récuperation des communes traitées ainsi que le code Insee et intégration dans deux listes
com_traite_in_dep = []
code_insee = []
for row in r :
if row[4] == code_dep :
com_traite_in_dep.append(str(row[0]))
code_insee.append(str(row[1]))
print ("Nombre de commune présente dans le " + str(code_dep) + " : " + str(len(com_traite_in_dep)))
lst_rech = []
tZero=time.time() #Récupération de tZero
print datetime.datetime.today()
t=time.time() -tZero # Temps après tZero
boucle = True
compteur = 0
# Parcour de ma liste de commune
while boucle == True :
global count
global compteur
if compteur < 7 :
for i in range (0,len(com_traite_in_dep)):
if i < len(com_traite_in_dep) :
commune = com_traite_in_dep[i]
test =commune + " " + motcle + " " + "since:\"2017-05-17\""
print test
nb_demande_restante = api.rate_limit_status()['resources']['search']['/search/tweets']['remaining']
print ("Demandes restantes : " + str(nb_demande_restante) )
if nb_demande_restante > 1 :
t = time.time() - tZero
# print t
time.sleep(0.01)
# Recherche de tweets.
# items(200) permet de récupérer les 200 premiers status
for status in tweepy.Cursor(api.search, q = test, count= 60 ).items():
#print commune
nb_demande_restante = api.rate_limit_status()['resources']['search']['/search/tweets']['remaining']
try :
# Récupération des données twitter et lecture du format Json
json_data = status._json
# Récuperation du nom de l'utilisateur
user = status.user.name.encode('ascii', 'ignore')
tweet_id = status.user.id
#print tweet_id
print ("Nom d'utilisateur : " + user)
# Récuperation de la date de publication
date = str(status.created_at)
print("Date de publication : " + str(status.created_at))
# Evenement survenu
evenement = u"Innondation"
print ("Evenement : " + evenement)
# Source de l'information
source = u"twitter"
print ("source : " + source )
# Récuperation du nom de la commune
commune = com_brut_in_dep[i]
print ("Nom de la commune : " + commune)
# Récuparation du code Insee de la commune
insee = code_insee[i]
print ("Code Insee commune : " + insee)
# Information complémentaire
localisation = u"Null"
print ("localisation : " + localisation)
# récuperation du texte tweeter
contenu = status.text
contenu = contenu.replace("\n" , ". ")
contenu = contenu.replace(";" , ".")
contenu = contenu.encode('iso8859-1','ignore')
print ("Tweet text: " + contenu)
url = json_data["entities"]["urls"]
if not url :
url = u"None"
else :
url = url[0]['expanded_url']
media = url.encode('iso8859-1', 'ignore')
print ("Media present : " + str(media))
# récuperation des coordonnées du tweet
coord = json_data["coordinates"]
if status.coordinates != None :
lon = coord["coordinates"][0]
lat = coord["coordinates"][1]
if lat < 51.40 and lat > 41.00 and lon < 10.50 and lon > -5.00 :
lon = lon
lat = lat
else :
lon = u"None"
lat = u"None"
print ("Longitude : " + str(lon))
print ("Latitude : " + str(lat))
confiance = u"Null"
print ("Indice de confiance : " + confiance)
print ("")
# Ajout des informations dans le .csv de sortie
file =open("D:/VIGNERON/test_inondations_80.csv", "a" )
wri = csv.writer( file , csvfile, delimiter = ';' , quotechar = '|' , quoting = csv.QUOTE_MINIMAL )
wri.writerow([user]+[date]+[evenement]+[source]+[commune]+[insee]+[localisation]+[contenu]+[media]+[lon]+[lat])
#fermeture du fichier rempli
#/!\ si le fichier n'est pas fermé il est inutilisable
file.close()
except :
break
else :
i = i
print i
compteur += 1
print compteur
print t
time.sleep (905 - t)
tZero=time.time()
#t = time.time() - tZero
else:
boucle = False
else :
boucle = False
Twitter's rate limit documentation seems to suggest you can apply 900 searches per 15 minute window - though if you're using a cursor to tab through pages, I think each page counts as 1 towards this limit.
To help avoid crashes on hitting rate limits, Tweepy as rate limit handling built into its API - if you change:
api = tweepy.API(auth)
to
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
Tweepy will be able to catch the rate limit errors, and waits the minimum amount of time before trying again.
EDIT: also, you shouldn't try to "get around" the rate limits - they exist for a reason, and trying to exceed them violates Twitter's terms of service.