Skip to content
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions metasyn/distribution/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ def _param_schema(cls):
"probs": {"type": "array", "items": {"type": "number"}},
}

@property
def _params_formatted(self) -> str:
n_categories = len(self.labels)
top_bottom_counts = (7, 3)
Comment thread
MSVermet marked this conversation as resolved.
Resolved
if n_categories <= top_bottom_counts[0]:
Comment thread
MSVermet marked this conversation as resolved.
Resolved
return super()._params_formatted
sorted_indices = np.argsort(-self.probs)
top_indices = sorted_indices[:top_bottom_counts[0]]
bottom_indices = sorted_indices[-top_bottom_counts[1]:]
top_labels = ", ".join(repr(label) for label in self.labels[top_indices].tolist())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the repr(label) instead of just label?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess because it's a np._str?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make sure if label is not a string it can be joined. Not sure if this could ever happen, but just in case.

top_probs = ", ".join(f"{prob:.3f}" for prob in self.probs[top_indices])
bottom_labels = ", ".join(repr(label) for label in self.labels[bottom_indices].tolist())
bottom_probs = ", ".join(f"{prob:.3f}" for prob in self.probs[bottom_indices])
return (
f"\t- labels: [{top_labels}, ..., {bottom_labels}]\n"
f"\t- probs: [{top_probs}, ..., {bottom_probs}]\n"
f"\tShowing {top_bottom_counts[0]+top_bottom_counts[1]} of {n_categories} categories"
)

def draw(self):
return np.random.choice(self.labels, p=self.probs)

Expand Down
Loading