serJD commited on
Commit
1191a77
1 Parent(s): 4462c16

major simpllification of "send_matrix_and_create_commit"

Browse files
Files changed (1) hide show
  1. tripGenerationFunc.py +22 -73
tripGenerationFunc.py CHANGED
@@ -834,82 +834,31 @@ def computeTrips(
834
  #==================================================================================================
835
  # speckle send
836
 
837
- def send_row_bundle(rows, indices, transport):
838
- bundle_object = Base()
839
- bundle_object.rows = rows
840
- bundle_object.indices = indices
841
- bundle_id = operations.send(base=bundle_object, transports=[transport])
842
- return bundle_id
843
-
844
- def send_matrix(matrix_df, transport, rows_per_chunk):
845
- matrix_object = Base(metaData="Some metadata")
846
- batch_index = 0 # Maintain a separate counter for batch indexing
847
-
848
- # Bundle rows together
849
- rows = []
850
- indices = []
851
- for index, row in matrix_df.iterrows():
852
- rows.append([round(r,4) for r in row.tolist()])
853
- indices.append(str(index))
854
- if len(rows) == rows_per_chunk:
855
- bundle_id = send_row_bundle(rows, indices, transport)
856
- # Set the reference to the bundle in the matrix object using setattr
857
- setattr(matrix_object, f"@batch_{batch_index}", {"referencedId": bundle_id})
858
- rows, indices = [], [] # Reset for the next bundle
859
- batch_index += 1 # Increment the batch index
860
- print( str(rows_per_chunk) +" rows has been sent")
861
-
862
- # Don't forget to send the last bundle if it's not empty
863
- if rows:
864
- bundle_id = send_row_bundle(rows, indices, transport)
865
- setattr(matrix_object, f"@batch_{batch_index}", {"referencedId": bundle_id})
866
-
867
- # Send the matrix object to Speckle
868
- matrix_object_id = operations.send(base=matrix_object, transports=[transport])
869
- return matrix_object_id
870
-
871
-
872
-
873
-
874
-
875
- # Main function to send all matrices and create a commit
876
  def send_matrices_and_create_commit(matrices, client, stream_id, branch_name, commit_message, rows_per_chunk, containerMetadata):
877
  transport = ServerTransport(client=client, stream_id=stream_id)
878
  matrix_ids = {}
879
-
880
- # Send each matrix row by row and store its object ID
881
- for k, df in matrices.items():
882
- matrix_ids[k] = send_matrix(df, transport, rows_per_chunk)
883
- print("object: " + k + " has been sent")
884
-
885
- # Create a container object that will hold references to all the matrix objects
886
  container_object = Base()
887
-
888
  for k, v in containerMetadata.items():
889
  container_object[k] = v
890
-
891
- # Assuming you have a way to reference matrix objects by their IDs in Speckle
892
- for k, obj_id in matrix_ids.items():
893
- print("obj_id", obj_id)
894
- container_object[k] = obj_id
895
-
896
-
897
- # Dynamically add references to the container object
898
- for matrix_name, matrix_id in matrix_ids.items():
899
- # This assigns a reference to the matrix object by its ID
900
- # You might need to adjust this based on how your Speckle server expects to receive references
901
- setattr(container_object, matrix_name, {"referencedId": matrix_id})
902
-
903
-
904
-
905
- # Send the container object
906
- container_id = operations.send(base=container_object, transports=[transport])
907
-
908
-
909
- # Now use the container_id when creating the commit
910
- commit_id = client.commit.create(
911
- stream_id=stream_id,
912
- object_id=container_id, # Use the container's ID here
913
- branch_name=branch_name,
914
- message=commit_message,
915
- )
 
834
  #==================================================================================================
835
  # speckle send
836
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
837
  def send_matrices_and_create_commit(matrices, client, stream_id, branch_name, commit_message, rows_per_chunk, containerMetadata):
838
  transport = ServerTransport(client=client, stream_id=stream_id)
839
  matrix_ids = {}
840
+
 
 
 
 
 
 
841
  container_object = Base()
842
+ # add to container object
843
  for k, v in containerMetadata.items():
844
  container_object[k] = v
845
+
846
+ #initialize the keys()
847
+ for k, df in matrices.items():
848
+ container_object[k] = Base()
849
+
850
+ for k, df in matrices.items():
851
+ matrix_object = Base(metaData="Some metadata")
852
+ # Prepare rows and indices as a single list for each, to be automatically chunked
853
+ rows = [[round(r,4) for r in row.tolist()] for _, row in df.iterrows()]
854
+ indices = [str(index) for index in df.index]
855
+
856
+ container_object[k][f"@({rows_per_chunk})"] = rows
857
+ container_object[k]["indices"] = indices
858
+
859
+ container_objectid = operations.send(base=container_object, transports=[transport])
860
+
861
+ #container_id = operations.send(base=container_object, transports=[transport])
862
+ commit_id = client.commit.create(stream_id=stream_id, object_id=container_objectid, branch_name=branch_name, message=commit_message)
863
+
864
+ return commit_id