Skip to content

Commit

Permalink
replay node
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineBellemare committed Feb 6, 2025
1 parent 1bf2f86 commit 28d9e2a
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/goofi/nodes/inputs/replay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import os
import pandas as pd
import numpy as np
from goofi.data import Data, DataType
from goofi.node import Node
from goofi.params import StringParam, BoolParam


class Replay(Node):
@staticmethod
def config_output_slots():
return {"table_output": DataType.TABLE}

@staticmethod
def config_params():
return {
"Read": {
"filename": StringParam("output.csv"),
"play": BoolParam(False),
"restart": BoolParam(False, trigger=True),
}
}

def setup(self):
self.df = None
self.current_index = 0
self.last_filename = None # Track filename changes
self.load_csv()

def load_csv(self):
filename = self.params["Read"]["filename"].value
if filename != self.last_filename: # Only reload if filename changed
if os.path.exists(filename):
self.df = pd.read_csv(filename)
self.current_index = 0
else:
self.df = None
self.last_filename = filename

def process(self):
filename = self.params["Read"]["filename"].value

# Reload CSV if filename has changed
self.load_csv()

if self.df is None or self.df.empty:
return {"table_output": ({}, {})} # Return empty table instead of None

if not self.params["Read"]["play"].value:
return

# Extract the current row as a dictionary
row_data = self.df.iloc[self.current_index].to_dict()

# Convert each value to the appropriate Data format
table_output = {
key: Data(
DataType.ARRAY if not isinstance(value, str) else DataType.STRING,
np.array([value]) if isinstance(value, (int, float)) else value, # Convert lists, ints, and floats to NumPy arrays
{}
)
for key, value in row_data.items()
}

# Increment index, loop back to the start when reaching the end
self.current_index = (self.current_index + 1) % len(self.df)

return {"table_output": (table_output, {})}

def read_filename_changed(self):
self.load_csv()

def read_restart_changed(self):
self.current_index = 0 # Reset index when restart is triggered

0 comments on commit 28d9e2a

Please sign in to comment.