deepskies / DeepBench

Simulation library for very simple simulations to benchmark machine learning algorithms
Apache License 2.0
3 stars 1 forks source link

Standalone examples in README throw errors #142

Open jwuphysics opened 2 weeks ago

jwuphysics commented 2 weeks ago

Describe the bug The first pendulum example on the README.md throws errors. The configuration dict construction is missing a closing curly brace, and beyond that, the object_parameters key is not initialized or does not fall back to a default value.

The second noisy shapes example is not correctly formatted either: there is an extra set of square braces, which causes a SyntaxError once it reaches the : in "rectangle": {. Removing square braces enables the configuration to run correctly, but then the remaining code still does not run.

To Reproduce

I use ipython to run the following (corrected) version of the example

import numpy as np
from deepbench.collection import Collection

configuration = {
    "object_type": "physics",
    "object_name": "Pendulum",
    "total_runs": 3,
    "parameter_noise": 0.2,
    "image_parameters": {
        "pendulum_arm_length": 2,
        "starting_angle_radians": 0.25,
        "acceleration_due_to_gravity": 9.8,
        "noise_std_percent":{
            "acceleration_due_to_gravity": 0
        },
        "object_parameters":{
            "time": np.linspace(0, 1, 10)
        }
    }
}

phy_objects = Collection(configuration)()

objects = phy_objects.objects
parameters = phy_objects.object_parameters

This results in the error:

KeyError                                  Traceback (most recent call last)
Cell In[1], line 22
      2 from deepbench.collection import Collection
      4 configuration = {
      5     "object_type": "physics",
      6     "object_name": "Pendulum",
   (...)
     19     }
     20 }
---> 22 phy_objects = Collection(configuration)()
     24 objects = phy_objects.objects
     25 parameters = phy_objects.object_parameters

File .../DeepBench/deepbench/collection/collection.py:51, in Collection.__init__(self, object_config)
     48 self.object_params = {}
     50 if object_config is not None: 
---> 51     self._set_parameters(object_config)

File .../DeepBench/deepbench/collection/collection.py:73, in Collection._set_parameters(self, object_config)
     71 self.total_objects = object_config["total_runs"]
     72 self.included_params = object_config["image_parameters"]
---> 73 self.object_rules = object_config["object_parameters"]
     75 object_parent_class = {
     76     "sky": image.sky_image.SkyImage,
     77     "shape": image.shape_image.ShapeImage,
     78     "physics": physics.physics_object.PhysicsObject,
     79     "astro": astro.astro_object.AstroObject,
     80 }[self.object_type]
     82 self.object_engine_classes = {
     83     cls.__name__: cls for cls in object_parent_class.__subclasses__()
     84 }

KeyError: 'object_parameters'

Similarly, for the second example, if I run (the corrected):

import numpy as np
from deepbench.collection import Collection

configuration = {
    "object_type": "shape",
    "object_name": "ShapeImage",

    "total_runs": 1,
    "image_parameters": {
        "image_shape": (28, 28),
        "object_noise_level": 0.6
    },

    "object_parameters": {
        [
        "rectangle": {
            "object": {
                "width": np.random.default_rng().integers(2, 28),
                "height": np.random.default_rng().integers(2, 28),
                "fill": True
            },
            "instance": {}
        },
        "arc":{
            "object": {
                "radius": np.random.default_rng().integers(2, 28),
                "theta1":np.random.default_rng().integers(0, 20),
                "theta2":np.random.default_rng().integers(21, 180)
            },
            "instance":{}
        }

        ]
    }
}

shape_image = Collection(configuration)()

objects = shape_image.objects
parameters = shape_image.object_parameters

I now get the error:

AttributeError                            Traceback (most recent call last)
Cell In[6], line 36
      4 configuration = {
      5     "object_type": "shape",
      6     "object_name": "ShapeImage",
   (...)
     31     }
     32 }
     34 shape_image = Collection(configuration)()
---> 36 objects = shape_image.objects
     37 parameters = shape_image.object_parameters

AttributeError: 'NoneType' object has no attribute 'objects'

Expected behavior I would expect both sets of code to run correctly, perhaps handling missing attributes (like object_parameters or objects) if not supplied to the configuration parameter in Collection().

Desktop (please complete the following information):

voetberg commented 1 week ago

Ah, this is actually a really silly mistake on my end. The return object of calling the Collection isn't the Collection instance, it's null. The objects and object_params are attributes of the Collection itself. These will be corrected no problem.