Hierarchical Clustering Strategy¶
hierarchical_clustering_strategy ¶
Hierarchical Clustering Strategy - Dendrogram Visualizations.
This module implements the HierarchicalClusteringStrategy for creating dendrogram visualizations using scipy hierarchical clustering and plotly.
Classes:
| Name | Description |
|---|---|
HierarchicalClusteringStrategy | Strategy for hierarchical clustering dendrogram generation. |
Notes
- Supports multiple distance metrics (jaccard, euclidean, cosine, etc.)
- Supports multiple linkage methods (average, complete, single, ward)
- Creates left-oriented dendrograms with sample labels
For supported use cases, refer to the official documentation.
Classes¶
HierarchicalClusteringStrategy ¶
Bases: BasePlotStrategy
Strategy for hierarchical clustering dendrogram visualizations.
This strategy creates dendrograms showing hierarchical relationships between samples based on binary KO presence/absence patterns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config | Dict[str, Any] | Complete configuration from YAML file. | required |
Attributes:
| Name | Type | Description |
|---|---|---|
data_config | Dict[str, Any] | Data processing configuration. |
plotly_config | Dict[str, Any] | Plotly-specific configuration. |
Methods:
| Name | Description |
|---|---|
validate_data | Validate input data for hierarchical clustering requirements |
process_data | Process binary matrix for hierarchical clustering |
create_figure | Create dendrogram figure from binary matrix |
apply_filters | Apply clustering parameters (metric, method) |
Notes
- Supports multiple distance metrics and linkage methods
- Validates Ward/Euclidean compatibility
- Creates left-oriented dendrograms
Initialize strategy with configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config | Dict[str, Any] | Complete configuration from YAML file. | required |
Source code in src/domain/plot_strategies/charts/hierarchical_clustering_strategy.py
Functions¶
validate_data ¶
Validate input data for hierarchical clustering requirements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df | DataFrame | Binary presence/absence matrix (samples × KOs). | required |
Raises:
| Type | Description |
|---|---|
ValueError | If DataFrame is empty, has fewer than 2 samples, or contains non-binary values. |
Source code in src/domain/plot_strategies/charts/hierarchical_clustering_strategy.py
process_data ¶
Process binary matrix for hierarchical clustering.
Ensures strict binary values (0 or 1) and returns matrix ready for clustering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df | DataFrame | Binary presence/absence matrix (samples × KOs). | required |
Returns:
| Type | Description |
|---|---|
DataFrame | Binary matrix ready for scipy linkage. |
Source code in src/domain/plot_strategies/charts/hierarchical_clustering_strategy.py
create_figure ¶
Create dendrogram figure from binary matrix.
Uses scipy hierarchical clustering and creates a plotly dendrogram visualization with configured parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
processed_df | DataFrame | Binary presence/absence matrix (samples × KOs). | required |
Returns:
| Type | Description |
|---|---|
Figure | Configured Plotly dendrogram. |
Source code in src/domain/plot_strategies/charts/hierarchical_clustering_strategy.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
apply_filters ¶
Apply clustering parameters to strategy.
Stores clustering parameters (metric, method) for use in figure creation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df | DataFrame | Binary presence/absence matrix. | required |
filters | Dict[str, Any] | Clustering parameters (metric, method). | required |
Returns:
| Type | Description |
|---|---|
DataFrame | Unchanged matrix (parameters stored internally). |
Source code in src/domain/plot_strategies/charts/hierarchical_clustering_strategy.py
apply_customizations ¶
Apply custom styling to figure.
This is a hook for future customization features (FLEXIVEL and FLEXIVEL2).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig | Figure | Base figure. | required |
customizations | Optional[Any] | Customization specifications. | None |
Returns:
| Type | Description |
|---|---|
Figure | Customized figure. |
Source code in src/domain/plot_strategies/base/base_plot_strategy.py
generate_plot ¶
generate_plot(data: DataFrame, filters: Optional[Dict[str, Any]] = None, customizations: Optional[Any] = None) -> go.Figure
Generate complete plot (Template Method).
This method orchestrates the entire plot generation process: 1. Validate input data 2. Process data 3. Apply filters 4. Create figure 5. Apply customizations
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | DataFrame | Input data. | required |
filters | Optional[Dict[str, Any]] | Filters to apply. | None |
customizations | Optional[Any] | Customizations to apply. | None |
Returns:
| Type | Description |
|---|---|
Figure | Complete Plotly figure. |
Raises:
| Type | Description |
|---|---|
ValueError | If validation fails. |