Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
briochh committed Aug 17, 2023
2 parents 1d762e7 + 509765e commit 3f5f1ff
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 31 deletions.
4 changes: 4 additions & 0 deletions autotest/pst_from_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ def freybergnwt_2_pstfrom(tmp_path):
def freyberg_test(tmp_path):
import numpy as np
import pandas as pd
from pyemu import PyemuWarning
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
Expand Down Expand Up @@ -273,6 +274,9 @@ def freyberg_test(tmp_path):
# sfr outputs to obs
sfr_idx = ['segment', 'reach', 'kstp', 'kper']
sfr_use = ["Qaquifer", "Qout", 'width']
with pytest.warns(PyemuWarning):
pf.add_py_function(__file__, '_gen_dummy_obs_file()',
is_pre_cmd=False)
pf.add_observations('freyberg.sfo.dat', insfile=None,
index_cols=sfr_idx,
use_cols=sfr_use, prefix='sfr',
Expand Down
17 changes: 17 additions & 0 deletions autotest/pst_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,23 @@ def load_test(tmp_path):
if len(exceptions) > 0:
raise Exception('\n'.join(exceptions))


def test_write_precision(tmp_path):
import os
from pyemu import Pst
pst_dir = os.path.join("pst")
org_path = os.path.join(pst_dir, "pest.pst")
new_path = os.path.join(tmp_path, "pest1.pst")
pst = Pst(org_path)
par = pst.parameter_data.copy()
par.loc[par.index[1:20], 'parlbnd'] = 1.e-20
par.loc[par.index[1:20], 'parval1'] = 1.e-14
pst.parameter_data = par
pst.write(new_path)
newpst = Pst(new_path)
assert all(newpst.parameter_data.iloc[1:20].parlbnd > 0)


def comments_test(tmp_path):
import os
import pyemu
Expand Down
2 changes: 1 addition & 1 deletion pyemu/pst/pst_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1908,7 +1908,7 @@ def _write_version1(self, new_filename):
# for line in self.other_lines:
# f_out.write(line)
if self.with_comments:
for line in self.comments.get("* singular value decompisition", []):
for line in self.comments.get("* singular value decomposition", []):
f_out.write(line)
self.svd_data.write(f_out)

Expand Down
73 changes: 43 additions & 30 deletions pyemu/utils/pst_from.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ def __init__(
self.ult_ubound_fill = 1.0e30
self.ult_lbound_fill = -1.0e30
self.chunk_len = int(chunk_len)
self.py_functions = set()


@property
def parfile_relations(self):
Expand Down Expand Up @@ -323,8 +325,8 @@ def parse_kij_args(self, args, kwargs):
(
"get_xy() warning: need locational information "
"(e.g. i,j) to generate xy, "
"insufficient index cols passed to interpret: {}"
""
"insufficient index cols passed to interpret: {}."
"i,j will be set to (None,None)"
).format(str(args))
)
self.ijwarned[self.add_pars_callcount] = True
Expand Down Expand Up @@ -1117,7 +1119,8 @@ def _next_count(self, prefix):
return self._prefix_count[prefix]

def add_py_function(
self, file_name, call_str=None, is_pre_cmd=True, function_name=None
self, file_name, call_str=None, is_pre_cmd=True,
function_name=None
):
"""add a python function to the forward run script
Expand Down Expand Up @@ -1191,33 +1194,42 @@ def add_py_function(
function_name = call_str[
: call_str.find("(")
] # strip to first occurance of '('
func_lines = []
search_str = "def " + function_name + "("
abet_set = set(string.ascii_uppercase)
abet_set.update(set(string.ascii_lowercase))
with open(file_name, "r") as f:
while True:
line = f.readline()
if line == "":
self.logger.lraise(
"add_py_function(): EOF while searching for function '{0}'".format(
search_str
if function_name in self.py_functions:
# todo: could add more duplication options here: override, increment
warnings.warn(
f"add_py_function(): {function_name} already "
f"in forward run python functions, not overriding here, "
f"original will be maintained",
PyemuWarning,
)
else:
func_lines = []
search_str = "def " + function_name + "("
abet_set = set(string.ascii_uppercase)
abet_set.update(set(string.ascii_lowercase))
with open(file_name, "r") as f:
while True:
line = f.readline()
if line == "":
self.logger.lraise(
"add_py_function(): EOF while searching for function '{0}'".format(
search_str
)
)
)
if line.startswith(
search_str
): # case sens and no strip since 'def' should be flushed left
func_lines.append(line)
while True:
line = f.readline()
if line == "":
break
if line[0] in abet_set:
break
if line.startswith(
search_str
): # case sens and no strip since 'def' should be flushed left
func_lines.append(line)
break
while True:
line = f.readline()
if line == "":
break
if line[0] in abet_set:
break
func_lines.append(line)
break

self._function_lines_list.append(func_lines)
self._function_lines_list.append(func_lines)
if is_pre_cmd is True:
self.pre_py_cmds.append(call_str)
elif is_pre_cmd is False:
Expand All @@ -1228,6 +1240,7 @@ def add_py_function(
call_str
)
)
self.py_functions.update({function_name})

def _process_array_obs(
self,
Expand Down Expand Up @@ -1974,17 +1987,17 @@ def add_parameters(
self.logger.warn(
"0) Inconsistency between " "geostruct transform and partrans."
)
self.logger.warn(f"1) Setting geostruct transform to " "{transform}")
self.logger.warn(f"1) Setting geostruct transform to {transform}")
if geostruct not in self.par_struct_dict.keys():
# safe to just reset transform
geostruct.transform = transform
else:
self.logger.warn("2) This will create a new copy of " "geostruct")
self.logger.warn("2) This will create a new copy of geostruct")
# to avoid flip flopping transform need to make a new geostruct
geostruct = copy.copy(geostruct)
geostruct.transform = transform
self.logger.warn(
"-) Better to pass an appropriately " "transformed geostruct"
"-) Better to pass an appropriately transformed geostruct"
)
# big sr and zone dependancy checker here: todo - tidy?
checker = (
Expand Down

0 comments on commit 3f5f1ff

Please sign in to comment.