Skip to content

Commit

Permalink
Expose option to downscale resolution of individual joyplots for cont…
Browse files Browse the repository at this point in the history
…inuous distribution plots
  • Loading branch information
nnansters committed Jan 17, 2024
1 parent 60a2597 commit 3cbb56e
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions nannyml/distribution/continuous/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def __init__(
chunk_number: Optional[int] = None,
chunk_period: Optional[str] = None,
chunker: Optional[Chunker] = None,
points_per_joy_plot: Optional[int] = None,
):
super().__init__(

Check warning on line 27 in nannyml/distribution/continuous/calculator.py

View check run for this annotation

Codecov / codecov/patch

nannyml/distribution/continuous/calculator.py#L27

Added line #L27 was not covered by tests
chunk_size,
Expand All @@ -33,6 +34,7 @@ def __init__(

self.column_names = column_names if isinstance(column_names, List) else [column_names]
self.result: Optional[Result] = None
self.points_per_joy_plot = points_per_joy_plot

Check warning on line 37 in nannyml/distribution/continuous/calculator.py

View check run for this annotation

Codecov / codecov/patch

nannyml/distribution/continuous/calculator.py#L35-L37

Added lines #L35 - L37 were not covered by tests

def _fit(self, reference_data: pd.DataFrame, *args, **kwargs) -> Self:
self.result = self._calculate(reference_data)
Expand All @@ -50,7 +52,10 @@ def _calculate(self, data: pd.DataFrame, *args, **kwargs) -> Result:

for column in self.column_names:
column_distributions_per_chunk = calculate_chunk_distributions(

Check warning on line 54 in nannyml/distribution/continuous/calculator.py

View check run for this annotation

Codecov / codecov/patch

nannyml/distribution/continuous/calculator.py#L54

Added line #L54 was not covered by tests
data[column], self.chunker, data.get(self.timestamp_column_name, default=None)
data[column],
self.chunker,
data.get(self.timestamp_column_name, default=None),
points_per_joy_plot=self.points_per_joy_plot,
)
column_distributions_per_chunk.drop(columns=['key', 'chunk_index'], inplace=True)

Check warning on line 60 in nannyml/distribution/continuous/calculator.py

View check run for this annotation

Codecov / codecov/patch

nannyml/distribution/continuous/calculator.py#L60

Added line #L60 was not covered by tests
for c in column_distributions_per_chunk.columns:
Expand Down Expand Up @@ -83,16 +88,16 @@ def _get_kde(array, cut=3, clip=(-np.inf, np.inf)):
return None

Check warning on line 88 in nannyml/distribution/continuous/calculator.py

View check run for this annotation

Codecov / codecov/patch

nannyml/distribution/continuous/calculator.py#L87-L88

Added lines #L87 - L88 were not covered by tests


def _get_kde_support(kde):
def _get_kde_support(kde, points_per_joy_plot: Optional[int] = None):
if kde is not None: # pragma: no cover
return kde.support[::5]
return kde.support[:: (len(kde.support) // (points_per_joy_plot or 50))]
else:
return np.array([])

Check warning on line 95 in nannyml/distribution/continuous/calculator.py

View check run for this annotation

Codecov / codecov/patch

nannyml/distribution/continuous/calculator.py#L95

Added line #L95 was not covered by tests


def _get_kde_density(kde):
def _get_kde_density(kde, points_per_joy_plot: Optional[int] = None):
if kde is not None: # pragma: no cover
return kde.density[::5]
return kde.density[:: (len(kde.support) // (points_per_joy_plot or 50))]
else:
return np.array([])

Check warning on line 102 in nannyml/distribution/continuous/calculator.py

View check run for this annotation

Codecov / codecov/patch

nannyml/distribution/continuous/calculator.py#L102

Added line #L102 was not covered by tests

Expand Down Expand Up @@ -124,6 +129,7 @@ def calculate_chunk_distributions(
kde_cut=3,
kde_clip=(-np.inf, np.inf),
post_kde_clip=None,
points_per_joy_plot: Optional[int] = None,
):
if isinstance(data, np.ndarray):
data = pd.Series(data, name='data')

Check warning on line 135 in nannyml/distribution/continuous/calculator.py

View check run for this annotation

Codecov / codecov/patch

nannyml/distribution/continuous/calculator.py#L135

Added line #L135 was not covered by tests
Expand Down Expand Up @@ -152,8 +158,8 @@ def calculate_chunk_distributions(
.reset_index()
)

data['kde_support'] = data['kde'].apply(lambda kde: _get_kde_support(kde))
data['kde_density'] = data['kde'].apply(lambda kde: _get_kde_density(kde))
data['kde_support'] = data['kde'].apply(lambda kde: _get_kde_support(kde, points_per_joy_plot))
data['kde_density'] = data['kde'].apply(lambda kde: _get_kde_density(kde, points_per_joy_plot))
data['kde_cdf'] = data[['kde_support', 'kde_density']].apply(
lambda row: _get_kde_cdf(row['kde_support'], row['kde_density'] if len(row['kde_support']) > 0 else []),
axis=1,
Expand Down

0 comments on commit 3cbb56e

Please sign in to comment.