added 6 more filters

This commit is contained in:
2026-02-03 15:15:59 +01:00
parent 2817ed240a
commit 081fb0dd6e
5 changed files with 172 additions and 28 deletions

View File

@@ -150,36 +150,57 @@ combinations.append({
## Adding a New Filter Dimension
To add an entirely new filter dimension (e.g., a new demographic question), edit **only** `FILTER_CONFIG` in `03_quant_report.script.py`:
To add an entirely new filter dimension (e.g., a new demographic question), you need to update several files:
### Checklist
1. **Ensure `QualtricsSurvey`** has the corresponding `options_*` attribute and `filter_data()` accepts the parameter
2. **Open** `03_quant_report.script.py`
3. **Find** `FILTER_CONFIG` near the top of the file:
1. **Update `utils.py` — `QualtricsSurvey.__init__()`** to initialize the filter state attribute:
```python
FILTER_CONFIG = {
'age': 'options_age',
'gender': 'options_gender',
'ethnicity': 'options_ethnicity',
'income': 'options_income',
'consumer': 'options_consumer',
# Add new filters here: 'newfilter': 'options_newfilter',
}
# In __init__(), add after existing filter_ attributes (around line 758):
self.filter_region:list = None # QID99
```
4. **Add** your new filter:
2. **Update `utils.py` — `load_data()`** to populate the `options_*` attribute:
```python
# In load_data(), add after existing options:
self.options_region = sorted(df['QID99'].drop_nulls().unique().to_list()) if 'QID99' in df.columns else []
```
3. **Update `utils.py` — `filter_data()`** to accept and apply the filter:
```python
# Add parameter to function signature:
def filter_data(self, q: pl.LazyFrame, ..., region:list=None) -> pl.LazyFrame:
# Add filter logic in function body:
self.filter_region = region
if region is not None:
q = q.filter(pl.col('QID99').is_in(region))
```
4. **Update `plots.py` — `_get_filter_slug()`** to include the filter in directory slugs:
```python
# Add to the filters list:
('region', 'Reg', getattr(self, 'filter_region', None), 'options_region'),
```
5. **Update `plots.py` — `_get_filter_description()`** for human-readable descriptions:
```python
# Add to the filters list:
('Region', getattr(self, 'filter_region', None), 'options_region'),
```
6. **Update `03_quant_report.script.py` — `FILTER_CONFIG`**:
```python
FILTER_CONFIG = {
'age': 'options_age',
'gender': 'options_gender',
'ethnicity': 'options_ethnicity',
'income': 'options_income',
'consumer': 'options_consumer',
# ... existing filters ...
'region': 'options_region', # ← New filter
}
```
@@ -190,4 +211,29 @@ This **automatically**:
- Passes it to `S.filter_data()`
- Writes it to the `.txt` filter description file
5. **Update** `run_filter_combinations.py` to generate combinations for the new filter (optional)
7. **Update `run_filter_combinations.py`** to generate combinations (optional):
```python
# Add after existing filter loops:
for region in survey.options_region:
combinations.append({
'name': f'Region-{region}',
'filters': {'region': [region]}
})
```
### Currently Available Filters
| CLI Argument | Options Attribute | QID Column | Description |
|--------------|-------------------|------------|-------------|
| `--age` | `options_age` | QID1 | Age groups |
| `--gender` | `options_gender` | QID2 | Gender |
| `--ethnicity` | `options_ethnicity` | QID3 | Ethnicity |
| `--income` | `options_income` | QID15 | Income brackets |
| `--consumer` | `options_consumer` | Consumer | Consumer segments |
| `--business_owner` | `options_business_owner` | QID4 | Business owner status |
| `--employment_status` | `options_employment_status` | QID13 | Employment status |
| `--personal_products` | `options_personal_products` | QID14 | Personal products |
| `--ai_user` | `options_ai_user` | QID22 | AI user status |
| `--investable_assets` | `options_investable_assets` | QID16 | Investable assets |
| `--industry` | `options_industry` | QID17 | Industry |