Skip to content

Commit

Permalink
last chance fix for coerce_estimates. bug-fix for clip_composition.
Browse files Browse the repository at this point in the history
  • Loading branch information
elphick committed Nov 4, 2024
1 parent 915266d commit 640e0fe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 4 additions & 2 deletions elphick/geomet/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,14 @@ def clip_composition(self, ranges: Optional[dict[str, list[float]]] = None, epsi
affected_indexes = set()
for component, component_range in component_ranges.items():
before_clip = self._mass_data[component].copy()
# add a small value to the range to ensure the clipped values lie marginally inside the specified range.
component_range = [component_range[0] + epsilon, component_range[1] - epsilon]
# define the component mass that aligns with the lower and upper bounds
component_mass_limits = self._mass_data[self.mass_dry_var].values[:, np.newaxis] * np.array(
component_range) / self.composition_factor
# apply the clip to the mass data
self._mass_data[component] = self._mass_data[component].clip(lower=component_mass_limits[:, 0] + epsilon,
upper=component_mass_limits[:, 1] - epsilon)
self._mass_data[component] = self._mass_data[component].clip(lower=component_mass_limits[:, 0],
upper=component_mass_limits[:, 1])
affected_indexes.update(self._mass_data.index[before_clip != self._mass_data[component]])

# log the action, including the first 50 indexes affected
Expand Down
15 changes: 14 additions & 1 deletion elphick/geomet/utils/estimates.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ def coerce_estimates(estimate_stream: Stream, input_stream: Stream,
name=f"{fs_name}: Balance prior to coercion")
fs.table_plot(plot_type='network', table_area=0.2, table_pos='top').show()

# # debugging snippet to show a failing record
# qry: str = 'index==1000'
# fs_debug: Flowsheet = fs.query(qry)
# fs_debug.name = f"{fs_name}: Balance prior to coercion: [{qry}]"
# fs_debug.table_plot(plot_type='network', table_area=0.2, table_pos='top').show()

if input_stream.status.ok is False:
raise ValueError('Input stream is not OK')

Expand Down Expand Up @@ -78,7 +84,14 @@ def coerce_estimates(estimate_stream: Stream, input_stream: Stream,
include_supplementary_data=True)

if estimate_stream.status.ok is False:
raise ValueError('Estimate stream is not OK after adjustment')
# This can occur in cases where the complement grade has been reduced (by balance_composition) to a point
# where the resultant estimate grade is out of range. In this case, we need to adjust the complement grade.

estimate_stream = estimate_stream.clip_composition()
complement_stream = input_stream.sub(estimate_stream, name=complement_name)

if estimate_stream.status.ok is False:
raise ValueError('Estimate stream is not OK after adjustment')

fs2: Flowsheet = Flowsheet.from_objects([input_stream, estimate_stream, complement_stream],
name=f"{fs_name}: Coerced Estimates")
Expand Down

0 comments on commit 640e0fe

Please sign in to comment.