Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bo Peng
committed
Sep 20, 2020
1 parent
7d415b6
commit d613b3f
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
def parse_param_with_multiplier(args, subpops=None, default=None): | ||
base = [x for x in args if '=' not in x] | ||
if not base: | ||
if default is None: | ||
raise ValueError(f'No vase value for multiplier: {" ".join(args)}') | ||
else: | ||
base = default | ||
else: | ||
try: | ||
base = [float(x) for x in base] | ||
if len(base) == 1: | ||
base = base[0] | ||
except: | ||
raise ValueError(f'Invalid parameter {" ".join(base)}') | ||
|
||
if not subpops: | ||
return {'': base} | ||
|
||
res = {x: base for x in subpops} | ||
for arg in [x for x in args if '=' in x]: | ||
sp, val = arg.split('=', 1) | ||
if sp not in subpops: | ||
raise ValueError(f'Invalid subpopulation name {sp}') | ||
try: | ||
val = float(val) | ||
except: | ||
raise ValueError(f'Invalid multiplier: {val}') | ||
# | ||
if isinstance(base, (list, tuple)): | ||
res[sp] = [x * val for x in base] | ||
else: | ||
res[sp] = base * val | ||
|
||
return res |