Source code for idmtools_platform_comps.utils.spatial_output
"""idmtools utility.Copyright 2021, Bill & Melinda Gates Foundation. All rights reserved."""importstructimportnumpyasnp
[docs]classSpatialOutput:""" SpatialOutput class is used to parse data from binary file (.bin). """
[docs]def__init__(self):""" Initialize an instance of SpatialOutput. This constructor does not take any parameters other than the implicit 'self'. """self.n_nodes=0self.n_tstep=0self.nodeids=[]self.data=Noneself.start=0self.interval=1
[docs]@classmethoddeffrom_bytes(cls,bytes,filtered=False):""" Convert from bytes to class object. Args: bytes: bytes filtered: flag for applying filter """# The header size changes if the file is a filtered oneheadersize=16iffilteredelse8# Create the classso=cls()# Retrive the number of nodes and number of timestepsso.n_nodes,=struct.unpack('i',bytes[0:4])so.n_tstep,=struct.unpack('i',bytes[4:8])# If filtered, retrieve the start and intervaliffiltered:start,=struct.unpack('f',bytes[8:12])interval,=struct.unpack('f',bytes[12:16])so.start=int(start)so.interval=int(interval)# Get the nodeidsso.nodeids=struct.unpack(str(so.n_nodes)+'I',bytes[headersize:headersize+so.n_nodes*4])so.nodeids=np.asarray(so.nodeids)# Retrieve the dataso.data=struct.unpack(str(so.n_nodes*so.n_tstep)+'f',bytes[headersize+so.n_nodes*4:headersize+so.n_nodes*4+so.n_nodes*so.n_tstep*4])so.data=np.asarray(so.data)so.data=so.data.reshape(so.n_tstep,so.n_nodes)returnso
[docs]defto_dict(self):""" Convert to dict. Return: dict """return{'n_nodes':self.n_nodes,'n_tstep':self.n_tstep,'nodeids':self.nodeids,'start':self.start,'interval':self.interval,'data':self.data}