2 from collections
import OrderedDict
6 from .base
import Creator
9 from refractor
import framework
as rf
28 'retrieval_components': retrieval_components,
29 'state_vector': state_vector,
30 'initial_guess': initial_guess,
43 retrieval_objects = super().
create(**kwargs)
50 retrieval_objects.update({
52 'covariance': covariance,
56 return retrieval_objects
73 if hasattr(rec_obj,
"sub_state_identifier")
and rec_obj.coefficient.value.shape[0] > 0:
74 ss_iden = rec_obj.sub_state_identifier
81 if include_list
is None:
84 for incl_re
in include_list:
85 if re.search(incl_re, iden):
93 if exclude_list
is None:
96 for excl_re
in exclude_list:
97 if re.search(excl_re, iden):
103 filtered_components = []
106 filtered_components.append( (iden, obj) )
111 def sort_key(comp_tuple):
113 for idx, ord_item
in enumerate(order):
114 if re.search(ord_item, comp_tuple[0]):
118 if found_order
is not None:
123 dflt_order = len(order) + filtered_components.index(comp_tuple)
126 return OrderedDict(sorted(filtered_components, key=sort_key))
141 if not hasattr(rec_obj,
"sub_state_identifier")
and rec_obj
not in self.
sv_observers:
145 sv = rf.StateVector()
149 sv.add_observer(observer)
153 sv.add_observer(observer)
169 used_indexes = np.nonzero(ret_component.used_flag_value)
170 ig_values.append(ret_component.coefficient.value[used_indexes])
172 if len(ig_values) == 0:
173 raise param.ParamError(
"InitialGuessFromSV: No initial guess values available as identified by the retrieval components")
175 ig = np.concatenate(ig_values)
177 if ig.shape[0] != sv.observer_claimed_size:
178 raise ValueError(
"The initial guess vector size %d does not match expected state vector size %d" % (ig.shape[0], sv.observer_claimed_size))
197 cov_inputs = self.
values()
203 if rc_name
not in cov_inputs:
204 raise param.ParamError(
"CovarianceByComponent: covariance values argument is missing data for this retrieval component: %s" % rc_name)
206 rc_cov = cov_inputs[rc_name]
208 if not hasattr(rc_cov,
"shape"):
209 raise param.ParamError(
"CovarianceByComponent: value for retrieval component covariance must be a numpy array: %s" % rc_name)
211 if len(rc_cov.shape) != 2:
212 raise param.ParamError(
"CovarianceByComponent: shape of array for retrieval component covariance must be 2: %s" % rc_name)
214 if rc_cov.shape[0] != rc_cov.shape[1]:
215 raise param.ParamError(
"CovarianceByComponent: array for retrieval component must be a square matrix: %s" % rc_name)
217 flag = rc_obj.used_flag_value
219 if flag.shape[0] != rc_cov.shape[0]:
220 raise param.ParamError(
"CovarianceByComponent: covariance shape %s and and flag shape are mismatched: %s" % (flag.shape, rc_cov.shape, rc_name))
222 used_indexes = np.nonzero(flag)
223 used_cov = rc_cov[np.ix_(used_indexes[0], used_indexes[0])]
225 total_len += used_cov.shape[0]
226 covariances.append(used_cov)
229 total_covariance = np.zeros((total_len, total_len), dtype=float)
232 for idx, cov
in enumerate(covariances):
233 total_covariance[offset:offset+cov.shape[0], offset:offset+cov.shape[1]] = cov
234 offset += cov.shape[0]
236 return total_covariance
260 observation = rf.ObservationLevel1b(self.
l1b(), self.
instrument(), fm.spectral_grid)
262 cost_func = rf.ConnorCostFunction(sv, fm, observation)
263 conv = rf.ConnorConvergence(fm,
271 def create_solve_wrapper(conn_solver, conn_creator):
272 orig_solve_func = conn_solver.solve
274 def solve_wrapper(**vargs):
275 "Automatically pass initial guess, apriori and covariance to solve routine if no arguments supplied" 278 return orig_solve_func()
279 elif len(vargs) == 0:
280 return orig_solve_func(conn_creator.initial_guess(), conn_creator.a_priori(), conn_creator.covariance())
282 raise Exception(
"Wrong number of arguments to ConnorSolver::solve. Expected 0 or 3 arguments.")
286 solver = rf.ConnorSolver(cost_func, conv, self.
gamma_initial())
287 solver.solve = create_solve_wrapper(solver, self)
289 iter_log = rf.ConnorIterationLog(sv)
290 solver.add_observer(iter_log)
315 observation = rf.ObservationLevel1b(self.
l1b(), self.
instrument(), fm.spectral_grid)
319 opt_problem = rf.NLLSMaxAPosteriori(stat_method)
328 solver.add_observer(iter_log)
338 solver = rf.NLLSSolverGSLLMSDER(self.
opt_problem(),
359 solver = rf.ConnorSolverMAP(self.
opt_problem(), conv,
386 opts = rf.NLLSSolverLMOptions()
388 if self.
min_W()
is not None:
389 opts.min_W = self.
min_W()
394 if self.
tr_rad()
is not None:
395 opts.tr_rad = self.
tr_rad()
def is_excluded(self, iden)
def __init__(self, vargs, kwargs)
def attach_logging(self, solver)
def is_included(self, iden)
def init_state_vector(self)
def receive(self, rec_obj)
def __init__(self, vargs, kwargs)
def receive(self, rec_obj)
def __init__(self, vargs, kwargs)
def register_to_receive(self, RfType)
def __init__(self, vargs, kwargs)