agd.Eikonal.FileIO

 1# Copyright 2020 Jean-Marie Mirebeau, University Paris-Sud, CNRS, University Paris-Saclay
 2# Distributed WITHOUT ANY WARRANTY. Licensed under the Apache License, Version 2.0, see http://www.apache.org/licenses/LICENSE-2.0
 3
 4import numbers;
 5import numpy as np;
 6import os
 7from operator import mul
 8from functools import reduce
 9import subprocess
10import platform
11
12
13#These two methods export a dictonary to a (pair of) files, and conversely.
14def RawToFiles(params,prefix='input'):
15	"""
16	Exports a dictionary to a pair of files, whose name begins with 'prefix'.
17	The dictionnary elements must by strings, scalars, and numpy arrays.
18	The resulting files are readable by the HFM library.
19	"""
20	assert isinstance(prefix,str)
21	f = open(prefix+'_Format.txt','w')
22	data=[]
23	for key,val in params.items():
24		if isinstance(val,numbers.Number):
25			f.write(key+'\n0\n\n')
26			data.append([val])
27		elif isinstance(val,str):
28			f.write(key+'\n-1\n'+val.encode("unicode_escape").decode("utf-8")+'\n\n')
29		elif type(val)==np.ndarray:
30			f.write(key+'\n'+str(val.ndim)+'\n')
31			for dim in val.shape:
32				f.write(str(dim)+'\n')
33			f.write('\n')
34			data.append(val.flatten())
35		else:
36			raise ValueError(f"Invalid type for key {key} : {type(val)}")
37	f.close()
38	np.concatenate(data).astype('d').tofile(prefix+'_Data.dat')
39	
40def FilesToRaw(prefix='output'):
41	"""
42	Imports a pair of files, whose name begins with 'prefix', into a dictionary.
43	These files may be produced by the HFM library.
44	"""
45	data=np.fromfile(prefix+'_Data.dat')
46	pos=0;
47	f=open(prefix+'_Format.txt')
48	dict={}
49	while True:
50		key=f.readline().strip()
51		if not key: break
52		keyType = int(f.readline())
53		if keyType==-1:
54			dict[key]=f.readline().strip().encode("utf-8").decode("unicode_escape")
55		elif keyType==0:
56			dict[key]=data[pos]
57			pos+=1
58		else:
59			dims=[int(f.readline()) for i in range(keyType)]
60			size=reduce(mul,dims)
61			dict[key]=np.reshape(data.take(np.arange(pos,pos+size)),dims)
62			pos+=size
63		f.readline()
64	return dict
65
66def WriteCallRead(inputData,executable,binary_dir='',working_dir=None,
67				  inputPrefix="input",outputPrefix="output"):
68	if working_dir is None: working_dir=binary_dir
69	if platform.system()=='Linux' and binary_dir=='.': binary_dir=''
70
71	RawToFiles(inputData,os.path.join(working_dir,inputPrefix) ) # Export inputData
72	
73	process = subprocess.Popen([os.path.join(binary_dir,executable),inputPrefix,outputPrefix],
74		stdout=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=True,cwd=working_dir)
75	for stdout_line in iter(process.stdout.readline, ""):
76		print(stdout_line,end='')
77	retcode = process.wait() #returncode
78	if retcode!=0:  print('Returned with exit code ', retcode)
79	
80	outputData = FilesToRaw(os.path.join(working_dir,outputPrefix)) # Import outputData
81	outputData['retcode']=retcode
82	return outputData
def RawToFiles(params, prefix='input'):
15def RawToFiles(params,prefix='input'):
16	"""
17	Exports a dictionary to a pair of files, whose name begins with 'prefix'.
18	The dictionnary elements must by strings, scalars, and numpy arrays.
19	The resulting files are readable by the HFM library.
20	"""
21	assert isinstance(prefix,str)
22	f = open(prefix+'_Format.txt','w')
23	data=[]
24	for key,val in params.items():
25		if isinstance(val,numbers.Number):
26			f.write(key+'\n0\n\n')
27			data.append([val])
28		elif isinstance(val,str):
29			f.write(key+'\n-1\n'+val.encode("unicode_escape").decode("utf-8")+'\n\n')
30		elif type(val)==np.ndarray:
31			f.write(key+'\n'+str(val.ndim)+'\n')
32			for dim in val.shape:
33				f.write(str(dim)+'\n')
34			f.write('\n')
35			data.append(val.flatten())
36		else:
37			raise ValueError(f"Invalid type for key {key} : {type(val)}")
38	f.close()
39	np.concatenate(data).astype('d').tofile(prefix+'_Data.dat')

Exports a dictionary to a pair of files, whose name begins with 'prefix'. The dictionnary elements must by strings, scalars, and numpy arrays. The resulting files are readable by the HFM library.

def FilesToRaw(prefix='output'):
41def FilesToRaw(prefix='output'):
42	"""
43	Imports a pair of files, whose name begins with 'prefix', into a dictionary.
44	These files may be produced by the HFM library.
45	"""
46	data=np.fromfile(prefix+'_Data.dat')
47	pos=0;
48	f=open(prefix+'_Format.txt')
49	dict={}
50	while True:
51		key=f.readline().strip()
52		if not key: break
53		keyType = int(f.readline())
54		if keyType==-1:
55			dict[key]=f.readline().strip().encode("utf-8").decode("unicode_escape")
56		elif keyType==0:
57			dict[key]=data[pos]
58			pos+=1
59		else:
60			dims=[int(f.readline()) for i in range(keyType)]
61			size=reduce(mul,dims)
62			dict[key]=np.reshape(data.take(np.arange(pos,pos+size)),dims)
63			pos+=size
64		f.readline()
65	return dict

Imports a pair of files, whose name begins with 'prefix', into a dictionary. These files may be produced by the HFM library.

def WriteCallRead( inputData, executable, binary_dir='', working_dir=None, inputPrefix='input', outputPrefix='output'):
67def WriteCallRead(inputData,executable,binary_dir='',working_dir=None,
68				  inputPrefix="input",outputPrefix="output"):
69	if working_dir is None: working_dir=binary_dir
70	if platform.system()=='Linux' and binary_dir=='.': binary_dir=''
71
72	RawToFiles(inputData,os.path.join(working_dir,inputPrefix) ) # Export inputData
73	
74	process = subprocess.Popen([os.path.join(binary_dir,executable),inputPrefix,outputPrefix],
75		stdout=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=True,cwd=working_dir)
76	for stdout_line in iter(process.stdout.readline, ""):
77		print(stdout_line,end='')
78	retcode = process.wait() #returncode
79	if retcode!=0:  print('Returned with exit code ', retcode)
80	
81	outputData = FilesToRaw(os.path.join(working_dir,outputPrefix)) # Import outputData
82	outputData['retcode']=retcode
83	return outputData