Skip to content

Commit

Permalink
closes #149. Removed default loggers (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
elphick authored May 14, 2024
1 parent 01b1771 commit f4043bc
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 15 deletions.
9 changes: 9 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
Mass_Composition 0.4.11 (2024-05-14)
====================================

Other Tasks
-----------

- Removed default loggers. (#149)


Mass_Composition 0.4.10 (2024-05-13)
====================================

Expand Down
7 changes: 6 additions & 1 deletion elphick/mass_composition/mass_composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,12 +1012,17 @@ def __truediv__(self, other: 'MassComposition') -> 'MassComposition':

return res

def __eq__(self, other):
if isinstance(other, MassComposition):
return self.__dict__ == other.__dict__
return False

@staticmethod
def _check_cols_in_data_cols(cols: List[str], cols_data: List[str]):
for col in cols:
if (col is not None) and (col not in cols_data):
msg: str = f"{col} not in the data columns: {cols_data}"
logging.error(msg)
logging.getLogger(__name__).error(msg)
raise IndexError(msg)

@staticmethod
Expand Down
3 changes: 2 additions & 1 deletion elphick/mass_composition/mc_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

class Status:
def __init__(self, df_oor: pd.DataFrame):
self._logger = logging.getLogger(__name__)
self.oor: pd.DataFrame = df_oor
self.num_oor: int = len(df_oor)
self.failing_components: List[str] = list(df_oor.dropna(axis=1).columns) if self.num_oor > 0 else None

@property
def ok(self) -> bool:
if self.num_oor > 0:
logging.warning(f'{self.num_oor} OOR records exist.')
self._logger.warning(f'{self.num_oor} OOR records exist.')
return True if self.num_oor == 0 else False

def __str__(self) -> str:
Expand Down
6 changes: 3 additions & 3 deletions elphick/mass_composition/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,13 +978,13 @@ def _check_indexes(cls, streams):
s: str = stream.name
tmp_nans: pd.Series = stream_nans.query('stream==@s').sum(axis=1)
if tmp_nans.iloc[0] > 0:
logging.debug(f'The {s} stream has missing coarse sizes')
logger.debug(f'The {s} stream has missing coarse sizes')
first_zero_index = tmp_nans.loc[tmp_nans == 0].index[0]
if tmp_nans[tmp_nans.index <= first_zero_index].sum() > 0:
logging.debug(f'The {s} stream has missing sizes requiring interpolation')
logger.debug(f'The {s} stream has missing sizes requiring interpolation')
raise NotImplementedError('Coming soon - we need interpolation!')
else:
logging.debug(f'The {s} stream has missing coarse sizes only')
logger.debug(f'The {s} stream has missing coarse sizes only')
stream_df = df_streams_full.loc[:, (slice(None), s)].droplevel(-1, axis=1).fillna(0)
# recreate the stream from the dataframe
stream.set_data(stream_df)
Expand Down
27 changes: 18 additions & 9 deletions elphick/mass_composition/utils/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@
from elphick.mass_composition.utils.parallel import TqdmParallel
from elphick.mass_composition.utils.pd_utils import column_prefix_counts, column_prefixes

logger = logging.getLogger(__name__)


def create_mass_composition(stream_data: Tuple[Union[int, str], pd.DataFrame],
interval_edges: Optional[Union[Iterable, int]] = None) -> Tuple[
Union[int, str], MassComposition]:
Union[int, str], MassComposition]:
stream, data = stream_data
if interval_edges is not None:
return stream, MassComposition(data=data, name=stream).resample_1d(interval_edges=interval_edges)
else:
return stream, MassComposition(data=data, name=stream)
res = None
try:
if interval_edges is not None:
res = stream, MassComposition(data=data, name=stream).resample_1d(interval_edges=interval_edges)
else:
res = stream, MassComposition(data=data, name=stream)
except Exception as e:
logger.error(f"Error creating MassComposition object for {stream}: {e}")

return res


def streams_from_dataframe(df: pd.DataFrame,
Expand All @@ -40,11 +48,10 @@ def streams_from_dataframe(df: pd.DataFrame,
Returns:
"""
logger: logging.Logger = logging.getLogger(__name__)

stream_data: Dict[str, pd.DataFrame] = {}
index_names: List[str] = []
if mc_name_col:
logger.debug("Creating MassComposition objects by name column.")
if mc_name_col in df.index.names:
index_names = df.index.names
df.reset_index(mc_name_col, inplace=True)
Expand All @@ -58,6 +65,7 @@ def streams_from_dataframe(df: pd.DataFrame,
df.reset_index(inplace=True)
df.set_index(index_names, inplace=True)
else:
logger.debug("Creating MassComposition objects by column prefixes.")
# wide case - find prefixes where there are at least 3 columns
prefix_counts = column_prefix_counts(df.columns)
prefix_cols = column_prefixes(df.columns)
Expand All @@ -69,6 +77,7 @@ def streams_from_dataframe(df: pd.DataFrame,
columns={col: col.replace(f'{prefix}_', '') for col in df.columns})

if interval_edges is not None:
logger.debug("Resampling MassComposition objects to new interval edges.")
# unify the edges - this will also interp missing grades
if not isinstance(df.index, pd.IntervalIndex):
raise NotImplementedError(f"The index `{df.index}` of the dataframe is not a pd.Interval. "
Expand All @@ -82,8 +91,8 @@ def streams_from_dataframe(df: pd.DataFrame,
indx = pd.IntervalIndex.from_arrays(left=all_edges[0:-1], right=all_edges[1:])
interval_edges = _upsample_grid_by_factor(indx=indx, factor=interval_edges)

with TqdmParallel(desc="Creating MassComposition objects", n_jobs=n_jobs, prefer="processes",
total=len(stream_data)) as p:
with TqdmParallel(desc="Creating MassComposition objects", n_jobs=n_jobs,
prefer=None, total=len(stream_data)) as p:
res = p(delayed(create_mass_composition)(stream_data, interval_edges) for stream_data in stream_data.items())
res = dict(res)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mass-composition"
version = "0.4.10"
version = "0.4.11"
description = "For managing multi-dimensional mass-composition datasets, supporting weighted mathematical operations and visualisation."
authors = ["Greg <greg@elphick.com.au>"]
packages = [{ include = "elphick/mass_composition" }]
Expand Down

0 comments on commit f4043bc

Please sign in to comment.