def remove_previous_checkpoint(model_dir: str, port: Port, start_time: str, current_time: str, new_optimum: bool, base_port: str = None) -> None: """ Remove a previous checkpoint's model- and checkpoint-file. Identification via file_type and encoded start_time: Files with the same start_time as given in the parameters are considered to be the same training :param model_dir: Directory to models for given port (including port name) :param port: Port :param start_time: Main identification for files that belong to the same training within the given directory :param current_time: Current time for identifying older checkpoints :param new_optimum: Specify if new optimum (= model) was found, so previous model gets removed :param base_port: Base port name if checkpoint is made during transfer :return: None """ current_time = as_datetime(current_time) cp_type = "checkpoint-base" if base_port is None else "checkpoint-transfer" model_type = "model-base" if base_port is None else "model-transfer" for file in os.listdir(model_dir): _, ext = os.path.splitext(file) if file.startswith(cp_type) or (new_optimum and file.startswith(model_type)): if ext in [".pt", ".tar"]: # model or checkpoint file _, cp_port_name, cp_start_time, cp_end_time, _ = \ decode_model_file(file) if ext == ".pt" else decode_checkpoint_file(file) cp_end_time = as_datetime(cp_end_time) if cp_port_name == port.name and cp_start_time == start_time and cp_end_time < current_time: os.remove(os.path.join(model_dir, file))
def __init__(self, parent, defn): super(File, self).__init__(parent, 'files/{file_id}', defn) if 'person_info' in defn: person_info, to, frm = process_person_info(parent, defn['person_info'], defn['addresses']) self.person_info = person_info self.addresses = {'to': to, 'from': frm} self.date = as_datetime(self.date)
def __init__(self, parent, defn): super(File, self).__init__(parent, 'files/{file_id}', defn) if 'person_info' in defn: person_info, to, frm = process_person_info(parent, defn['person_info'], defn['addresses']) self.person_info = person_info self.addresses = { 'to': to, 'from': frm } self.date = as_datetime(self.date)
def find_latest_dataset_config_path(dataset_dir: str, training_type: str) -> str: if training_type not in ["base", "transfer"]: raise ValueError(f"Unknown training type '{training_type}'. Not in [base, transfer]") max_time, path = None, None for file in os.listdir(dataset_dir): if file.startswith(f"dataset-config-{training_type}"): _, start_time = decode_dataset_config_file(file) start_time = as_datetime(start_time) if max_time is None or max_time < start_time: max_time = start_time path = file return path if path is None else os.path.join(dataset_dir, path)
def __init__(self, parent, defn): super(Message, self).__init__(parent, 'messages/{message_id}', defn) person_info, to, frm = process_person_info(parent, defn['person_info'], defn['addresses']) self.person_info = person_info self.addresses = {'to': to, 'from': frm} self.date = as_datetime(self.date) if 'files' in defn: self.files = [File(self.parent, f) for f in defn['files']] if 'headers' in defn: self.process_headers(defn['headers'])
def __init__(self, parent, defn): super(Message, self).__init__(parent, 'messages/{message_id}', defn) person_info, to, frm = process_person_info(parent, defn['person_info'], defn['addresses']) self.person_info = person_info self.addresses = { 'to': to, 'from': frm } self.date = as_datetime(self.date) if 'files' in defn: self.files = [File(self.parent, f) for f in defn['files']] if 'headers' in defn: self.process_headers(defn['headers'])
def get_files(self, **params): params = Resource.sanitize_params(params, ['limit', 'offset']) body = self.request_uri('files', params=params) result = [] for obj in body: file_name = obj.get('file_name') occurrences = [] for o in obj.get('occurrences'): occurrences.append({ 'file': File(self.parent, o), 'message': Message(self.parent, o) }) result.append({ 'file_name': obj.get('file_name'), 'latest_date': as_datetime(obj.get('latestDate')), 'occurrences': occurrences }) return result