Browse Source

[ENH] Add check_learnware

tags/v0.3.2
Gene 3 years ago
parent
commit
7eca0debf5
3 changed files with 46 additions and 22 deletions
  1. +6
    -1
      examples/example_market_db/example_db.py
  2. +10
    -11
      learnware/learnware/base.py
  3. +30
    -10
      learnware/market/easy.py

+ 6
- 1
examples/example_market_db/example_db.py View File

@@ -56,5 +56,10 @@ def test_market():
print("Available ids:", curr_inds)


def test_search():
easy_market = EasyMarket()

if __name__ == "__main__":
test_market()
# test_market()
test_search()

+ 10
- 11
learnware/learnware/base.py View File

@@ -14,19 +14,13 @@ class Learnware:
self.model = self._import_model(model)
self.specification = specification

def _import_model(self, model: Union[BaseModel, dict]) -> BaseModel:
def _import_model(self, model: Union[BaseModel, str]) -> BaseModel:
"""_summary_

Parameters
----------
model : Union[BaseModel, dict]
- If isinstance(model, dict), auto import the model w.r.t the following format:
model = {
"module_path": str,
"class_name": str
}
- where the module_path is the path of python file
- where the class_name is the name of class in python file
- If isinstance(model, str), model is the path of the python file
- If isinstance(model, BaseModel), return model directly
Returns
-------
@@ -39,9 +33,14 @@ class Learnware:
"""
if isinstance(model, BaseModel):
return model
elif isinstance(model, dict):
model_module = get_module_by_module_path(model["module_path"])
return getattr(model_module, model["class_name"])()
elif isinstance(model, str):
model_dict = {
"module_path": model, # path of python file
"class_name": "Model" # the name of class in python file, default is "Model", can be changed by yaml
}
# TODO: test yaml file, change model_dict["class_name"]
model_module = get_module_by_module_path(model_dict["module_path"])
return getattr(model_module, model_dict["class_name"])()
else:
raise TypeError("model must be BaseModel or dict")



+ 30
- 10
learnware/market/easy.py View File

@@ -27,6 +27,25 @@ class EasyMarket(BaseMarket):
def reload_market(self) -> bool:
self.learnware_list, self.count = load_market_from_db()

def check_learnware(self, learnware: Learnware) -> bool:
"""Check the utility of a learnware

Parameters
----------
learnware : Learnware

Returns
-------
bool
A flag indicating whether the learnware can be accepted.
"""
try:
spec_data = learnware.specification.stat_spec["RKME"].get_z()
pred_spec = learnware.predict(spec_data)
return True
except:
return False
def add_learnware(
self, learnware_name: str, model_path: str, stat_spec_path: str, semantic_spec: dict
) -> Tuple[str, bool]:
@@ -65,21 +84,22 @@ class EasyMarket(BaseMarket):
if (not os.path.exists(model_path)) or (not os.path.exists(stat_spec_path)):
raise FileNotFoundError("Model or Stat_spec NOT Found.")

id = "%08d" % (self.count)
rkme_stat_spec = RKMEStatSpecification()
rkme_stat_spec.load(stat_spec_path)
stat_spec = {"RKME": rkme_stat_spec}
specification = Specification(semantic_spec=semantic_spec, stat_spec=stat_spec)
# Commented for test purpose. Uncomment when Learnware class is implemented.
# model_dict = {"module_path": model_path, "class_name": "BaseModel"}
id = "%08d" % (self.count)
new_learnware = Learnware(id=id, name=learnware_name, model=model_path, specification=specification)
self.learnware_list[id] = new_learnware
self.count += 1
add_learnware_to_db(
id, name=learnware_name, model_path=model_path, stat_spec_path=stat_spec_path, semantic_spec=semantic_spec
)

return id, True
if self.check_learnware(new_learnware):
self.learnware_list[id] = new_learnware
self.count += 1
add_learnware_to_db(
id, name=learnware_name, model_path=model_path, stat_spec_path=stat_spec_path, semantic_spec=semantic_spec
)
return id, True
else:
return None, False

def _calculate_rkme_spec_mixture_weight(
self,


Loading…
Cancel
Save