[docs]defserialize_config(obj):"""Helper function to make config JSON serializable. Handles special cases like lambda functions, torch modules, and numpy arrays. Parameters ---------- obj : Any The object to serialize Returns ------- Any JSON serializable representation of the object """# Handle NoneifobjisNone:returnNone# Handle lambda and regular functionsifisinstance(obj,(types.LambdaType,types.FunctionType,types.MethodType)):# For lambda functions, try to get the source codetry:ifisinstance(obj,types.LambdaType):source=inspect.getsource(obj).strip()return{"_type":"lambda","source":source}else:return{"_type":"function","name":obj.__name__}except(IOError,TypeError):# Fallback for built-in functions or when source isn't availablereturn{"_type":"function","name":str(obj)}# Handle PyTorch modules and optimizerselifisinstance(obj,(torch.nn.Module,torch.optim.Optimizer)):return{"_type":"torch_class","class_name":obj.__class__.__name__,"module":obj.__class__.__module__}# Handle PyTorch tensorselifisinstance(obj,torch.Tensor):# If it's a single number wrapped in a torch tensor, just return the numberifobj.numel()==1:returnobj.item()elifobj.numel()<1000:return{"_type":"torch_tensor","data":obj.detach().cpu().tolist(),"shape":list(obj.shape)}return{"_type":"torch_tensor","shape":list(obj.shape)}# Handle numpy arrayselifisinstance(obj,(np.ndarray,np.generic)):# If it's a single number wrapped in a numpy array, just return the numberifobj.size==1:returnobj.item()elifobj.size<1000:return{"_type":"numpy_array","data":obj.tolist(),"shape":list(obj.shape)}return{"_type":"numpy_array","shape":list(obj.shape)}# Handle sets and frozensetselifisinstance(obj,(set,frozenset)):return{"_type":"set","data":list(obj)}# Handle custom objects with __dict__elifhasattr(obj,'__dict__'):return{"_type":"custom_class","class_name":obj.__class__.__name__,"module":obj.__class__.__module__}# Handle basic types that are JSON serializableelifisinstance(obj,(str,int,float,bool)):returnobj# Handle any other types by converting to stringreturn{"_type":"unknown","repr":str(obj)}
[docs]defsanitize_config(config_dict):"""Recursively sanitize config dictionary for JSON serialization. Handles nested structures and special cases. Parameters ---------- config_dict : dict Configuration dictionary to sanitize Returns ------- dict Sanitized configuration dictionary that is JSON serializable """ifnotisinstance(config_dict,dict):returnserialize_config(config_dict)clean_dict={}forkey,valueinconfig_dict.items():# Skip private attributes and callable objects stored as attributesifisinstance(key,str)and(key.startswith('_')orcallable(value)):continue# Handle nested dictionariesifisinstance(value,dict):clean_dict[key]=sanitize_config(value)# Handle lists and tupleselifisinstance(value,(list,tuple)):clean_dict[key]=[sanitize_config(v)forvinvalue]# Handle all other typeselse:clean_dict[key]=serialize_config(value)returnclean_dict