Skip to content

Commit

Permalink
Merge pull request #20 from ToFuProject/Issue008_Verb
Browse files Browse the repository at this point in the history
[#8] compute_spectral_fit(verb=True, False, 0, 1, 2, 3 implemented)
  • Loading branch information
dvezinet authored Jul 25, 2024
2 parents 0d82793 + 5d16b14 commit 09a7728
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 14 deletions.
49 changes: 43 additions & 6 deletions spectrally/_class02_compute_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def main(
binning,
chain,
store, overwrite,
strict, verb, timing,
strict, verb, verb_scp, timing,
) = _check(
coll=coll,
key=key,
Expand Down Expand Up @@ -107,7 +107,7 @@ def main(
dsolver_options = _get_solver_options(
solver=solver,
dsolver_options=dsolver_options,
verb=verb,
verb_scp=verb_scp,
)

# ------------
Expand All @@ -120,6 +120,19 @@ def main(
else:
pass

# -----------
# verb

if verb >= 1:
msg = (
"\n\n-------------------------------------------\n"
f"\tComputing spectral fit '{key}'\n"
)
print(msg)

# --------
# run

dout = compute(
coll=coll,
# keys
Expand All @@ -146,6 +159,7 @@ def main(
# options
strict=strict,
verb=verb,
verb_scp=verb_scp,
timing=timing,
)

Expand All @@ -170,6 +184,7 @@ def main(
# ------------

if store is True:

_store(
coll=coll,
overwrite=overwrite,
Expand All @@ -184,6 +199,8 @@ def main(
binning=binning,
# dout
dout=dout,
# verb
verb=verb,
)

return dout
Expand Down Expand Up @@ -327,6 +344,16 @@ def _check(
)
if verb is True:
verb = def_verb
elif verb is False:
verb = 0

# verb_scp
verb_scp = {
0: 0,
1: 0,
2: 0,
3: 2,
}[verb]

# --------------
# timing
Expand All @@ -347,7 +374,7 @@ def _check(
binning,
chain,
store, overwrite,
strict, verb, timing,
strict, verb, verb_scp, timing,
)


Expand All @@ -360,7 +387,7 @@ def _check(
def _get_solver_options(
solver=None,
dsolver_options=None,
verb=None,
verb_scp=None,
):

# -------------------
Expand Down Expand Up @@ -392,7 +419,7 @@ def _get_solver_options(
diff_step=None,
max_nfev=None,
loss='linear',
verbose=verb,
verbose=verb_scp,
)

else:
Expand Down Expand Up @@ -443,6 +470,8 @@ def _store(
binning=None,
# dout
dout=None,
# verb
verb=None,
):

# ------------
Expand Down Expand Up @@ -497,7 +526,7 @@ def _store(

# other outputs
lk = [
'cost', 'chi2n', 'time', 'success', 'nfev',
'cost', 'chi2n', 'time', 'status', 'nfev',
'msg', 'validity', 'errmsg',
]
dk_out = {k0: f"{key}_{k0}" for k0 in lk}
Expand Down Expand Up @@ -548,4 +577,12 @@ def _store(
for k0, k1 in dk_out.items():
coll._dobj[wsf][key]['dsolver'][k0] = k1

# ---------
# verb
# ----------

if verb >= 2:
msg = f"Storing under key '{ksol}'"
print(msg)

return
83 changes: 75 additions & 8 deletions spectrally/_class02_compute_fit_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,22 @@ def main(
# options
strict=None,
verb=None,
verb_scp=None,
timing=None,
):
""" Fit 1d spectra
"""

# -----------
# verb

if verb >= 2:
msg = (
"Preparing input data (scales, bounds, x0...)\n"
)
print(msg)

# ------------
# iok
# ------------
Expand Down Expand Up @@ -205,6 +215,7 @@ def main(
lk_xfree=lk_xfree,
strict=strict,
verb=verb,
verb_scp=verb_scp,
timing=timing,
)

Expand Down Expand Up @@ -287,6 +298,7 @@ def _loop(
lk_xfree=None,
strict=None,
verb=None,
verb_scp=None,
timing=None,
):

Expand All @@ -311,20 +323,49 @@ def _loop(
# nspect
nspect = int(np.prod(shape_reduced))

# verb init
if verb is not False:
end = '\r'

# timing init
if timing is True:
t0 = dtm.datetime.now()

# -----------------
# prepare verb
# -----------------

# verb init
if verb == 1:
end = '\r'
elif verb in [2, 3]:
end = '\n'

# iterations
if verb >= 1:
sep = ' '
ditems = {
'spectrum ind': {
'just': max(13, len(str(shape_reduced))*2+3),
'val': None,
},
'nfev': {'just': 6, 'val': None},
'cost_final': {'just': 10, 'val': None},
'status': {'just': 6, 'val': None},
# 'termination': {'just': 12, 'val': None},
}
litems = ['spectrum ind', 'nfev', 'cost_final', 'status']

if verb in [1, 2]:
msg = (
sep.join([k0.ljust(ditems[k0]['just']) for k0 in litems])
+ '\n'
+ sep.join(['-'*ditems[k0]['just'] for k0 in litems])
)
print(msg)

# -----------------
# initialize
# -----------------

validity = np.zeros(shape_reduced, dtype=int)
success = np.full(shape_reduced, np.nan)
status = np.full(shape_reduced, np.nan)
cost = np.full(shape_reduced, np.nan)
chi2n = np.full(shape_reduced, np.nan)
nfev = np.full(shape_reduced, np.nan)
Expand Down Expand Up @@ -383,7 +424,7 @@ def _loop(
# verb

if verb == 3:
msg = f"\nspect {ii+1} / {nspect}"
msg = f"\nspectrum {ind} / {shape_reduced}"
print(msg)

# -----------
Expand Down Expand Up @@ -422,7 +463,7 @@ def _loop(
time[ind] = round(dti, ndigits=6)

# other outputs
success[ind] = res.success
status[ind] = res.status
cost[ind] = res.cost
nfev[ind] = res.nfev

Expand Down Expand Up @@ -461,6 +502,32 @@ def _loop(
errmsg[ii] = msg
validity[ii] = -2

# -------------
# verb

finally:

if verb in [1, 2]:
ditems['spectrum ind']['val'] = f"{ind} / {shape_reduced}"
ditems['status']['val'] = f"{status[ind]:.0f}"
ditems['cost_final']['val'] = f"{cost[ind]:.4e}"
ditems['nfev']['val'] = f"{nfev[ind]:.0f}"

msg = (
sep.join([
ditems[k0]['val'].ljust(ditems[k0]['just'])
for k0 in litems
])
)
print(msg, end=end)

# -------------
# adjust verb
# -------------

if verb == 2:
print()

# --------------
# prepare output
# --------------
Expand All @@ -472,7 +539,7 @@ def _loop(
'nfev': nfev,
'cost': cost,
'chi2n': chi2n,
'success': success,
'status': status,
'time': time,
'errmsg': np.reshape(errmsg, shape_reduced),
'scales': scales,
Expand Down

0 comments on commit 09a7728

Please sign in to comment.