flywhl / flywhl/vyper

feat: load and replace parameters from YAML

Open
#1 1 comment 0 reactions 0 assignees Claimed by @rorybyrne View on GitHub
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Description

**BEFORE**:
```python
from pydantic import BaseModel

class MyExperiment(BaseModel):
'''Training a neural network to do a thing.'''

# identifier after @ indicates a YAML file
network: Network@spk-vih[
# modifications to the YAML contents
shape[1] = 500
layers[1].populations.e.kind = adaptive-LIF
]
loss: Loss@mse
optimizer: Optimizer@adam
dataset: Dataset@default
```

**AFTER**:
```python
from pydantic import BaseModel
from typing import ClassVar, Dict, Any
import yaml
from pathlib import Path

class MyExperiment(BaseModel):
'''Training a neural network to do a thing.'''

# Static storage of YAML modifications
_yaml_mods: ClassVar[Dict[str, Dict[str, Any]]] = {
'network': {
'yaml_id': 'spk-vih',
'modifications': [
('shape[1]', 500),
('layers[1].populations.e.kind', 'adaptive-LIF')
]
},
'loss': {
'yaml_id': 'mse',
'modifications': []
},
'optimizer': {
'yaml_id': 'adam',
'modifications': []
},
'dataset': {
'yaml_id': 'default',
'modifications': []
}
}

# The actual instance fields keep their original types
network: Network # Original type preserved
loss: Loss
optimizer: Optimizer
dataset: Dataset

@classmethod
def load(cls, yaml_dir: str = '.') -> 'MyExperiment':
"""Load and construct instance with YAML data."""
yaml_data = {}

for field, yaml_info in cls._yaml_mods.items():
# Load base YAML
yaml_path = Path(yaml_dir) / f"{yaml_info['yaml_id']}.yaml"
with open(yaml_path) as f:
field_data = yaml.safe_load(f)

# Apply modifications
for path, value in yaml_info['modifications']:
# Split path into parts (handle both dot notation and array indices)
parts = []
current = ''
in_bracket = False
for char in path:
if char == '[':
if current:
parts.append(current)
current = ''
in_bracket = True
elif char == ']':
if current:
parts.append(int(current))
current = ''
in_bracket = False
elif char == '.' and not in_bracket:
if current:
parts.append(current)
current = ''
else:
current += char
if current:
parts.append(current)

# Navigate to target and set value
target = field_data
for part in parts[:-1]:
if isinstance(part, int):
while len(target) <= part:
target.append({})
target = target[part]
else:
if part not in target:
target[part] = {}
target = target[part]

last = parts[-1]
if isinstance(last, int):
while len(target) <= last:
target.append(None)
target[last] = value

yaml_data[field] = field_data

return cls(**yaml_data)
```

Contributor guide

No contributing guide indexed for this repository

Research direction

The issue names no repository files, tests, or entry points. Start by tracing the proposed MyExperiment.load flow and the @ YAML identifiers shown in the example. Done means the requested YAML-backed fields and path modifications are implemented and verified by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, yaml
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.