API Reference
reddwarf.implementations.base
reddwarf.implementations.polis.run_pipeline(**kwargs)
Source code in reddwarf/implementations/polis.py
10 11 12 13 14 15 16 |
|
reddwarf.implementations.polis
reddwarf.implementations.polis.run_pipeline(**kwargs)
Source code in reddwarf/implementations/polis.py
10 11 12 13 14 15 16 |
|
reddwarf.sklearn
Various custom Scikit-Learn estimators to mimick aspects of Polis, suitable for use in Scikit-Learn workflows, pipelines, and APIs.
reddwarf.sklearn.cluster.PolisKMeans
Bases: KMeans
A modified version of scikit-learn's KMeans that allows partial initialization with user-supplied cluster centers and custom fallback strategies.
This subclass extends sklearn.cluster.KMeans
with additional features
around centroid initialization. Outside the behavior documented, it retains
all other parameters and behavior from the base KMeans implementation.
Parameters: |
|
---|
Attributes: |
|
---|
See Also
sklearn.cluster.KMeans
: Original implementation with full parameter list.
Source code in reddwarf/sklearn/cluster.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
|
reddwarf.sklearn.cluster.PolisKMeansDownsampler
Bases: BaseEstimator
, TransformerMixin
A transformer that fits PolisKMeans
and returns the cluster centers as the
downsampled dataset.
This will support mimicking "base clusters" from the Polis platform.
This enables use in sklearn pipelines, where intermediate steps
are expected to implement both fit
and transform
.
Source code in reddwarf/sklearn/cluster.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
reddwarf.sklearn.model_selection.GridSearchNonCV
Bases: GridSearchCV
sklearn.model_selection.GridSearchCV
, but modified to score against the
full dataset (ie. not cross-validated).
Normally, GridSearchCV
splits up the X
data and scores each "fold" of data.
This is identical, but we automatically use the full dataset in each fold.
Source code in reddwarf/sklearn/model_selection.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
reddwarf.sklearn.transformers.SparsityAwareScaler
Bases: BaseEstimator
, TransformerMixin
Scale projected points (participant or statements) based on sparsity of vote matrix, to account for any small number of votes by a participant and prevent those participants from bunching up in the center.
Attributes: |
|
---|
Source code in reddwarf/sklearn/transformers.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
reddwarf.utils.matrix
reddwarf.utils.matrix.generate_raw_matrix(votes, cutoff=None)
Generates a raw vote matrix from a list of vote records.
See filter_votes
method for details of cutoff
arg.
Parameters: |
|
---|
Returns: |
|
---|
Source code in reddwarf/utils/matrix.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
|
reddwarf.utils.matrix.simple_filter_matrix(vote_matrix, mod_out_statement_ids=[])
The simple filter on the vote_matrix that is used by Polis prior to running PCA.
Parameters: |
|
---|
Returns: |
|
---|
Source code in reddwarf/utils/matrix.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
|
reddwarf.utils.matrix.get_clusterable_participant_ids(vote_matrix, vote_threshold)
Find participant IDs that meet a vote threshold in a vote_matrix.
Parameters: |
|
---|
Returns: |
|
---|
Source code in reddwarf/utils/matrix.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
reddwarf.utils.reducer
reddwarf.utils.reducer.base.run_reducer(vote_matrix, reducer='pca', n_components=2, **reducer_kwargs)
Process a prepared vote matrix to be imputed and return participant and (optionally) statement data, projected into reduced n-dimensional space.
The vote matrix should not yet be imputed, as this will happen within the method.
Parameters: |
|
---|
Returns: |
|
---|
Source code in reddwarf/utils/reducer/base.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
reddwarf.utils.reducer.base.get_reducer(reducer='pca', n_components=2, random_state=None, **reducer_kwargs)
Source code in reddwarf/utils/reducer/base.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
reddwarf.utils.clusterer
reddwarf.utils.clusterer.base.run_clusterer(X_participants_clusterable, clusterer='kmeans', force_group_count=None, max_group_count=5, init_centers=None, random_state=None, **clusterer_kwargs)
Source code in reddwarf/utils/clusterer/base.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
reddwarf.utils.clusterer.kmeans.find_best_kmeans(X_to_cluster, k_bounds=[2, 5], init='k-means++', init_centers=None, random_state=None)
Use silhouette scores to find the best number of clusters k to assume to fit the data.
Parameters: |
|
---|
Returns: |
|
---|
Source code in reddwarf/utils/clusterer/kmeans.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
|
reddwarf.utils.consensus
reddwarf.utils.consensus.select_consensus_statements(vote_matrix, mod_out_statement_ids=[], pick_max=5, prob_threshold=0.5, confidence=0.9)
Select consensus statements from a given vote matrix.
Parameters: |
|
---|
Returns: |
|
---|
Source code in reddwarf/utils/consensus.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
reddwarf.utils.stats
reddwarf.utils.stats.select_representative_statements(grouped_stats_df, mod_out_statement_ids=[], pick_max=5, confidence=0.9)
Selects statistically representative statements from each group cluster.
This is expected to match the Polis outputs when all defaults are set.
Parameters: |
|
---|
Returns: |
|
---|
Source code in reddwarf/utils/stats.py
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 |
|
reddwarf.utils.stats.calculate_comment_statistics(vote_matrix, cluster_labels=None, pseudo_count=1)
Calculates comparative statement statistics across all votes and groups, using only efficient numpy operations.
Note: when no cluster_labels are supplied, we internally apply the group 0
to each row,
and calculated values can be accessed in the first group index.
The representativeness metric is defined as: R_v(g,c) = P_v(g,c) / P_v(~g,c)
Where: - P_v(g,c) is probability of vote v on comment c in group g - P_v(~g,c) is probability of vote v on comment c in all groups except g
And: - N(g,c) is the total number of non-missing votes on comment c in group g - N_v(g,c) is the total number of vote v on comment c in group g
Parameters: |
|
---|
Returns: |
|
---|
Source code in reddwarf/utils/stats.py
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 303 304 305 306 307 308 309 310 |
|
reddwarf.utils.stats.calculate_comment_statistics_dataframes(vote_matrix, cluster_labels=None, pseudo_count=1)
Calculates comparative statement statistics across all votes and groups, generating dataframes.
This returns both group-specific statistics, and also overall stats (group-aware consensus).
Parameters: |
|
---|
Returns: |
|
---|
Source code in reddwarf/utils/stats.py
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
|
reddwarf.utils
(These are in the process of being either moved or deprecated.)
reddwarf.utils.filter_votes(votes, cutoff=None)
Filters a list of votes.
If a cutoff
is provided, votes are filtered based on either:
- An
int
representing unix timestamp (ms), keeping only votes before or at that time.- Any int above 13_000_000_000 is considered a timestamp.
- Any other positive or negative
int
is considered an index, reflecting where to trim the time-sorted vote list.- positive: filters in votes that many indices from start
- negative: filters out votes that many indices from end
Parameters: |
|
---|
Returns: |
|
---|
Source code in reddwarf/utils/matrix.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
reddwarf.utils.filter_matrix(vote_matrix, min_user_vote_threshold=7, active_statement_ids=[], keep_participant_ids=[], unvoted_filter_type='drop')
Generates a filtered vote matrix from a raw matrix and filter config.
Parameters: |
|
---|
Returns: |
|
---|
Source code in reddwarf/utils/matrix.py
154 155 156 157 158 159 160 161 162 163 164 165 166 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 |
|
reddwarf.utils.get_unvoted_statement_ids(vote_matrix)
A method intended to be piped into a VoteMatrix DataFrame, returning list of unvoted statement IDs.
See: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.pipe.html
Parameters: |
|
---|
Returns: |
|
---|
Example:
unused_statement_ids = vote_matrix.pipe(get_unvoted_statement_ids)
Source code in reddwarf/utils/matrix.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
reddwarf.data_presenter
reddwarf.data_presenter.generate_figure(coord_data, coord_labels=None, cluster_labels=None, flip_x=False, flip_y=False)
Generates a matplotlib scatterplot with optional bounded clusters.
The plot is drawn from a dataframe of xy values, each point labelled by index participant_id
.
When a list of cluster labels are supplied (corresponding to each row), concave hulls are drawn around them.
Signs of PCA projection coordinates are arbitrary, and can flip without meaning. Inverting axes can help compare results with Polis platform visualizations.
Parameters: |
|
---|
Returns: |
|
---|
Source code in reddwarf/data_presenter.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
reddwarf.data_presenter.generate_figure_polis(result, show_guesses=False, flip_x=True, flip_y=False, show_pids=True)
Generate a Polis-style visualization from clustering results.
Parameters: |
|
---|
Source code in reddwarf/data_presenter.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
Types
reddwarf.implementations.base.PolisClusteringResult
dataclass
Attributes: |
|
---|
Source code in reddwarf/implementations/base.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|