Skip to content

Commit 009eff2

Browse files
authored
Merge pull request #180 from GREENRAT-K405/dev
fix: infinite loop problem during the running and debugging of concore study
2 parents 256f5cf + 152359f commit 009eff2

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

concore.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
import re
66
import zmq # Added for ZeroMQ
7+
import numpy as np # Added for numpy type conversion
78

89
# if windows, create script to kill this process
910
# because batch files don't provide easy way to know pid of last command
@@ -100,6 +101,24 @@ def terminate_zmq():
100101
print(f"Error while terminating ZMQ port {port.address}: {e}")
101102
# --- ZeroMQ Integration End ---
102103

104+
105+
# NumPy Type Conversion Helper
106+
def convert_numpy_to_python(obj):
107+
#Recursively convert numpy types to native Python types.
108+
#This is necessary because literal_eval cannot parse numpy representations
109+
#like np.float64(1.0), but can parse native Python types like 1.0.
110+
if isinstance(obj, np.generic):
111+
# Convert numpy scalar types to Python native types
112+
return obj.item()
113+
elif isinstance(obj, list):
114+
return [convert_numpy_to_python(item) for item in obj]
115+
elif isinstance(obj, tuple):
116+
return tuple(convert_numpy_to_python(item) for item in obj)
117+
elif isinstance(obj, dict):
118+
return {key: convert_numpy_to_python(value) for key, value in obj.items()}
119+
else:
120+
return obj
121+
103122
# ===================================================================
104123
# File & Parameter Handling
105124
# ===================================================================
@@ -229,8 +248,10 @@ def read(port_identifier, name, initstr_val):
229248
ins = infile.read()
230249
except FileNotFoundError:
231250
ins = str(initstr_val)
251+
s += ins # Update s to break unchanged() loop
232252
except Exception as e:
233253
print(f"Error reading {file_path}: {e}. Using default value.")
254+
s += str(initstr_val) # Update s to break unchanged() loop
234255
return default_return_val
235256

236257
# Retry logic if file is empty
@@ -248,6 +269,7 @@ def read(port_identifier, name, initstr_val):
248269

249270
if len(ins) == 0:
250271
print(f"Max retries reached for {file_path}, using default value.")
272+
s += str(initstr_val) # Update s to break unchanged() loop
251273
return default_return_val
252274

253275
s += ins
@@ -304,7 +326,9 @@ def write(port_identifier, name, val, delta=0):
304326
try:
305327
with open(file_path, "w") as outfile:
306328
if isinstance(val, list):
307-
data_to_write = [simtime + delta] + val
329+
# Convert numpy types to native Python types
330+
val_converted = convert_numpy_to_python(val)
331+
data_to_write = [simtime + delta] + val_converted
308332
outfile.write(str(data_to_write))
309333
simtime += delta
310334
else:

0 commit comments

Comments
 (0)