11import datetime
2- import os
32import threading
43import json
54from typing import Any , List , Dict , Optional , Tuple
65import numpy as np
76import matplotlib .pyplot as plt
7+ from pathlib import Path
88from paho .mqtt .client import Client as MQTTClient , MQTTMessage
99from data .comm .mqtt import (shutdown ,start_mqtt , publish_to_mqtt )
1010from functions .util import (convert_numpy_to_list , _convert_list_to_dict_or_array )
@@ -169,7 +169,7 @@ def save_model_parameters(config: Dict[str,Any], timestamp: str,
169169 """
170170 if model_parameters is not None :
171171 # Ensure output directory exists
172- os . makedirs (MODEL_DIR , exist_ok = True )
172+ Path (MODEL_DIR ). mkdir ( parents = True , exist_ok = True )
173173
174174 # Thread-safe file locks
175175 file_locks = {topic : threading .Lock () for topic in config ["model_update" ]["TopicsToSubscribe" ]}
@@ -178,7 +178,7 @@ def save_model_parameters(config: Dict[str,Any], timestamp: str,
178178 "timestamp" : timestamp ,
179179 "parameters" : convert_numpy_to_list (model_parameters )
180180 }
181- file_path = os . path . join (MODEL_DIR , MODEL_PARS_NAME )
181+ file_path = Path (MODEL_DIR ) / MODEL_PARS_NAME
182182 with file_locks [config ["model_update" ]["TopicsToSubscribe" ][0 ]]:
183183 with open (file_path , "a" , encoding = "utf-8" ) as f :
184184 f .write (json .dumps (record ) + "\n " )
@@ -197,14 +197,14 @@ def load_model_parameters() -> Optional[Tuple[str, Dict[str,Any]]]:
197197
198198 """
199199 try :
200- path = os . path . join (MODEL_DIR , MODEL_PARS_NAME )
201- if not os . path .exists (path ):
200+ path = Path (MODEL_DIR ) / MODEL_PARS_NAME
201+ if not path .exists ():
202202 print (f"File not found: { path } . Proceed with standard parameters." )
203203 model_parameters = MODEL_PARAMETERS
204204 timestamp = datetime .datetime .now (datetime .timezone .utc ).isoformat ()
205205 return timestamp , model_parameters
206206 else :
207- with open (path , 'r' ) as json_file :
207+ with path . open ('r' ) as json_file :
208208 data = json .loads (json_file .readlines ()[- 1 ])
209209 timestamp = data ['timestamp' ]
210210 model_parameters = data ['parameters' ]
@@ -268,7 +268,6 @@ def live_model_update_with_remote_clustering(config: Dict[str,Any],
268268 fig_axes = model_update_plots ([1 ,1 ], model_parameters ,
269269 params ['pars_to_update' ], omega_model ,
270270 fig_axes )
271-
272271 except KeyboardInterrupt :
273272 print ("Keyboard interrupt of live model updating\n " )
274273 except Exception as e :
0 commit comments