-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils_spain.py
84 lines (66 loc) · 2.05 KB
/
utils_spain.py
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
"""
Code File for:
- Train-Test division
- Loading dataset
- Making normalized adjacency matrix
"""
import numpy as np
import torch
import csv
import os
import networkx as nx
from scipy.linalg import fractional_matrix_power
from sklearn import preprocessing
def load_data_spain(dataset="Spain"):
"""Load Spain dataset"""
print('Loading {} dataset for 2016 and 2017...'.format(dataset))
#train-test division
train_set=40
test_set=9
# build adjacency
path_for_edge="./Datasets/Spain/graph/edges.txt"
adj=np.zeros((20,20))
f=open(path_for_edge, "r")
f1=f.readlines()
for line in f1:
i=int(line.rstrip().split(',')[0])
j=int(line.rstrip().split(',')[1])
if adj[i][j]==0 or adj[j][i]==0:
adj[i][j]=1
adj[j][i]=1
# normalize adjacency matrix
adj = adj + np.eye(adj.shape[0])
G = nx.from_numpy_matrix(adj)
L=nx.laplacian_matrix(G).toarray()
D=L+adj
frac=fractional_matrix_power(D,-1)
adj=frac @ adj
adj = torch.FloatTensor(adj)
#features and ID
path_for_feature="./Datasets/Spain/features/"
features=[]
AList=[]
for files in sorted(os.listdir(path_for_feature)):
reader = csv.reader(open(path_for_feature+ str(files), "r"), delimiter=",")
x = list(reader)
idx_features = np.array(x)
f=idx_features[1:,2:].astype(float)
f=preprocessing.normalize(f,norm='l2')
f = torch.FloatTensor(f)
features.append(f)
AList.append(adj)
idx=idx_features[1:,0].astype(int)
name=idx_features[1:,1]
print("Region: ",name)
print("ID: ",idx)
#soilmoisture
path_for_SM="./Datasets/Spain/groundtruth_SM/"
SM=[]
for files in sorted(os.listdir(path_for_SM)):
reader = csv.reader(open(path_for_SM+str(files), "r"), delimiter=",")
x = list(reader)
SoilMoisturedata = np.array(x)
sm=SoilMoisturedata[1:,-1].astype(float)
sm = torch.FloatTensor(sm)
SM.append(sm)
return train_set ,test_set ,features, SM, AList