sweverett / CluStR

Calculates various scaling relations from cluster catalogs.
5 stars 4 forks source link

Keyerror Raised #38

Closed jjobel closed 4 years ago

jjobel commented 4 years ago

It seems that the the key value x_label is not found in the config file when running the following code,

def get_data(self, config, catalog):
        xlabel = fits_label(config['x_label'])
        ylabel = fits_label(config['y_label'])

From my understanding, the xlabel references the fits_label function while taking in as input config['x_label'] as the axis name but I end up with the following error,

KeyError: 'x_label'

Which I found to mean that I'm trying to access a key that is not in the dictionary. I've also tried using the following,

def get_data(self, config, catalog):
        xlabel = fits_label(Config.__getitem__(x_label))
        ylabel = fits_label(Config.__getitem__(y_label))

I now get the following error,

NameError: name 'x_label' is not defined

Essentially, I'm trying to figure out how to obtain x and y for the Data class.

jjobel commented 4 years ago

This is a follow up.

It seems that I can define def get_data(self, config, catalog): as the following,

def get_data(self, config, catalog):
        args = parser.parse_args()
        x_label = args.x
        y_label = args.y
        xlabel = fits_label(config.__getitem__(x_label))
        ylabel = fits_label(config.__getitem__(y_label))
        x = catalog[xlabel]
        y = catalog[ylabel]

This way I'm able to retrieve x_label = r2500_band_lumin value. Unfortunately, I still get a KeyError message but I'm working that out. I used the user input arguments for the x and y values and define them as x_label and y_label. This is an alternative to def get_data(options): from the master branch.

sweverett commented 4 years ago

I wrote the __getitem__() function of the Config class so that you can access it like a normal dictionary, so you don't need to do config.__getitem__(x_label).

The key error just means that there is no entry for x_label in the config. In the master branch that is because it is passed as a command line argument rather than through the config. Personally I think that is probably the better way, so you can quickly explore different x,y fits on a catalog with the same config settings. But we could move it to the config if you'd like.

Here's the short version of what I'd suggest:

# in main():
args = parser.parse_args()
config = Config(args)

...

# in Config():

def __init__(args):
        self.config_filename = args.config_file
        self.x = args.x
        self.y = args.y

        with open(self.config_filename, 'r') as stream:
            self._config = yaml.safe_load(stream)

        return

This way now constructs a Config purely from the command line arguments. It still grabs the config_file from args, but also grabs other useful things like the x and y names that are passed to fits_label() in the master branch.