-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodule_Net.py
More file actions
153 lines (120 loc) · 4.91 KB
/
module_Net.py
File metadata and controls
153 lines (120 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 3 13:31:15 2017
@author: isaac
"""
import numpy as np
from sklearn.cluster import MeanShift, estimate_bandwidth
import networkx as nx
"""
Return Labels Of Cluster, Cluster Center
LabelsOfCluster - array 1d int
Cluster Center -- arrat 2d lat,long
"""
def doCluster(coords):
bandwidth = estimate_bandwidth(coords[:,:2], quantile=0.005) #0.01
ms = MeanShift(bandwidth=bandwidth, bin_seeding=True)
ms.fit(coords[:,:2])
labels = ms.labels_
cluster_centers = ms.cluster_centers_
return labels,cluster_centers
#==============================================================================
# #NODOS
# select by THRESHOLD
#==============================================================================
"""
Return Nodes by Frequency of Groups
Params: Threshold
"""
def getNodes(coords,labels,treshold):
nodosByLabel = {}
for idx,et in enumerate(labels):
if et in nodosByLabel.keys():
if not coords[idx][2] in nodosByLabel[et]:
nodosByLabel[et].append(coords[idx][2])
else:
nodosByLabel[et] = [coords[idx][2]]
return nodosByLabel
#==============================================================================
# Uniones entre grupos
#==============================================================================
#La unión entre los grupos viene dada por la secuencia de codeRoute en coords
# OOPT: Filtrado aquellos grupos que no estarán en el TOP - nodosByLabel
"""
Return sequence of arrays 1d[1d.size diff]
"""
def getEdges(coords,labels):
previousRoute = 0
seq = []
seqRoute = []
for idx,ruta in enumerate(coords[:,2]):
if ruta != previousRoute: #comenzamos una nueva ruta
previousRoute = ruta
if len(seqRoute)>1 :
seq.append(seqRoute)
labelGroup = labels[idx]
if labelGroup in nodosByLabel.keys():
seqRoute = [labelGroup]
else:
seqRoute =[] #LINEA VITAL
else:
labelGroup = labels[idx]
if labelGroup in nodosByLabel.keys():
seqRoute.append(labelGroup)
if len(seqRoute)>1 :
seq.append(seqRoute)
return seq
#==============================================================================
# RED
#==============================================================================
def createGraph(nodosByLabel,cluster_centers,seq):
G=nx.DiGraph()
# G=nx.MultiGraph()
for key in nodosByLabel.keys():
lat=float(cluster_centers[key][0])
lng=float(cluster_centers[key][1])
n = G.add_node(key,
idN=float(key),
weight=float(len(nodosByLabel[key])),
lat = lat,
lng = lng
)
#Convertir la sequencia de route sobre en tupla edge o introducirla
for path in seq:
for i in range(0,len(path)-1):
if path[i]!=path[i+1]:
G.add_edge(path[i],path[i+1])
return G
#==============================================================================
#==============================================================================
#==============================================================================
# # # MAIN
#==============================================================================
#==============================================================================
#==============================================================================
def runNetwork(job,options):
coords = np.load("FNodes.npy")
labels, cluster_centers = doCluster(coords)
nodosByLabel = getNodes(coords,labels,4)
###Saving info nodes for QGIS app
with job.fileStore.writeGlobalFileStream() as (fileHandle, outID):
fileHandle.write("lat,long,frec\n")
for idx,key in enumerate(nodosByLabel.keys()):
fileHandle.write("%s,%s,%s,%i\n" % (idx,cluster_centers[key][0],cluster_centers[key][1],len(nodosByLabel[key])))
job.fileStore.exportFile(outID, 'file:///Users/isaac/IdeaProjects/Network_Workflow/qgis_stats_nodes.csv')
seq = getEdges(coords,labels)
G = createGraph(nodosByLabel,cluster_centers,seq)
#==============================================================================
# Save: shaping file
#==============================================================================
# BUG of NX library
mapping = {}
counter = 0
for idx,d in enumerate(G.nodes):
co_od=(G.node[d]["lat"],G.node[d]["lng"])
mapping[counter] = co_od
counter += 1
G=nx.relabel_nodes(G, mapping)
with job.fileStore.writeGlobalFileStream() as (fileHandle, outID):
nx.write_shp(G,fileHandle)
job.fileStore.exportFile(outID, 'file:///Users/isaac/IdeaProjects/Network_Workflow/shape')