<jupyter_start><jupyter_text>Importando o que importa (haha)<jupyter_code>from freeSpace import *
from collections import defaultdict
import numpy as np
import math
import pandas as pd
from geopy.distance import vincenty
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
from geopy.distance import great_circle
from sklearn.neighbors import KNeighborsRegressor
import matplotlib.pyplot as plt

from scipy.interpolate import griddata<jupyter_output><empty_output><jupyter_text>Pegando os Valores de Treinamento<jupyter_code>#Erbs
csvErbs = pd.read_csv("erbs.csv")
erbsLidas = csvErbs[['lat','lon']].values
erbs_posicao = dict()
for i in range (1,len(erbsLidas)+1):
    erbs_posicao[i] = (erbsLidas[i-1])

#Medicoes, dados de treinamento
csvMed = pd.read_csv("medicoes.csv")
medidas_posicao = csvMed[['lat','lon']].values # Valores em Tupla
medidas_potencia = csvMed[["RSSI_1","RSSI_2","RSSI_3","RSSI_4","RSSI_5","RSSI_6"]].values<jupyter_output><empty_output><jupyter_text>Interpolacao e criacao de pontos do grid<jupyter_code>#ver link https://docs.scipy.org/doc/scipy/reference/interpolate.html
#ver link https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.griddata.html
resolucao = 40 #valor referente a resolucao do grid. OBS: nao tem relacao com metros
#o grid so aceita valores positivos para os pontos
#intervalos do grid
lat_in = 8.08
lat_fim = 8.0605
lon_in = 34.91
lon_fim = 34.887


desloc_lat = (lat_in - lat_fim)/resolucao #valor correspondente ao deslocamento em latitude das celulas do grid, positivo
desloc_lon = (lon_in - lon_fim)/resolucao #valor correspondente ao deslocamento em longitude das celulas do grid

#construcao do grid
grid_lat, grid_lon = np.mgrid[lat_fim:lat_in:desloc_lat, lon_fim:lon_in:desloc_lon] 
np.shape(grid_lat)
#grid_lon

#pegando pontos conhecidos para regressao, uma para cada erb
pontos_teste = -medidas_posicao #o grid so aceita coordenadas positivas
medidas_ref_erb1 = medidas_potencia[:,0] #medidas de ref para ajuste do modelo de regressao
medidas_ref_erb2 = medidas_potencia[:,1]
medidas_ref_erb3 = medidas_potencia[:,2]
medidas_ref_erb4 = medidas_potencia[:,3]
medidas_ref_erb5 = medidas_potencia[:,4]
medidas_ref_erb6 = medidas_potencia[:,5]

#grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest')
#interpolacao, considerada de foma independente para cada erb
grid_z1 = griddata(pontos_teste, medidas_ref_erb1, (grid_lat, grid_lon), method='linear')
grid_z2 = griddata(pontos_teste, medidas_ref_erb2, (grid_lat, grid_lon), method='linear')
grid_z3 = griddata(pontos_teste, medidas_ref_erb3, (grid_lat, grid_lon), method='linear')
grid_z4 = griddata(pontos_teste, medidas_ref_erb4, (grid_lat, grid_lon), method='linear')
grid_z5 = griddata(pontos_teste, medidas_ref_erb5, (grid_lat, grid_lon), method='linear')
grid_z6 = griddata(pontos_teste, medidas_ref_erb6, (grid_lat, grid_lon), method='linear')
#np.shape(medidas_teste)
np.shape(grid_z1)
#grid_z1[0:10][0:10]
#grid_z1
(grid_lat[9][26], grid_lon[9][26])
not(math.isnan(grid_z1[0][0]))

#adicionar dados validos obtidos da interpolacao para a base de dados do knn, criando um novo dataframe

#criar lista para receber novas coordenadas
coord_interpol = []
coord_interpol

for i in range(resolucao):
    for j in range(resolucao):
        if not(math.isnan(grid_z1[i][j])) and not(math.isnan(grid_z2[i][j])) and not(math.isnan(grid_z3[i][j])) and not(math.isnan(grid_z4[i][j])) and not(math.isnan(grid_z5[i][j])) and not(math.isnan(grid_z6[i][j])):  
            coord_interpol.append([-grid_lat[i][j] ,-grid_lon[i][j],grid_z1[i][j], grid_z2[i][j], grid_z3[i][j], grid_z4[i][j], grid_z5[i][j], grid_z6[i][j]])
        


#np.shape(coord_interpol)
#type(coord_interpol)


#criar dataframe e concatenar com os dados da base do knn
dados_iterpol = pd.DataFrame(coord_interpol,columns=['lat','lon','RSSI_1','RSSI_2','RSSI_3','RSSI_4','RSSI_5','RSSI_6'])
csvMed = pd.concat([csvMed, dados_iterpol])
np.shape(coord_interpol)
csvMed<jupyter_output><empty_output><jupyter_text>Gera Modelo<jupyter_code>#medidas_potencia.values
medidas_posicao_lat = csvMed['lat'].values
medidas_posicao_lon = csvMed['lon'].values

medidas_potencia = csvMed[["RSSI_1","RSSI_2","RSSI_3","RSSI_4","RSSI_5","RSSI_6"]].values




#modela latitude e longitude separadamente
neigh_lat = KNeighborsRegressor(n_neighbors=10, weights = 'distance')
neigh_lat.fit(medidas_potencia,medidas_posicao_lat )

neigh_lon = KNeighborsRegressor(n_neighbors=10, weights = 'distance')
neigh_lon.fit(medidas_potencia,medidas_posicao_lon )


medidas_potencia[0:5]<jupyter_output><empty_output><jupyter_text>Pega os Valores de Teste<jupyter_code>csvMedTest = pd.read_csv("testLoc.csv")
medidas_posicao_teste = csvMedTest[['lat','lon']].values # Valores em Tupla
medidas_potencia_teste = csvMedTest[["RSSI_1","RSSI_2","RSSI_3","RSSI_4","RSSI_5","RSSI_6"]].values    

np.shape(medidas_posicao_teste)<jupyter_output><empty_output><jupyter_text>Faz a Predição<jupyter_code>predicao_lat = neigh_lat.predict(medidas_potencia_teste)
predicao_lon = neigh_lon.predict(medidas_potencia_teste)<jupyter_output><empty_output><jupyter_text>Calcula o Erro<jupyter_code>vet_err_lat = []
vet_err_lon = []
for i in range(0,200):
    vet_err_lat.append(predicao_lat[i] - medidas_posicao_teste[i][0])
    vet_err_lon.append(predicao_lon[i] - medidas_posicao_teste[i][1])

err_geral = []
for i in range(len(predicao_lat)):
    err_geral.append(vincenty((predicao_lat[i],predicao_lon[i]), (medidas_posicao_teste[i][0], medidas_posicao_teste[i][1])).kilometers)


print("MEDIA LAT = " + str(np.mean(vet_err_lat)))
print("MEDIA LON = " + str(np.mean(vet_err_lon)))
print("MEDIA KM = " + str(np.mean(err_geral)))
print("STD = " + str(np.std(err_geral)))

plt.plot(range(200),err_geral, range(200),[np.mean(err_geral)for i in range(200)])
plt.show()
plt.clf()
bins = np.linspace(-0.01, 1, 100)
plt.hist(err_geral,bins)
plt.show()
X = medidas_posicao_lat
Y = medidas_posicao_lon
rssi1 = csvMed["RSSI_1"].values
Z = rssi1
plt.clf()
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(X,Y,Z)
plt.show()
plt.clf()
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X,Y,Z)
plt.show()
plt.clf()
fig = plt.figure(figsize=(20,5))
axes = fig.add_axes([0.1,0.1,1,1])
plt.plot(X,Z,X,)
plt.show()
plt.clf()
fig = plt.figure(figsize=(20,5))
axes = fig.add_axes([0.1,0.1,1,1])
plt.plot(Y,Z)
plt.show()
plt.clf()
fig = plt.figure(figsize=(20,10))
ax = fig.add_subplot(111, projection='3d')

surf = ax.plot_trisurf(X, Y, Z, cmap=cm.jet, linewidth=0)
fig.colorbar(surf)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14275081869604048, 'fraction_numerical': 0.025900565644537063, 'mean_word_length': 7.692108667529108, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '147631', 'license': 'MIT', 'n_tokens_mistral': 2947, 'n_tokens_neox': 2862, 'n_words': 471, 'path': 'Projeto/testes_interpolacao.ipynb', 'repo_name': 'CBurgardt/comMov-project', 'size': '6718'}