ClassBalance
Introduction
ClassBalance is a widget in Supervisely that allows for displaying input data classes balance on the UI. For example, you can display the distribution of tags to different classes in the project, or set your data according to the required format. It also provides functionality for data streaming and dynamic updates, allowing the class balance to display real-time data. Additionally, users can control the widget through Python code by detecting events such as clicking on a class name or segment data.
Function signature
ClassBalance(
segments=[],
rows_data=[],
slider_data={},
max_value=None,
max_height=350,
rows_height=100,
selectable=True,
collapsable=False,
clickable_name=False,
clickable_segment=False,
widget_id=None,
)Example of input data we will use. If max_value is None, the maximum total form rows_data will be taken as max_value.
Parameters
segments
List[Dict]
List of segments in the widget
rows_data
List[Dict]
List of rows data in the widget
slider_data
Dict
Dict containing ClassBalance slider images
max_value
int
The maximum value of the row input data "total" field
max_height
int
Specifies the maximum height of the ClassBalance
rows_height
int
Specifies the height of the ClassBalance rows
selectable
bool
Determines if rows can be selected
collapsable
bool
Determines whether a rows data can be collapsed
clickable_name
bool
Allows clicking on class names
clickable_segment
bool
Allows clicking on class segments
widget_id
str
ID of the widget
segments
List of segments in the widget.
type: List[Dict]
default value: []
rows_data
List of rows data in the widget.
type: List[Dict]
default value: []
slider_data
Dict containing images for row images sliders. It needs collapsable=True to be set.
type: Dict
default value: {}
max_value
The maximum value of the row input data "total" field. If max_value is None, the maximum total form rows_data will be taken as max_value.
type: int
default value: None
max_height
Specifies the maximum height of the ClassBalance.
type: int
default value: 350
rows_height
Specifies the height of the ClassBalance rows.
type: int
default value: 100
selectable
Determines whether a collapse button is displayed.
type: bool
default value: True

collapsable
Display the collapse button. The case collapsable=True has been shown above to show examples for changing the max_height parameter. So now an example will be shown for case collapsable=False.
type: bool
default value: False

clickable_name
Allows clicking on class names.
type: bool
default value: False
clickable_segment
Allow clicking on class segments.
type: bool
default value: False
Methods and attributes
is_selectable
Returns True if ClassBalance rows is selectable, otherwise False
is_collapsable
Returns True if ClassBalance rows is collapsable, otherwise False
is_clickable_name
Returns True if ClassBalance rows name is clickable, otherwise False
is_clickable_segment
Returns True if ClassBalance rows segment is clickable, otherwise False
get_max_value()
Returns the maximum value of ClassBalance rows
set_max_value(value: int)
Sets the maximum value of ClassBalance rows
get_max_height()
Return ClassBalance max height.
set_max_height(value: int)
Sets the maximum height of ClassBalance
get_segments()
Return ClassBalance segments
add_segments(segments: List[Dict])
Add new segments to existing in ClassBalance
set_segments(segments: List[Dict])
Set new segments in ClassBalance
get_rows_data()
Return ClassBalance rows_data
add_rows_data(rows_data: List[Dict])
Add new rows_data to now existing in ClassBalance
set_rows_data(rows_data: List[Dict])
Set new rows_data in ClassBalance
get_slider_data()
Return ClassBalance slider_data
add_slider_data(slider_data: Dict)
Add new slider_data to now existing in ClassBalance
set_slider_data(slider_data: Dict)
Set new slider_data in ClassBalance
get_selected_rows()
Return list of ClassBalance selected rows
@click
Decorator function to handle clicks on class names or rows segments
Mini App Example
You can find this example in our Github repository:
supervisely-ecosystem/ui-widgets-demos/compare-data/004_class_balance/src/main.py
Import libraries
Init API client
First, we load environment variables with credentials and init API for communicating with Supervisely Instance:
Example 1. A simple example of using ClassBalance widget.
ClassBalance widget.Prepare series for class balance
Initialize ClassBalance
Create card widget with first ClassBalance widget content
Example 2. Advanced using ClassBalance widget.
ClassBalance widget.👍 In this example, we will iterate through sample dataset images from the Supervisely platform and crop them based on object classes to set in the image slider. Finally, we will calculate and collect statistics for each class and display the class balance information.
Create static dir
Create a static directory to store the cropped images in local directory. Or you can upload them to the Team Files and use the full_storage_url to display them in the image slider.
Get environment variables
Get ProjectMeta and DatasetInfo from the server
Get all image infos, annotations and download images from server
Calculate and collect series for the ClassBalance widget
Initialize ClassBalance
Create additional widgets
Create app layout
Prepare a layout for the app using Card widget with the content parameter.
Create app using layout
Create an app object with the layout and static_dir parameters.
Add functions to control widgets from python code
Last updated
Was this helpful?