nastasiasnk commited on
Commit
166b945
1 Parent(s): 2a4109d

Update imports_utils.py

Browse files
Files changed (1) hide show
  1. imports_utils.py +107 -30
imports_utils.py CHANGED
@@ -172,8 +172,114 @@ def fetchSubdomainMapper (livabilityAttributePages):
172
  return attribute_mapper
173
 
174
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
 
176
-
 
177
  def fetchDistanceMatrices (streamObj):
178
 
179
  matrices = {}
@@ -227,35 +333,6 @@ def fetchDistanceMatrices (streamObj):
227
 
228
  """
229
 
230
- def fetchDistanceMatrices (stream_distance_matrices):
231
-
232
- # navigate to list with speckle objects of interest
233
- distance_matrices = {}
234
-
235
- destUUID = matrixObj.__getitem__('@destinationUUID')
236
- oriUUID = matrixObj.__getitem__('@originUUID')
237
- chunks = matrixObj.__getitem__('@chunks')
238
-
239
- for distM in stream_distance_matrices["@Data"]['@{0}']:
240
- for kk in distM.__dict__.keys():
241
- try:
242
- if kk.split("+")[1].startswith("distance_matrix"):
243
- distance_matrix_dict = json.loads(distM[kk])
244
- origin_ids = distance_matrix_dict["origin_uuid"]
245
- destination_ids = distance_matrix_dict["destination_uuid"]
246
- distance_matrix = distance_matrix_dict["matrix"]
247
-
248
- # Convert the distance matrix to a DataFrame
249
- df_distances = pd.DataFrame(distance_matrix, index=origin_ids, columns=destination_ids)
250
-
251
- # i want to add the index & colum names to dist_m_csv
252
- #distance_matrices[kk] = dist_m_csv[kk]
253
- distance_matrices[kk] = df_distances
254
- except:
255
- pass
256
-
257
- return distance_matrices
258
- """
259
 
260
 
261
 
 
172
  return attribute_mapper
173
 
174
 
175
+ # --------------------------------------------------------------------------------------------- #
176
+
177
+
178
+ def getDataFromSpeckle(
179
+ speckleToken,
180
+ streamID,
181
+ matrixBranchName,
182
+ landuseBranchName,
183
+ matrixComitID="",
184
+ landuseComitID="",
185
+ pathToData = ["@Data", "@{0}"]
186
+ uuidColumn = "uuid",
187
+ landuseColumns="lu+",
188
+ mergeAssetWithNonAssetLanduse = True
189
+
190
+ ):
191
+
192
+
193
+ if landuseBranchName:
194
+ streamLanduses = speckle_utils.getSpeckleStream(streamId,luBranchName,CLIENT, luCommitId)
195
+ streamData = streamLanduses["@Data"]["@{0}"]
196
+
197
+ dfLanduses = speckle_utils.get_dataframe(streamData, return_original_df=False)
198
+ dfLanduses = dfLanduses.set_index("uuid", drop=False) # variable, uuid as default
199
+
200
+ if type(landuseColumns) == type("s"):
201
+ # extract landuse columns with "landuseColumns"
202
+ landuse_columns = [] # provided by user? or prefix
203
+ for name in df_lu.columns:
204
+ if name.startswith(landuseColumns):
205
+ landuse_columns.append(name)
206
+
207
+ elif type(landuseColumns) == type([]):
208
+ #assmuming the user provided a lsit of columns
209
+ landuse_columns = landuseColumns
210
+
211
+ dfLanduses_filtered = df_lu[landuse_columns]
212
+ dfLanduses_filtered.columns = [col.replace('lu+', '') for col in df_lu_filtered.columns]
213
+
214
+ """
215
+
216
+ if mergeAssetWithNonAssetLanduse:
217
+ df_lu_filtered.columns = [col.replace('ASSETS+', '') for col in df_lu_filtered.columns]
218
+
219
+ df_lu_filtered = df_lu_filtered.replace([np.inf, -np.inf], 10000).fillna(0)
220
+ df_lu_filtered = df_lu_filtered.apply(pd.to_numeric, errors='coerce')
221
+ df_lu_filtered = df_lu_filtered.astype(int)
222
+ df_lu_filtered = df_lu_filtered.T.groupby(level=0).sum().T
223
+ """
224
+
225
+
226
+
227
+ # -------------------------- stream matrices ------------------------------ #
228
+
229
+
230
+ if matrixBranchName:
231
+ streamObj = speckle_utils.getSpeckleStream(streamId,dmBranchName,CLIENT, dmCommitId)
232
+
233
+ matrices = {}
234
+ isDict = False
235
+ try:
236
+ data_part = streamObj["@Data"]["@{0}"]
237
+ for matrix in data_part:
238
+ # Find the matrix name
239
+ matrix_name = next((attr for attr in dir(matrix) if "matrix" in attr), None)
240
+
241
+ if not matrix_name:
242
+ continue
243
+
244
+ matrix_data = getattr(matrix, matrix_name)
245
+ originUUID = matrix_data["@originUUID"]
246
+ destinationUUID = matrix_data["@destinationUUID"]
247
+
248
+ processed_rows = []
249
+ for chunk in matrix_data["@chunks"]:
250
+ for row in chunk["@rows"]:
251
+ processed_rows.append(row["@row"])
252
+
253
+ matrix_array = np.array(processed_rows)
254
+ matrix_df = pd.DataFrame(matrix_array, index=originUUID, columns=destinationUUID)
255
+ matrices[matrix_name] = matrix_df
256
+ except KeyError:
257
+ data_part = streamObj["@Data"].__dict__
258
+ print(data_part.keys())
259
+
260
+ for k, v in data_part.items():
261
+ if "matrix" in k:
262
+ matrix_name = k
263
+ matrix_data = v
264
+ originUUID = matrix_data["@originUUID"]
265
+ destinationUUID = matrix_data["@destinationUUID"]
266
+
267
+ processed_rows = []
268
+ for chunk in matrix_data["@chunks"]:
269
+ for row in chunk["@rows"]:
270
+ processed_rows.append(row["@row"])
271
+
272
+ matrix_array = np.array(processed_rows)
273
+ matrix_df = pd.DataFrame(matrix_array, index=originUUID, columns=destinationUUID)
274
+ matrices[matrix_name] = matrix_df
275
+
276
+
277
+
278
+ return dfLanduses_filtered, matrices
279
+
280
 
281
+
282
+ """
283
  def fetchDistanceMatrices (streamObj):
284
 
285
  matrices = {}
 
333
 
334
  """
335
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
336
 
337
 
338