Replies: 1 comment
-
In case anyone else has wanted to do this, I think a solution for now is to save your recording. The recording is just a JSON object that you can parse. Depending on the structure of your graph, you're likely looking for an "event" with "type" "done". Here are some helper functions you can use to manipulate the recording JSON easily. I hope this helps! import json
def organize_recording_events_by_event_types(recording_json):
"""
Organizes events from a Rivet recording JSON object into categories based on event types.
This function takes a Rivet recording JSON object and iterates through its events. It then
organizes these events into lists based on their event types and returns a dictionary
where each key is an event type and its value is a list of events of that type.
Parameters:
recording_json (dict): A JSON object representing a Rivet recording. The structure is expected to have
events stored under the keys ["recording"]["events"].
Returns:
dict: A dictionary categorizing events by their types. Each key is an event type and the corresponding
value is a list of events of that type.
Raises:
KeyError: If the provided JSON object does not have the expected structure.
Example Usage:
--------------
sample_recording_file_path = "./sample.rivet-recording"
sample_recording = load_json_file(file_path=sample_recording_file_path)
organized_events = organize_recording_events_by_event_types(sample_recording)
The 'organized_events' dictionary will have a structure like:
{
"eventType1": [event1, event2, ...],
"eventType2": [event3, event4, ...],
...
}
"""
organized_events = {}
for event in recording_json["recording"]["events"]:
event_type = event["type"]
if event_type not in organized_events:
organized_events[event_type] = []
organized_events[event_type].append(event)
return organized_events
def summarize_recording_event_types(recording_json, debug_mode=False):
"""
Summarizes the types of events in a Rivet recording JSON object.
This function iterates through all the events in the provided recording JSON object,
counts how many times each distinct event type occurs, and returns these counts in a dictionary.
Parameters:
recording_json (dict): A JSON object representing a Rivet recording. It is expected to have
a structure where recording events are stored under the keys
["recording"]["events"].
debug_mode (bool, optional): If set to True, the function prints additional debugging information,
including a detailed count of each event type. Defaults to False.
Returns:
dict: A dictionary where each key is a unique event type from the recording, and each value is the
count of how many times that event type occurs in the recording.
Raises:
KeyError: If the provided JSON object does not have the expected structure (missing ["recording"]["events"] keys).
Example Usage:
--------------
sample_recording_file_path = "./sample.rivet-recording"
sample_recording = load_json_file(file_path=sample_recording_file_path)
event_counts = summarize_recording_event_types(sample_recording)
The returned 'event_counts' dictionary will have a structure like:
{
"eventType1": count1,
"eventType2": count2,
...
}
"""
event_types = {}
for event in recording_json["recording"]["events"]:
# Debug print each event type
if debug_mode:
print(event["type"])
# Counting occurrences of each event type
if event["type"] in event_types:
event_types[event["type"]] += 1
else:
event_types[event["type"]] = 1 # Initialize with 1 (as it's the first occurrence)
# Debug print the final counts
if debug_mode:
print("="*80)
print("Counts:")
print(json.dumps(event_types, indent=4))
return event_types |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is there any option to store the text of a node in a text file?
Beta Was this translation helpful? Give feedback.
All reactions