Skip to content

Commit

Permalink
Expose -sw, -lw, -iter
Browse files Browse the repository at this point in the history
  • Loading branch information
Jennings Zhang authored and Jennings Zhang committed Jul 29, 2022
1 parent c30138e commit 4e73048
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
30 changes: 25 additions & 5 deletions ep_surface_fit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python

import itertools
import os
import sys
from pathlib import Path
Expand Down Expand Up @@ -34,6 +34,11 @@
formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('--no-fail', dest='no_fail', action='store_true',
help='Produce exit code 0 even if any subprocesses do not.')
parser.add_argument('--sw', type=int, default=200, help='stretch weight')
parser.add_argument('--lw', type=float, default=5e-6, help='laplacian weight')
parser.add_argument('--iter', type=int, default=600, help='iterations')


parser.add_argument('-V', '--version', action='version',
version=f'%(prog)s {__version__}')

Expand All @@ -48,17 +53,26 @@
def main(options: Namespace, inputdir: Path, outputdir: Path):
print(DISPLAY_TITLE, file=sys.stderr, flush=True)

params = [
'-lw',
str(options.lw),
'-sw',
str(options.sw),
'-iter',
str(options.iter)
]

nproc = len(os.sched_getaffinity(0))
logger.info('Using {} threads.', nproc)
mapper = PathMapper.file_mapper(inputdir, outputdir, glob='**/*.obj', suffix='.obj')
with ThreadPoolExecutor(max_workers=nproc) as pool:
results = pool.map(lambda t: run_surface_fit(*t), mapper)
results = pool.map(lambda t, p: run_surface_fit(*t, p), mapper, itertools.repeat(params))

if not options.no_fail and not all(results):
sys.exit(1)


def run_surface_fit(surface: Path, output: Path) -> bool:
def run_surface_fit(surface: Path, output: Path, params: list[str]) -> bool:
"""
:return: True if successful
"""
Expand All @@ -67,7 +81,7 @@ def run_surface_fit(surface: Path, output: Path) -> bool:
logger.error('No mask found for {}', surface)
return False

cmd = ['surface_fit_script.pl', mask, surface, output]
cmd = ['surface_fit_script.pl', *params, mask, surface, output]
log_file = output.with_name(output.name + '.log')
logger.info('Starting: {}', ' '.join(map(str, cmd)))
with log_file.open('wb') as log_handle:
Expand All @@ -88,7 +102,13 @@ def locate_mask_for(surface: Path) -> Optional[Path]:
mask = surface.with_name(name)
if mask.exists():
return mask
return None

glob = surface.parent.glob('*.mnc')
first = next(glob, None)
second = next(glob, None)
if second is not None:
return None
return first


if __name__ == '__main__':
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='ep_surface_fit',
version='0.0.1',
version='0.1.0',
description='surface_fit wrapper',
author='Jennings Zhang',
author_email='[email protected]',
Expand All @@ -11,7 +11,6 @@
scripts=['surface_fit_script.pl'],
install_requires=['chris_plugin'],
license='MIT',
python_requires='>=3.10.5',
entry_points={
'console_scripts': [
'ep_surface_fit = ep_surface_fit:main'
Expand Down
13 changes: 9 additions & 4 deletions surface_fit_script.pl
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@
my $age = 0;
my $no_downsize = 0;
my $save_chamfer = undef;
my $given_sw = 200;
my $given_lw = 5e-6;
my $given_iter = 600;

my @options = (
# no options
['-iter', 'integer', 1, \$given_iter, "iterations"],
['-sw', 'integer', 1, \$given_sw, "stretch weight"],
['-lw', 'float', 1, \$given_lw, "laplacian weight"],
);

GetOptions( \@options, \@ARGV ) or exit 1;
Expand Down Expand Up @@ -101,9 +106,9 @@
# t iterations of taubin smoothing after cycles of surface_fit

my @schedule = (
# size sw n_itr inc l_w iso si os iw self t chamfer_algo
# ----- --- ----- --- ---- --- ---- --- ---- ---- -- --------
81920, 30, 800, 50, 5e-6, 10, 0.20, 0.0, 1e0, 0.01, 0, $simple,
# size sw n_itr inc l_w iso si os iw self t chamfer_algo
# ----- --- ----- --- ---- --- ---- --- ---- ---- -- --------
81920, $given_sw, $given_iter, 50, $given_lw, 10, 0.20, 0.0, 1e0, 0.01, 0, $simple,
);

# Do the fitting stages like gray surface expansion.
Expand Down

0 comments on commit 4e73048

Please sign in to comment.