Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make config parser #20

Open
cfuselli opened this issue Oct 16, 2023 · 2 comments
Open

Make config parser #20

cfuselli opened this issue Oct 16, 2023 · 2 comments

Comments

@cfuselli
Copy link
Collaborator

It would be nice to have a module or a function that reads in the config file and sets all the necessary defaults in the same place, deals with missing entries or wrong values etc..

Something like this, but better adapted to our needs.

import json

class Config:
    def __init__(self, config_file):
        self.defaults = {
            "detector": "XAMS",
            "nevents": 10000,
            "nphoton_per_event": [100, 100000],
            "photon_zgen": 0.5,
            "geometry": {
                "type": "cylinder",
                "radius": 3.2,
                "ztop": 1.2,
                "zliq": 0.0,
                "zbot": -6.7,
                "ptfe_zmin": -5.32,
                "ptfe_zmax": -0.25
            },
            "npmt_xy": 2,
            "pmt": {
                "type": "square",
                "size": 2.54,
                "ndivs": 10
            },
            "scatter": True,
            "experimental_scatter_model": True
        }
        
        with open(config_file, 'r') as f:
            self.config = json.load(f)
        
        self._validate_and_set_defaults()

    def _validate_and_set_defaults(self):
        for key, value in self.defaults.items():
            if key not in self.config:
                self.config[key] = value
            elif isinstance(value, dict):
                for subkey, subvalue in value.items():
                    if subkey not in self.config[key]:
                        self.config[key][subkey] = subvalue

    def get(self, key, default=None):
        return self.config.get(key, default)

    def __getitem__(self, key):
        return self.config[key]

    def __setitem__(self, key, value):
        self.config[key] = value

    def __contains__(self, key):
        return key in self.config

# Usage
config = Config('path_to_config_file.json')
print(config.get('detector'))
print(config['geometry']['type'])
@acolijn
Copy link
Owner

acolijn commented Oct 16, 2023

So this would be to make sure that all necessary parameters are defined? Could be useful, but if things are field in automatically you may also miss essentials and set them to some crazy default.

As an alternative we could build in some checks after reading in the config? If parameters are missing it should throw an exception and stop running

@cfuselli
Copy link
Collaborator Author

Yess I agree. I do not want to have all the keys to have a defualt, and we can raise errors where they are needed etc.. but it's nice to have this done in only one place for the entire package, so you make sure that you don't have different values as default in different parts of the code or stuff like this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants