An error occurred while loading the file. Please try again.
-
Le Roux Erwan authoreddb5692b5
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import os.path as op
from typing import List
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
from spatio_temporal_dataset.slicer.abstract_slicer import AbstractSlicer, df_sliced
from spatio_temporal_dataset.slicer.spatial_slicer import SpatialSlicer
from spatio_temporal_dataset.slicer.spatio_temporal_slicer import SpatioTemporalSlicer
from spatio_temporal_dataset.slicer.split import s_split_from_df, TEST_SPLIT_STR, \
TRAIN_SPLIT_STR, ind_train_from_s_split, Split
from spatio_temporal_dataset.slicer.temporal_slicer import TemporalSlicer
class AbstractCoordinates(object):
# Spatial columns
COORDINATE_X = 'coord_x'
COORDINATE_Y = 'coord_y'
COORDINATE_Z = 'coord_z'
COORDINATE_SPATIAL_NAMES = [COORDINATE_X, COORDINATE_Y, COORDINATE_Z]
SPATIAL_SPLIT = 'spatial_split'
# Temporal columns
COORDINATE_T = 'coord_t'
TEMPORAL_SPLIT = 'temporal_split'
# Coordinates columns
COORDINATES_NAMES = COORDINATE_SPATIAL_NAMES + [COORDINATE_T]
def __init__(self, df_coord: pd.DataFrame, slicer_class: type, s_split_spatial: pd.Series = None, s_split_temporal: pd.Series = None):
self.df_all_coordinates = df_coord # type: pd.DataFrame
self.s_split_spatial = s_split_spatial # type: pd.Series
self.s_split_temporal = s_split_temporal # type: pd.Series
self.slicer = slicer_class(ind_train_spatial=self.ind_train_spatial,
ind_train_temporal=self.ind_train_temporal) # type: AbstractSlicer
assert isinstance(self.slicer, AbstractSlicer)
# ClassMethod constructor
@classmethod
def from_df_and_slicer(cls, df: pd.DataFrame, slicer_class: type, train_split_ratio: float = None):
"""
train_split_ratio is shared between the spatial part of the data, and the temporal part
"""
# All the index should be unique
assert len(set(df.index)) == len(df)
# Create a spatial split
if slicer_class in [SpatialSlicer, SpatioTemporalSlicer]:
s_split_spatial = s_split_from_df(df, cls.COORDINATE_X, cls.SPATIAL_SPLIT, train_split_ratio, concat=False)
else:
s_split_spatial = None
# Create a temporal split
if slicer_class in [TemporalSlicer, SpatioTemporalSlicer]:
s_split_temporal = s_split_from_df(df, cls.COORDINATE_T, cls.TEMPORAL_SPLIT, train_split_ratio, concat=True)
else:
s_split_temporal = None
return cls(df_coord=df, slicer_class=slicer_class,
s_split_spatial=s_split_spatial, s_split_temporal=s_split_temporal)
@classmethod
def from_csv(cls, csv_path: str = None):
assert csv_path is not None
assert op.exists(csv_path)
df = pd.read_csv(csv_path)
# Index correspond to the first column
index_column_name = df.columns[0]
assert index_column_name not in cls.COORDINATE_SPATIAL_NAMES
df.set_index(index_column_name, inplace=True)
return cls.from_df(df)
@classmethod
def from_nb_points(cls, nb_points: int, train_split_ratio: float = None, **kwargs):
# Call the default class method from csv
coordinates = cls.from_csv() # type: AbstractCoordinates
# Check that nb_points asked is not superior to the number of coordinates
nb_coordinates = len(coordinates)
if nb_points > nb_coordinates:
raise Exception('Nb coordinates in csv: {} < Nb points desired: {}'.format(nb_coordinates, nb_points))
# Sample randomly nb_points coordinates
df_sample = pd.DataFrame.sample(coordinates.df_merged, n=nb_points)
return cls.from_df(df=df_sample, train_split_ratio=train_split_ratio)
@property
def index(self):
return self.df_all_coordinates.index
@property
def df_merged(self) -> pd.DataFrame:
# Merged DataFrame of df_coord and s_split
return self.df_all_coordinates if self.s_split_spatial is None else self.df_all_coordinates.join(
self.s_split_spatial)
# Split
def df_coordinates(self, split: Split = Split.all) -> pd.DataFrame:
return df_sliced(df=self.df_all_coordinates, split=split, slicer=self.slicer)
def coordinates_values(self, split: Split = Split.all) -> np.ndarray:
return self.df_coordinates(split).values
def coordinates_index(self, split: Split = Split.all) -> pd.Index:
return self.df_coordinates(split).index
@property
def ind_train_spatial(self) -> pd.Series:
return ind_train_from_s_split(s_split=self.s_split_spatial)
@property
def ind_train_temporal(self) -> pd.Series:
return ind_train_from_s_split(s_split=self.s_split_temporal)
# Columns
@property
def coordinates_names(self) -> List[str]:
return self.coordinates_spatial_names + self.coordinates_temporal_names
@property
def nb_coordinates(self) -> int:
return len(self.coordinates_names)
@property
def coordinates_spatial_names(self) -> List[str]:
return [name for name in self.COORDINATE_SPATIAL_NAMES if name in self.df_all_coordinates.columns]
@property
def nb_coordinates_spatial(self) -> int:
return len(self.coordinates_spatial_names)
@property
def coordinates_temporal_names(self) -> List[str]:
return [self.COORDINATE_T] if self.COORDINATE_T in self.df_all_coordinates else []
@property
def nb_coordinates_temporal(self) -> int:
return len(self.coordinates_temporal_names)
# Visualization
@property
def x_coordinates(self) -> np.ndarray:
return self.df_all_coordinates[self.COORDINATE_X].values.copy()
@property
def y_coordinates(self) -> np.ndarray:
return self.df_all_coordinates[self.COORDINATE_Y].values.copy()
@property
def z_coordinates(self) -> np.ndarray:
return self.df_all_coordinates[self.COORDINATE_Z].values.copy()
@property
def t_coordinates(self):
return self.df_all_coordinates[self.COORDINATE_T].values.copy()
def visualize(self):
if self.nb_coordinates_spatial == 1:
self.visualization_1D()
elif self.nb_coordinates_spatial == 2:
self.visualization_2D()
else:
self.visualization_3D()
def visualization_1D(self):
assert self.nb_coordinates_spatial >= 1
x = self.x_coordinates
y = np.zeros(len(x))
plt.scatter(x, y)
plt.show()
def visualization_2D(self):
assert self.nb_coordinates_spatial >= 2
plt.scatter(self.x_coordinates, self.y_coordinates)
plt.show()
def visualization_3D(self):
assert self.nb_coordinates_spatial == 3
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d') # type: Axes3D
ax.scatter(self.x_coordinates, self.y_coordinates, self.z_coordinates, marker='^')
plt.show()
# Magic Methods
def __len__(self):
return len(self.df_all_coordinates)
def __mul__(self, other: float):
self.df_all_coordinates *= other
return self
def __rmul__(self, other):
return self * other