Best Python code snippet using ATX
save2json.py
Source:save2json.py
1import random2import numpy as np3from datetime import datetime4import os5class ReplayBuffer:6 def __init__(self, capacity):7 self.capacity = capacity8 self.buffer = []9 self.position = 010 11 def push(self, state, action, reward, next_state, done):12 if len(self.buffer) < self.capacity:13 self.buffer.append(None)14 self.buffer[self.position] = (state, action, reward, next_state, done)15 self.position = (self.position + 1) % self.capacity16 17 def sample(self, batch_size):18 batch = random.sample(self.buffer, batch_size)19 state, action, reward, next_state, done = map(np.stack, zip(*batch))20 return state, action, reward, next_state, done21 22 def __len__(self):23 return len(self.buffer)24 25class save2json(object):26 def __init__(self,27 version_id,28 capacity:int,29 model_type='onnx',30 check_input=True,31 check_model_pth=True,32 check_tensorboard_pth=True) -> None:33 super(self,save2json).__init__()34 self.capacity=capacity35 self.model_type=model_type36 self.check_input=check_input37 self.check_model_pth=check_model_pth38 self.check_tensorboard_pth=check_tensorboard_pth39 """40 {41 VERSION:{42 InputData:ReplayBuffer,43 ModelPath:{44 TIME1:ModelPath1,45 TIME2:ModelPath2,46 ...47 }48 TensorboardPath:{49 TIME1:TensorboardPath1,50 TIME2:TensorboardPath2,51 ...52 }53 },54 METAINFO:{55 VERSION:{56 Model Type:Model Type,57 Replay Buffer Size:capacity58 }59 }60 }61 """62 self.js={}63 self.js['metaInfo']={}64 self.version_name=self._get_version_format(version_id)65 self.js['metaInfo'][self.version_name]={66 "Model Type":model_type,67 "Replay Buffer Size":capacity68 }69 if self.version_name not in self.js.keys():70 self.js[self.version_name]={71 "InputData":ReplayBuffer(capacity=capacity),72 "ModelPath":{},73 "TensorboardPath":{}74 }75 def __len__(self,version_id=None):76 version_name=self.version_name if version_id==None else self._get_version_format(version_id)77 return self.js['metaInfo'][version_name]["Replay Buffer Size"]78 def save2json(self):79 pass80 def loadjson(self,path):81 pass82 83 def push(self,inputs):84 for i,input_one in enumerate(inputs):85 check=None86 if i!=1:87 check=False88 self.push_single(input_one,check)89 def push_single(self,input_one,check=None):90 if check==None:91 check=self.check_input92 if check:93 self._check_input_data(input_one)94 self.js[self.version_name]["InputData"].push(*input_one)95 96 def update_model_path(self,model_pth):97 NOW=datetime.now().strftime("%Y_%m_%d_%H_%M")98 self.js[self.version_name]['ModelPath'][NOW]=model_pth99 def update_tensorboard_path(self,tensorboard_pth):100 """101 FORMAT: ("%Y_%m_%d_%H_%M")102 """103 NOW=datetime.now().strftime("%Y_%m_%d_%H_%M")104 self.js[self.version_name]['TensorboardPath'][NOW]=tensorboard_pth105 def get_replay_buffer(self,batch_size,version_id=None):106 version_name=self.version_name if version_id==None else self._get_version_format(version_id)107 108 assert version_name in self.js.keys()109 state, action, reward, next_state, done=self.js[version_name]["InputData"].sample(batch_size)110 return state, action, reward, next_state, done111 def get_model_path(self,version_id=None):112 version_name=self.version_name if version_id==None else self._get_version_format(version_id)113 return self.js[version_name]["ModelPath"]114 def get_tensorboar_path(self,version_id=None):115 version_name=self.version_name if version_id==None else self._get_version_format(version_id)116 return self.js[version_name]["TensorboardPath"]117 def get_latest_model_pth(self,version_id=None):118 version_name=self.version_name if version_id==None else self._get_version_format(version_id)119 Keys=[datetime.strptime("1999-01-11", "%Y_%m_%d_%H_%M") for k in self.js[version_name]["ModelPath"].keys() ]120 Keys=sorted(Keys)121 return self.js[version_name]["ModelPath"][Keys[-1].strftime("%Y_%m_%d_%H_%M")]122 def get_latest_tensorboard_pth(self,version_id=None):123 version_name=self.version_name if version_id==None else self._get_version_format(version_id)124 Keys=[datetime.strptime("1999-01-11", "%Y_%m_%d_%H_%M") for k in self.js[version_name]["TensorboardPath"].keys() ]125 Keys=sorted(Keys)126 return self.js[version_name]["TensorboardPath"][Keys[-1].strftime("%Y_%m_%d_%H_%M")]127 def delete_certain_version(self,version_id=None):128 version_name=self.version_name if version_id==None else self._get_version_format(version_id)129 self.js[version_name]={}130 self.js['metaInfo'][version_name]={}131 132 @staticmethod133 def _get_version_format(version_id):134 version_name=f"version_PPO_{version_id}"135 return version_name136 @staticmethod137 def _check_model_path(model_pth,default_type='onnx'):138 assert model_pth.endswith('.'+default_type)139 assert os.path.isfile(model_pth) and "FileError: model path must be a file"140 assert isinstance(model_pth,str) and 'InputError: model path should be str type'141 @staticmethod142 def _check_tensorboard_path(tensorboard_dir):143 assert os.path.isfile(tensorboard_dir) or os.path.isdir(tensorboard_dir)144 assert isinstance(tensorboard_dir,str) and 'InputError: tensorboard_dir should be str type'145 @staticmethod146 def _check_input_data(input_one:list):147 """148 every input in inputs contains:149 observation: np.ndarray150 action: np.ndarray or int or float151 reward: float152 next_observation: np.ndarray153 info: dict154 network_onnx_path: str155 tensorboard_dir: str156 """157 if isinstance(input_one,(list,tuple)):158 assert len(input_one==5) and """159 every input in inputs contains:160 observation: np.ndarray161 action: np.ndarray or int or float162 reward: float163 next_observation: np.ndarray164 info: dict165 network_onnx_path: str166 tensorboard_dir: str167 """168 assert isinstance(input_one[0],np.ndarray) and 'InputError: observation should be np.ndarray type'169 assert isinstance(input_one[1],(np.ndarray,int,float)) and 'InputError: action should be np.ndarray or int or float type'170 assert isinstance(input_one[2], float) and 'InputError: reward should be float type'171 assert isinstance(input_one[3], np.ndarray) and 'InputError: observation should be np.ndarray type'...
version_specs.py
Source:version_specs.py
...38 self.version_name = version_name39 if version_id is not None:40 self.version_id = version_id41 @property42 def version_name(self):43 """Gets the version_name of this VersionSpecs. # noqa: E50144 :return: The version_name of this VersionSpecs. # noqa: E50145 :rtype: str46 """47 return self._version_name48 @version_name.setter49 def version_name(self, version_name):50 """Sets the version_name of this VersionSpecs.51 :param version_name: The version_name of this VersionSpecs. # noqa: E50152 :type: str53 """54 self._version_name = version_name55 @property56 def version_id(self):57 """Gets the version_id of this VersionSpecs. # noqa: E50158 :return: The version_id of this VersionSpecs. # noqa: E50159 :rtype: str60 """61 return self._version_id62 @version_id.setter63 def version_id(self, version_id):...
get_version_response_data.py
Source:get_version_response_data.py
...58 :type id: str59 """60 self._id = id61 @property62 def version_name(self) -> str:63 """64 Gets the version_name of this GetVersionResponseData.65 :return: The version_name of this GetVersionResponseData.66 :rtype: str67 """68 return self._version_name69 @version_name.setter70 def version_name(self, version_name: str):71 """72 Sets the version_name of this GetVersionResponseData.73 :param version_name: The version_name of this GetVersionResponseData.74 :type version_name: str75 """76 self._version_name = version_name77 @property78 def created_at(self) -> str:79 """80 Gets the created_at of this GetVersionResponseData.81 :return: The created_at of this GetVersionResponseData.82 :rtype: str83 """84 return self._created_at...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!