[docs]classDiskDatasetWriter(Writer):def__init__(self,path:Union[str,Path],capabilities:Optional[ModelCapabilities]=None,# unused, but matches base signatureappend:Optional[bool]=False,# if True, open zip in append mode):super().__init__(filename=path,capabilities=capabilities,append=append)mode:Literal["w","a"]="a"ifappendelse"w"self.zip_file=zipfile.ZipFile(path,mode)self.index=0
[docs]defwrite(self,systems:List[System],predictions:Dict[str,TensorMap]):""" Write a single (system, predictions) into the zip under a new folder "<index>/". """iflen(systems)==1:# Avoid reindexing samplessplit_predictions=[predictions]else:split_predictions=_split_tensormaps(systems,predictions,istart_system=self.index)forsystem,predsinzip(systems,split_predictions):# systemwithself.zip_file.open(f"{self.index}/system.mta","w")asf:mta.save(f,system.to("cpu").to(torch.float64))# each targetfortarget_name,tensor_mapinpreds.items():withself.zip_file.open(f"{self.index}/{target_name}.mts","w")asf:buf=tensor_map.to("cpu").to(torch.float64)# metatensor.torch.save_buffer returns a torch.Tensor bufferbuffer=buf.save_buffer()np.save(f,buffer.numpy())self.index+=1