Ejemplo n.º 1
0
 def get_restriction(serialized_restriction):
     """Convert a serialized restriction (a dict) returned by the JSON decoder into a Restriction object."""
     restriction = Restriction(serialized_restriction["index"],
                               serialized_restriction["name"],
                               serialized_restriction["objective_func"],
                               serialized_restriction["normalization"],
                               serialized_restriction["default_low"],
                               serialized_restriction["default_upp"],
                               dec_pt=serialized_restriction["dec_pt"])
     return restriction
Ejemplo n.º 2
0
    def new_other_restriction(self):
        ot = Other('',{}, {'fluxes_total': 1}, 0, 100, 1)
        self.add_other_restriction(ot)
        i = list(self.other_dict.keys())[-1]     # index of new restriction
        ot.name = 'Restriction #'+i
        self.order['other'].append(i)
        self.restr_dict['other_'+i] = Restriction('other_'+i, ot.name, 'other_'+i, ot.normalization, 0, 100)

        self.json_write_other()  # Can we replace this by function that just updates a single ingredient?
        self.json_write_order()
        self.json_write_restrictions()
    
        return i, ot
Ejemplo n.º 3
0
    def new_ingredient(self):
        ing = Ingredient('', notes='', analysis={}, other_attributes={})
                            # If we just had Ingredient('Ingredient #'+index) above, the default values of the notes, analysis
                            # and other_attributes attributes would change when the last instance of the class defined had those
                            # attributes changed
        self.add_ingredient(ing)
        i = list(self.ingredient_dict.keys())[-1]     # index of new ingredient
        ing.name = 'Ingredient #'+i
        self.order['ingredients'].append(i)
        self.restr_dict['ingredient_'+i] = Restriction('ingredient_'+i, ing.name, 'ingredient_'+i, {'ingredient_total': 0.01}, 0, 100)

        self.json_write_ingredients()  # Can we replace this by function that just updates a single ingredient?
        self.json_write_order()
        self.json_write_restrictions()
    
        return i, ing
Ejemplo n.º 4
0
    def __init__(self):
        
        OxideData.set_default_oxides()
        with open(path.join(persistent_data_path, "JSONOxides.json"), 'r') as f:
            OxideData.oxide_dict = OxideSerializer.deserialize_dict(json.load(f))

        CoreData.__init__(self)

        if True:    # Use data saved by user if True
            self.other_attr_dict = {'0': 'LOI', '2': 'Clay', '1': 'Cost'}  # Replace by functions that sets data saved by user

            with open(path.join(persistent_data_path, "JSONIngredients.json"), 'r') as f:
                self.ingredient_dict = IngredientSerializer.deserialize_dict(json.load(f))
                
            for i, ing in self.ingredient_dict.items():
                self.ingredient_analyses[i] = ing.analysis

            # All of the data contained in JSONOther, except numerator_coefs, can be obtained from JSONRestriction.
            # Need to rethink how data is packaged.
            with open(path.join(persistent_data_path, "JSONOther.json"), 'r') as f:
                self.other_dict = OtherSerializer.deserialize_dict(json.load(f))

            with open(path.join(persistent_data_path, "JSONRecipes.json"), 'r') as f:
                self.recipe_dict = RecipeSerializer.deserialize_dict(json.load(f))

            with open(path.join(persistent_data_path, "JSONOrder.json"), 'r') as f:
                self.order = json.load(f)

            #Create Restriction dictionary
            with open(path.join(persistent_data_path, "JSONRestrictions.json"), 'r') as f:
                self.restr_dict = RestrictionSerializer.deserialize_dict(json.load(f))
            # It seems a bit silly to record the default lower and upper bounds in both self.restr_dict
            # and in self.default_lower_bounds and self.default_lower_bounds, but that's how things
            # stand at the moment
            for key in self.restr_keys():
                self.default_lower_bounds[key] = self.restr_dict[key].default_low
                self.default_upper_bounds[key] = self.restr_dict[key].default_upp
                
        else:   # Reset data
            self.set_default_data()
             
            self.set_default_default_bounds()
            with open(path.join(persistent_data_path, "JSONRecipes.json"), 'r') as f:
                self.recipe_dict = RecipeSerializer.deserialize_dict(json.load(f))
            self.order = {"ingredients": ["0", "1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19"],
                          "oxides":["SiO2","Al2O3","B2O3","MgO","CaO","SrO","BaO","ZnO","Li2O","Na2O","K2O","P2O5","Fe2O3","TiO2","MnO2","ZrO2"],
                          "other": ["0","1","2","3","4","5","6"],
                          "other attributes": ["0", "1", "2"] }
                
            self.restr_dict = {}

            # Create oxide restrictions:
            for ox in self.order['oxides']:
                key = 'umf_'+ox
                self.restr_dict[key] = Restriction(key, ox, 'mole_'+ox, {'fluxes_total': 1}, \
                                                   self.default_lower_bounds[key], self.default_upper_bounds[key], dec_pt=3)
                key = 'mass_perc_'+ox
                self.restr_dict[key] = Restriction(key, ox, 'mass_'+ox, {'ox_mass_total': 0.01}, \
                                                   self.default_lower_bounds[key], self.default_upper_bounds[key], dec_pt=2)
                key = 'mole_perc_'+ox
                self.restr_dict[key] = Restriction(key, ox, 'mole_'+ox, {'ox_mole_total': 0.01}, \
                                                   self.default_lower_bounds[key], self.default_upper_bounds[key], dec_pt=2)

            # Create ingredient restrictions:
            for i, ing in self.ingredient_dict.items():
                key = 'ingredient_'+i
                self.restr_dict[key] = Restriction(key, ing.name, key, {'ingredient_total': 0.01}, \
                                       self.default_lower_bounds[key], self.default_upper_bounds[key])

            # Create other restrictions:
            for i, ot in self.other_dict.items():
                key = 'other_'+i
                self.restr_dict[key] = Restriction(key, ot.name, key, ot.normalization, ot.def_low, ot.def_upp, dec_pt=ot.dec_pt)

            self.json_write_restrictions()
            self.json_write_ingredients()
            self.json_write_other()
            self.json_write_order()

        self.current_recipe = None
        self.recipe_index = None
        self.set_current_recipe('0')