Skip to content

Commit 53d2f9c

Browse files
committed
'silent_output' argument
instead of 'tee' key in definition files for algorithms
1 parent 19cb393 commit 53d2f9c

38 files changed

+59
-73
lines changed

dds.py

+26-26
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def get_subfolders_generated_by(path: Path, generator_name: str) -> list[Path]:
169169
# Execute either <algo_name>.yml or <algo_name>.py
170170
# The fist one must be executed on an instance of DataFolder
171171
# The second has not this constraint (any folder, eg the parent folder of many DataFolder)
172-
def run(path: Path, algo_name: str, arguments_as_list: list = list()):
172+
def run(path: Path, algo_name: str, arguments_as_list: list = list(), silent_output: bool = None):
173173
YAML_filepath: Path = Path('definitions/algorithms') / (algo_name + '.yml')
174174
if not YAML_filepath.exists():
175175
# it can be the name of a custom algorithm, defined in an <algo_name>.py
@@ -178,7 +178,6 @@ def run(path: Path, algo_name: str, arguments_as_list: list = list()):
178178
log.error(f"Cannot run '{algo_name}' because neither {YAML_filepath} nor {script_filepath} exist")
179179
exit(1)
180180

181-
# command = f"{script_filepath} {' '.join(arguments_as_list)}"
182181
spec = importlib.util.spec_from_file_location(
183182
name="ext_module",
184183
location=script_filepath,
@@ -187,10 +186,11 @@ def run(path: Path, algo_name: str, arguments_as_list: list = list()):
187186
spec.loader.exec_module(ext_module)
188187

189188
console = Console()
190-
console.print(Rule(f'beginning of [magenta]{script_filepath}[/]'))
191-
# completed_process = subprocess_tee.run(command, shell=True, capture_output=True, tee=True)
192-
ext_module.main(path,arguments_as_list)
193-
console.print(Rule(f'end of [magenta]{script_filepath}[/]'))
189+
if not silent_output:
190+
console.print(Rule(f'beginning of [magenta]{script_filepath}[/]'))
191+
ext_module.main(path,arguments_as_list,silent_output)
192+
if not silent_output:
193+
console.print(Rule(f'end of [magenta]{script_filepath}[/]'))
194194
exit(0)
195195
# convert arguments to a dict
196196
# -> from ['arg1=value', 'arg2=value'] to {'arg1': 'value', 'arg2': 'value'}
@@ -203,7 +203,7 @@ def run(path: Path, algo_name: str, arguments_as_list: list = list()):
203203
log.error(f"No '=' in supplemental argument '{arg}'")
204204
exit(1)
205205
data_folder = DataFolder(path)
206-
data_folder.run(algo,arguments)
206+
data_folder.run(algo,arguments,silent_output)
207207

208208
class DataFolderInstantiationError(Exception):
209209
"""
@@ -336,7 +336,7 @@ def get_closest_parent_of_type(self, data_folder_type: str, check_self = True):
336336
return parent
337337
return parent.get_closest_parent_of_type(data_folder_type,False)
338338

339-
def execute_algo_preprocessing(self, console: Console, algo_name: str, output_subfolder: Path, arguments: dict) -> dict:
339+
def execute_algo_preprocessing(self, console: Console, algo_name: str, output_subfolder: Path, arguments: dict, silent_output: bool) -> dict:
340340
script_filepath: Path = Path('definitions/algorithms') / (algo_name + '.pre.py')
341341
if not script_filepath.exists():
342342
return dict() # no preprocessing defined for this algorithm
@@ -347,12 +347,14 @@ def execute_algo_preprocessing(self, console: Console, algo_name: str, output_su
347347
)
348348
ext_module = importlib.util.module_from_spec(spec)
349349
spec.loader.exec_module(ext_module)
350-
console.print(Rule(f'beginning of {script_filepath.name} pre_processing()'))
351-
data_from_preprocessing = ext_module.pre_processing(self,output_subfolder,arguments)
352-
console.print(Rule(f'end of {script_filepath.name} pre_processing()'))
350+
if not silent_output:
351+
console.print(Rule(f'beginning of {script_filepath.name} pre_processing()'))
352+
data_from_preprocessing = ext_module.pre_processing(self,output_subfolder,arguments,silent_output)
353+
if not silent_output:
354+
console.print(Rule(f'end of {script_filepath.name} pre_processing()'))
353355
return data_from_preprocessing
354356

355-
def execute_algo_postprocessing(self, console: Console, algo_name: str, output_subfolder: Optional[Path], arguments: dict, data_from_preprocessing: dict) -> dict:
357+
def execute_algo_postprocessing(self, console: Console, algo_name: str, output_subfolder: Optional[Path], arguments: dict, data_from_preprocessing: dict, silent_output: bool) -> dict:
356358
script_filepath: Path = Path('definitions/algorithms') / (algo_name + '.post.py')
357359
if not script_filepath.exists():
358360
return # no postprocessing defined for this algorithm
@@ -363,14 +365,16 @@ def execute_algo_postprocessing(self, console: Console, algo_name: str, output_s
363365
)
364366
ext_module = importlib.util.module_from_spec(spec)
365367
spec.loader.exec_module(ext_module)
366-
console.print(Rule(f'beginning of {script_filepath.name} post_processing()'))
368+
if not silent_output:
369+
console.print(Rule(f'beginning of {script_filepath.name} post_processing()'))
367370
if output_subfolder is None: # post-processing of a transformative algorithme
368-
ext_module.post_processing(self,arguments,data_from_preprocessing)
371+
ext_module.post_processing(self,arguments,data_from_preprocessing,silent_output)
369372
else: # post-processing of a generative algorithm
370-
ext_module.post_processing(self,output_subfolder,arguments,data_from_preprocessing)
371-
console.print(Rule(f'end of {script_filepath.name} post_processing()'))
373+
ext_module.post_processing(self,output_subfolder,arguments,data_from_preprocessing,silent_output)
374+
if not silent_output:
375+
console.print(Rule(f'end of {script_filepath.name} post_processing()'))
372376

373-
def run(self, algo_name: str, arguments: dict = dict()):
377+
def run(self, algo_name: str, arguments: dict = dict(), silent_output: bool = False):
374378
YAML_filepath: Path = Path('definitions/algorithms') / (algo_name + '.yml')
375379
if not YAML_filepath.exists():
376380
log.error(f"Cannot run '{algo_name}' because {YAML_filepath} does not exist")
@@ -497,18 +501,14 @@ def run(self, algo_name: str, arguments: dict = dict()):
497501
console = Console()
498502
with console.status(f'Executing [bold yellow]{algo_name}[/] on [bold cyan]{collapseuser(self.path)}[/]...') as status:
499503
# execute preprocessing
500-
data_from_preprocessing = self.execute_algo_preprocessing(console,algo_name,output_folder_path,all_arguments)
504+
data_from_preprocessing = self.execute_algo_preprocessing(console,algo_name,output_folder_path,all_arguments,silent_output)
501505
# execute the command line
502-
if 'tee' not in YAML_content[self.type]:
503-
log.error(f"{YAML_filepath} has no '{self.type}/tee' entry")
504-
exit(1)
505-
tee = YAML_content[self.type]['tee']
506-
if tee:
506+
if not silent_output:
507507
console.print(Rule(f'beginning of [magenta]{collapseuser(executable_path)}'))
508508
chrono_start = time.monotonic()
509-
completed_process = subprocess_tee.run(command_line, shell=True, capture_output=True, tee=tee)
509+
completed_process = subprocess_tee.run(command_line, shell=True, capture_output=True, tee=(not silent_output))
510510
chrono_stop = time.monotonic()
511-
if tee:
511+
if not silent_output:
512512
console.print(Rule(f'end of [magenta]{collapseuser(executable_path)}'))
513513
# write stdout and stderr
514514
if completed_process.stdout != '': # if the subprocess wrote something in standard output
@@ -531,7 +531,7 @@ def run(self, algo_name: str, arguments: dict = dict()):
531531
with open(info_file_path,'w') as file:
532532
json.dump(info_file, file, sort_keys=True, indent=4)
533533
# execute postprocessing
534-
self.execute_algo_postprocessing(console,algo_name,output_folder_path,all_arguments,data_from_preprocessing)
534+
self.execute_algo_postprocessing(console,algo_name,output_folder_path,all_arguments,data_from_preprocessing,silent_output)
535535

536536
if __name__ == "__main__":
537537

definitions/algorithms/AlgoHex.yml

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ tet-mesh: { # case of 'AlgoHex' applied on a 'tet-mesh' subfolder
55
path: ALGOHEX,
66
command_line: "-i {tet_mesh} -o {hex_mesh} --igm-out-path {IGM}"
77
},
8-
tee: true,
98
output_folder: 'AlgoHex_%d',
109
arguments: {
1110
input_files: {

definitions/algorithms/Gmsh.yml

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ step: { # case of 'Gmsh' applied on a 'step' subfolder
1515
path: GMSH,
1616
command_line: '{CAD_file} -3 -format mesh -o {mesh} -setnumber Mesh.CharacteristicLengthFactor {characteristic_length_factor} -nt {nb_threads}', # 'Mesh.MeshSizeFactor' or '-clscale' instead ?
1717
},
18-
tee: true,
1918
output_folder: 'Gmsh_{characteristic_length_factor}',
2019
arguments: {
2120
input_files: {

definitions/algorithms/HexBox.yml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ tet-mesh: { # case of 'HexBox' applied on a 'tet-mesh' subfolder
66
filename: ,
77
command_line: '{mesh}'
88
},
9-
tee: true,
109
output_folder: 'HexBox_%d',
1110
arguments: {
1211
input_files: {

definitions/algorithms/MEDIT_to_VTKv2.yml

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ tet-mesh: { # case of 'MEDIT_to_VTKv2' applied on a 'tet-mesh' subfolder
55
path: GMSH,
66
command_line: '{input_mesh} -format vtk -o {dot_vtk} -save'
77
},
8-
tee: true,
98
# no `output_folder` -> transformative algorithm
109
arguments: {
1110
input_files: {

definitions/algorithms/OVM_to_MEDIT.yml

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ hex-mesh: { # case of 'OVM_to_MEDIT' applied on a 'hex-mesh' subfolder
55
path: OVM_IO,
66
command_line: '{input} {output}'
77
},
8-
tee: true,
98
# no `output_folder` -> transformative algorithm
109
arguments: {
1110
input_files: {

definitions/algorithms/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ input_type: {
1414
filename: , # optional, if a filename must be appended to the path
1515
command_line: # command line template. between curly brackets are {arguments}, filled below. but {output_folder} is a reserved keyword that will be filled with the output folder path
1616
},
17-
tee: , # boolean. if the stdout should be printed while the algorithm is running (in all cases the stdout is captured and written to file)
1817
output_folder: , # string template of the output folder to create ("generative algorithm" case). '%d' replaced by datetime
1918
arguments: { # all keywords in the executable definition must be covered with 'argumentX' entries
2019
input_files: {

definitions/algorithms/automatic_polycube.yml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ tet-mesh: { # case of 'automatic_polycube' applied on a 'tet-mesh' subfolder
66
filename: automatic_polycube,
77
command_line: '{mesh} {labeling} gui=false'
88
},
9-
tee: true,
109
output_folder: 'automatic_polycube_%d',
1110
arguments: {
1211
input_files: {

definitions/algorithms/automatic_polycube_gui.yml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ tet-mesh: { # case of 'automatic_polycube_gui' applied on a 'tet-mesh' subfolder
66
filename: automatic_polycube,
77
command_line: '{mesh} {labeling} gui=true'
88
},
9-
tee: true,
109
output_folder: 'automatic_polycube_%d',
1110
arguments: {
1211
input_files: {

definitions/algorithms/evocube.post.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# own module
99
from dds import *
1010

11-
def post_processing(input_subfolder: DataFolder, output_subfolder: Path, arguments: dict, data_from_pre_processing: dict):
11+
def post_processing(input_subfolder: DataFolder, output_subfolder: Path, arguments: dict, data_from_pre_processing: dict, silent_output: bool):
1212
assert(input_subfolder.type == 'tet-mesh')
1313

1414
# read labeling.yml to get some filenames
@@ -36,13 +36,15 @@ def post_processing(input_subfolder: DataFolder, output_subfolder: Path, argumen
3636
old_to_new_filenames['fast_polycube_surf.obj'] = labeling_type['filenames']['POLYCUBE_SURFACE_MESH_OBJ']
3737
for old,new in old_to_new_filenames.items():
3838
if (output_subfolder / old).exists():
39-
print(f'Renaming {old}...')
39+
if not silent_output:
40+
print(f'Renaming {old}...')
4041
move(
4142
str((output_subfolder / old).absolute()),
4243
str((output_subfolder / new).absolute())
4344
)
4445

4546
# remove the tris_to_tets.txt file created in pre-processing
4647
if (output_subfolder / 'tris_to_tets.txt').exists():
47-
print(f'Removing tris_to_tets.txt...')
48+
if not silent_output:
49+
print(f'Removing tris_to_tets.txt...')
4850
unlink(output_subfolder / 'tris_to_tets.txt')

definitions/algorithms/evocube.pre.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
# own module
66
from dds import *
77

8-
def pre_processing(input_subfolder: DataFolder, output_subfolder: Path, arguments: dict) -> dict: # return data for the post-processing
8+
def pre_processing(input_subfolder: DataFolder, output_subfolder: Path, arguments: dict, silent_output: bool) -> dict: # return data for the post-processing
99
data_for_post_processing = dict()
1010
# evocube also wants the surface map, as 'tris_to_tets.txt', inside the output folder, but without the 'triangles' and 'tetrahedra' annotations
1111
with open(input_subfolder.get_file('SURFACE_MAP_TXT'),'r') as infile:
12-
print('Writing tris_to_tets.txt...')
12+
if not silent_output:
13+
print('Writing tris_to_tets.txt...')
1314
with open(output_subfolder / 'tris_to_tets.txt','w') as outfile: # where evocube expects the surface map
1415
for line in infile.readlines():
1516
outfile.write(line.split()[0] + '\n') # keep only what is before ' '

definitions/algorithms/evocube.yml

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ tet-mesh: { # case of 'evocube' applied on a 'tet-mesh' subfolder
55
path: EVOCUBE,
66
command_line: '{mesh} {output_folder}'
77
},
8-
tee: true,
98
output_folder: 'evocube_%d',
109
arguments: {
1110
input_files: {

definitions/algorithms/extract_surface.yml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ tet-mesh: { # case of 'extract_surface' applied on a 'tet-mesh' subfolder
66
filename: extract_surface,
77
command_line: '{tet_mesh} {surface_mesh} {surface_map}'
88
},
9-
tee: true,
109
# no `output_folder` -> transformative algorithm
1110
arguments: {
1211
input_files: {

definitions/algorithms/fastbndpolycube.yml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ labeling: { # case of 'fastbndpolycube' applied on a 'labeling' subfolder
66
filename: marchinghex_hexmeshing,
77
command_line: "{mesh} {labeling} {polycube}"
88
},
9-
tee: true,
109
# no `output_folder` -> transformative algorithm
1110
arguments: {
1211
input_files: {

definitions/algorithms/global_padding.post.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# own module
1010
from dds import *
1111

12-
def post_processing(input_subfolder: DataFolder, output_subfolder: DataFolder, arguments: dict, data_from_pre_processing: dict):
12+
def post_processing(input_subfolder: DataFolder, output_subfolder: DataFolder, arguments: dict, data_from_pre_processing: dict, silent_output: bool):
1313
assert(input_subfolder.type == 'hex-mesh')
1414
assert(output_subfolder.type == 'hex-mesh')
1515

@@ -31,6 +31,10 @@ def post_processing(input_subfolder: DataFolder, output_subfolder: DataFolder, a
3131
]:
3232
if Path(debug_filename).exists():
3333
if arguments['others']['keep_debug_files']:
34+
if not silent_output:
35+
print(f'Renaming {debug_filename}...')
3436
move(debug_filename, output_subfolder / f'rb_perform_postprocessing.{debug_filename}')
3537
else:
38+
if not silent_output:
39+
print(f'Removing {debug_filename}...')
3640
unlink(debug_filename)

definitions/algorithms/global_padding.yml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ hex-mesh: { # case of 'global_padding' applied on a 'hex-mesh' subfolder
66
filename: rb_perform_postprocessing,
77
command_line: '{tet_mesh} {hex_mesh} {improved_hex_mesh}'
88
},
9-
tee: true,
109
output_folder: global_padding,
1110
arguments: {
1211
input_files: {

definitions/algorithms/graphcut_labeling_gui.yml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ tet-mesh: { # case of 'graphcut_labeling' applied on a 'tet-mesh' subfolder
66
filename: graphcut_labeling,
77
command_line: '{mesh} {labeling}'
88
},
9-
tee: true,
109
output_folder: 'graphcut_labeling',
1110
arguments: {
1211
input_files: {

definitions/algorithms/hex_mesh_stats.yml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ hex-mesh: { # case of 'hex_mesh_stats' applied on a 'hex-mesh' subfolder
66
filename: mesh_stats,
77
command_line: '{mesh}'
88
},
9-
tee: true,
109
# no `output_folder` -> transformative algorithm
1110
arguments: {
1211
input_files: {

definitions/algorithms/import_MAMBO.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from urllib import request
99
from rich.prompt import Confirm
1010

11-
def main(folder_path: Path, arguments: list):
11+
def main(folder_path: Path, arguments: list, silent_output: bool):
1212
# check `arguments`
1313
path_to_MAMBO = None
1414
if len(arguments) > 1:
@@ -55,7 +55,8 @@ def main(folder_path: Path, arguments: list):
5555
continue
5656
mkdir(folder_path / file.stem)
5757
copyfile(file, folder_path / file.stem / STEP_filename)
58-
print(file.stem + ' imported')
58+
if not silent_output:
59+
print(file.stem + ' imported')
5960
if tmp_folder is not None:
6061
# delete the temporary directory
6162
logging.debug(f"Deleting folder '{tmp_folder}'")

definitions/algorithms/labeling_painter.yml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ tet-mesh: { # case of 'labeling_painter' applied on a 'tet-mesh' subfolder
66
filename: labeling_painter,
77
command_line: '{mesh}'
88
},
9-
tee: true,
109
output_folder: 'labeling_painter_%d',
1110
arguments: {
1211
input_files: {

definitions/algorithms/labeling_stats.yml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ labeling: { # case of 'write_polycube_as_geogram' applied on a 'labeling' subfol
66
filename: labeling_stats,
77
command_line: '{mesh} {labeling}'
88
},
9-
tee: true,
109
# no `output_folder` -> transformative algorithm
1110
arguments: {
1211
input_files: {

definitions/algorithms/marchinghex.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from dds import *
66

7-
def main(input_path: Path, arguments: list):
7+
def main(input_path: Path, arguments: list, silent_output: bool):
88
# check 'arguments'
99
# check existence of 'input_path'
1010
# instantiate and get type (must be 'tet-mesh')

definitions/algorithms/marchinghex_gridgenerator.yml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ tet-mesh: { # case of 'marchinghex_gridgenerator' applied on a 'tet-mesh' subfol
66
filename: gridgenerator,
77
command_line: '{input_mesh} {grid_mesh} {scale}'
88
},
9-
tee: true,
109
output_folder: 'marchinghex_{scale}',
1110
arguments: {
1211
input_files: {

definitions/algorithms/marchinghex_hexmeshing.post.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# own module
1010
from dds import *
1111

12-
def post_processing(input_subfolder: DataFolder, output_subfolder: Path, arguments: dict, data_from_pre_processing: dict):
12+
def post_processing(input_subfolder: DataFolder, output_subfolder: Path, arguments: dict, data_from_pre_processing: dict, silent_output: bool):
1313
assert(input_subfolder.type == 'marchinghex_grid')
1414

1515
# It may be interesting to read the last printed line to have the average Hausdorff distance between the domain and the hex-mesh
@@ -24,8 +24,10 @@ def post_processing(input_subfolder: DataFolder, output_subfolder: Path, argumen
2424
] + [x for x in Path(curdir).iterdir() if x.is_file() and x.stem.startswith('iter_')]: # and all 'iter_*' files
2525
if Path(debug_filename).exists():
2626
if arguments['others']['keep_debug_files']:
27-
print(f'Renaming {debug_filename}...')
27+
if not silent_output:
28+
print(f'Renaming {debug_filename}...')
2829
move(debug_filename, output_subfolder / f'marchinghex_hexmeshing.{debug_filename}')
2930
else:
30-
print(f'Removing {debug_filename}...')
31+
if not silent_output:
32+
print(f'Removing {debug_filename}...')
3133
unlink(debug_filename)

definitions/algorithms/marchinghex_hexmeshing.yml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ marchinghex_grid: { # case of 'marchinghex_hexmeshing' applied on a 'marchinghex
66
filename: marchinghex_hexmeshing,
77
command_line: '{grid_mesh} {tet_mesh} {hex_mesh}'
88
},
9-
tee: true,
109
# no `output_folder` -> transformative algorithm
1110
arguments: {
1211
input_files: {

definitions/algorithms/naive_labeling.yml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ tet-mesh: { # case of 'naive_labeling' applied on a 'tet-mesh' subfolder
66
filename: naive_labeling,
77
command_line: '{mesh} {labeling}'
88
},
9-
tee: true,
109
output_folder: 'naive_labeling',
1110
arguments: {
1211
input_files: {

definitions/algorithms/polycube_withHexEx.yml

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ labeling: { # case of 'polycube_withHexEx' applied on a 'labeling' subfolder
55
path: POLYCUBE_WITH_HEXEX,
66
command_line: '{tet_mesh} {labeling} {hex_mesh} {scale}'
77
},
8-
tee: true,
98
output_folder: 'polycube_withHexEx_{scale}',
109
arguments: {
1110
input_files: {

0 commit comments

Comments
 (0)