Browse Source

[MNT] modify details and format code

tags/v0.3.2
Gene 2 years ago
parent
commit
05b6fd16c9
11 changed files with 77 additions and 67 deletions
  1. +1
    -0
      learnware/reuse/feature_augment_reuser.py
  2. +3
    -1
      learnware/reuse/hetero_reuser/__init__.py
  3. +13
    -17
      tests/test_hetero_market/example_learnwares/config.py
  4. +4
    -4
      tests/test_hetero_market/example_learnwares/example_learnware_0/__init__.py
  5. +0
    -0
      tests/test_hetero_market/example_learnwares/example_learnware_0/learnware.yaml
  6. +0
    -0
      tests/test_hetero_market/example_learnwares/example_learnware_0/requirements.txt
  7. +4
    -4
      tests/test_hetero_market/example_learnwares/example_learnware_1/__init__.py
  8. +0
    -0
      tests/test_hetero_market/example_learnwares/example_learnware_1/learnware.yaml
  9. +0
    -0
      tests/test_hetero_market/example_learnwares/example_learnware_1/requirements.txt
  10. +48
    -37
      tests/test_hetero_market/test_hetero.py
  11. +4
    -4
      tests/test_workflow/test_workflow.py

+ 1
- 0
learnware/reuse/feature_augment_reuser.py View File

@@ -4,6 +4,7 @@ from sklearn.linear_model import RidgeCV, LogisticRegressionCV
from .base import BaseReuser
from learnware.learnware import Learnware


class FeatureAugmentReuser(BaseReuser):
"""
FeatureAugmentReuser is a class for augmenting features using predictions of a given learnware model and applying regression or classification on the augmented dataset.


+ 3
- 1
learnware/reuse/hetero_reuser/__init__.py View File

@@ -51,7 +51,9 @@ class HeteroMapTableReuser(BaseReuser):
user_rkme : RKMETableSpecification
The RKME specification from the user dataset.
"""
self.feature_aligner = FeatureAligner(learnware=self.learnware, mode=self.mode, cuda_idx=self.cuda_idx, **self.align_arguments)
self.feature_aligner = FeatureAligner(
learnware=self.learnware, mode=self.mode, cuda_idx=self.cuda_idx, **self.align_arguments
)
self.feature_aligner.fit(user_rkme)
self.reuser = self.feature_aligner



tests/test_market/test_hetero_market/example_learnwares/config.py → tests/test_hetero_market/example_learnwares/config.py View File

@@ -1,9 +1,9 @@
input_shape_list=[20, 30] # 20-input shape of example learnware 0, 30-input shape of example learnware 1
input_shape_list = [20, 30] # 20-input shape of example learnware 0, 30-input shape of example learnware 1

input_description_list=[
input_description_list = [
{
"Dimension": 20,
"Description": { # medical description
"Description": { # medical description
"0": "baseline value: Baseline Fetal Heart Rate (FHR)",
"1": "accelerations: Number of accelerations per second",
"2": "fetal_movement: Number of fetal movements per second",
@@ -23,12 +23,12 @@ input_description_list=[
"16": "histogram_mode: Hist mode",
"17": "histogram_mean: Hist mean",
"18": "histogram_median: Hist Median",
"19": "histogram_variance: Hist variance"
"19": "histogram_variance: Hist variance",
},
},
{
"Dimension": 30,
"Description": { # business description
"Description": { # business description
"0": "This is a consecutive month number, used for convenience. For example, January 2013 is 0, February 2013 is 1,..., October 2015 is 33.",
"1": "This is the unique identifier for each shop.",
"2": "This is the unique identifier for each item.",
@@ -58,32 +58,28 @@ input_description_list=[
"26": "This is the average count of items of the same subtype sold in the shop one month ago.",
"27": "This is the average count of items sold in the same city one month ago.",
"28": "This is the average count of this type of item sold in the same city one month ago.",
"29": "This is the average count of items of the same type sold one month ago."
"29": "This is the average count of items of the same type sold one month ago.",
},
},
]

output_description_list=[
output_description_list = [
{
"Dimension": 1,
"Description": { # medical description
"0": "length of stay: Length of hospital stay (days)"
},
"Description": {"0": "length of stay: Length of hospital stay (days)"}, # medical description
},
{
"Dimension": 1,
"Description": { # business description
"Description": { # business description
"0": "sales of the item in the next day: Number of items sold in the next day"
},
},
]

user_description_list=[
user_description_list = [
{
"Dimension": 15,
"Description": { # medical description
"Description": { # medical description
"0": "Whether the patient is on thyroxine medication (0: No, 1: Yes)",
"1": "Whether the patient has been queried about thyroxine medication (0: No, 1: Yes)",
"2": "Whether the patient is on antithyroid medication (0: No, 1: Yes)",
@@ -98,7 +94,7 @@ user_description_list=[
"11": "Whether TSH (Thyroid Stimulating Hormone) level has been measured (0: No, 1: Yes)",
"12": "Whether T3 (Triiodothyronine) level has been measured (0: No, 1: Yes)",
"13": "Whether TT4 (Total Thyroxine) level has been measured (0: No, 1: Yes)",
"14": "Whether T4U (Thyroxine Utilization) level has been measured (0: No, 1: Yes)"
"14": "Whether T4U (Thyroxine Utilization) level has been measured (0: No, 1: Yes)",
},
}
]
]

tests/test_market/test_hetero_market/example_learnwares/example_learnware_0/__init__.py → tests/test_hetero_market/example_learnwares/example_learnware_0/__init__.py View File

@@ -8,15 +8,15 @@ class MyModel(BaseModel):
def __init__(self):
super(MyModel, self).__init__(input_shape=(20,), output_shape=(1,))
dir_path = os.path.dirname(os.path.abspath(__file__))
model_path=os.path.join(dir_path, "ridge.pkl")
model_path = os.path.join(dir_path, "ridge.pkl")
model = joblib.load(model_path)
self.model=model
self.model = model

def fit(self, X: np.ndarray, y: np.ndarray):
pass

def predict(self, X: np.ndarray) -> np.ndarray:
return self.model.predict(X)
def finetune(self, X: np.ndarray, y: np.ndarray):
pass
pass

tests/test_market/test_hetero_market/example_learnwares/example_learnware_0/learnware.yaml → tests/test_hetero_market/example_learnwares/example_learnware_0/learnware.yaml View File


tests/test_market/test_hetero_market/example_learnwares/example_learnware_0/requirements.txt → tests/test_hetero_market/example_learnwares/example_learnware_0/requirements.txt View File


tests/test_market/test_hetero_market/example_learnwares/example_learnware_1/__init__.py → tests/test_hetero_market/example_learnwares/example_learnware_1/__init__.py View File

@@ -8,15 +8,15 @@ class MyModel(BaseModel):
def __init__(self):
super(MyModel, self).__init__(input_shape=(30,), output_shape=(1,))
dir_path = os.path.dirname(os.path.abspath(__file__))
model_path=os.path.join(dir_path, "ridge.pkl")
model_path = os.path.join(dir_path, "ridge.pkl")
model = joblib.load(model_path)
self.model=model
self.model = model

def fit(self, X: np.ndarray, y: np.ndarray):
pass

def predict(self, X: np.ndarray) -> np.ndarray:
return self.model.predict(X)
def finetune(self, X: np.ndarray, y: np.ndarray):
pass
pass

tests/test_market/test_hetero_market/example_learnwares/example_learnware_1/learnware.yaml → tests/test_hetero_market/example_learnwares/example_learnware_1/learnware.yaml View File


tests/test_market/test_hetero_market/example_learnwares/example_learnware_1/requirements.txt → tests/test_hetero_market/example_learnwares/example_learnware_1/requirements.txt View File


tests/test_market/test_hetero_market/test_hetero.py → tests/test_hetero_market/test_hetero.py View File

@@ -16,7 +16,12 @@ import learnware
from learnware.market import instantiate_learnware_market, BaseUserInfo
from learnware.specification import RKMETableSpecification, generate_rkme_spec
from learnware.reuse import HeteroMapTableReuser
from example_learnwares.config import input_shape_list, input_description_list, output_description_list, user_description_list
from example_learnwares.config import (
input_shape_list,
input_description_list,
output_description_list,
user_description_list,
)

curr_root = os.path.dirname(os.path.abspath(__file__))

@@ -32,6 +37,7 @@ user_semantic = {
"Name": {"Values": "", "Type": "String"},
}


def check_learnware(learnware_name, dir_path=os.path.join(curr_root, "learnware_pool")):
print(f"Checking Learnware: {learnware_name}")
zip_file_path = os.path.join(dir_path, learnware_name)
@@ -56,7 +62,6 @@ class TestMarket(unittest.TestCase):
hetero_market = instantiate_learnware_market(market_id="hetero_toy", name="hetero", rebuild=True)
return hetero_market


def test_prepare_learnware_randomly(self, learnware_num=5):
self.zip_path_list = []

@@ -66,13 +71,13 @@ class TestMarket(unittest.TestCase):

print("Preparing Learnware: %d" % (i))

example_learnware_idx=i%2
input_dim=input_shape_list[example_learnware_idx]
example_learnware_name="example_learnwares/example_learnware_%d" % (example_learnware_idx)
example_learnware_idx = i % 2
input_dim = input_shape_list[example_learnware_idx]
example_learnware_name = "example_learnwares/example_learnware_%d" % (example_learnware_idx)

X, y = make_regression(n_samples=5000, n_informative=15, n_features=input_dim, noise=0.1, random_state=42)

clf=Ridge(alpha=1.0)
clf = Ridge(alpha=1.0)
clf.fit(X, y)

joblib.dump(clf, os.path.join(dir_path, "ridge.pkl"))
@@ -86,7 +91,9 @@ class TestMarket(unittest.TestCase):
) # cp example_init.py init_file

yaml_file = os.path.join(dir_path, "learnware.yaml")
copyfile(os.path.join(curr_root, example_learnware_name, "learnware.yaml"), yaml_file) # cp example.yaml yaml_file
copyfile(
os.path.join(curr_root, example_learnware_name, "learnware.yaml"), yaml_file
) # cp example.yaml yaml_file

env_file = os.path.join(dir_path, "requirements.txt")
copyfile(os.path.join(curr_root, example_learnware_name, "requirements.txt"), env_file)
@@ -143,14 +150,16 @@ class TestMarket(unittest.TestCase):
for learnware_id in curr_inds:
hetero_market.delete_learnware(learnware_id)
self.learnware_num -= 1
assert len(hetero_market) == self.learnware_num, f"The number of learnwares must be {self.learnware_num}!"
assert (
len(hetero_market) == self.learnware_num
), f"The number of learnwares must be {self.learnware_num}!"

curr_inds = hetero_market.get_learnware_ids()
print("Available ids After Deleting Learnwares:", curr_inds)
assert len(curr_inds) == 0, f"The market should be empty!"

return hetero_market
def test_train_market_model(self, learnware_num=5):
hetero_market = self._init_learnware_market()
self.test_prepare_learnware_randomly(learnware_num)
@@ -214,7 +223,7 @@ class TestMarket(unittest.TestCase):

# hetero test
print("+++++ HETERO TEST ++++++")
user_dim=15
user_dim = 15

test_folder = os.path.join(curr_root, "test_stat")

@@ -230,18 +239,20 @@ class TestMarket(unittest.TestCase):

user_spec = RKMETableSpecification()
user_spec.load(os.path.join(unzip_dir, "stat.json"))
z=user_spec.get_z()
z=z[:,:user_dim]
device=user_spec.device
z=torch.tensor(z, device=device)
user_spec.z=z
z = user_spec.get_z()
z = z[:, :user_dim]
device = user_spec.device
z = torch.tensor(z, device=device)
user_spec.z = z

print(">> normal case test:")
semantic_spec = copy.deepcopy(user_semantic)
semantic_spec["Input"]=copy.deepcopy(input_description_list[idx%2])
semantic_spec["Input"]['Dimension']=user_dim
semantic_spec["Input"] = copy.deepcopy(input_description_list[idx % 2])
semantic_spec["Input"]["Dimension"] = user_dim
# keep only the first user_dim descriptions
semantic_spec["Input"]['Description']={key: semantic_spec["Input"]['Description'][str(key)] for key in range(user_dim)}
semantic_spec["Input"]["Description"] = {
key: semantic_spec["Input"]["Description"][str(key)] for key in range(user_dim)
}

user_info = BaseUserInfo(semantic_spec=semantic_spec, stat_info={"RKMETableSpecification": user_spec})
(
@@ -257,7 +268,7 @@ class TestMarket(unittest.TestCase):

# empty value of key "Task" in semantic_spec, use homo search and print invalid semantic_spec
print(">> test for key 'Task' has empty 'Values':")
semantic_spec["Task"]={"Values":{}}
semantic_spec["Task"] = {"Values": {}}

user_info = BaseUserInfo(semantic_spec=semantic_spec, stat_info={"RKMETableSpecification": user_spec})
(
@@ -267,8 +278,7 @@ class TestMarket(unittest.TestCase):
mixture_learnware_list,
) = hetero_market.search_learnware(user_info)

assert(len(single_learnware_list)==0), f"Statistical search failed!"

assert len(single_learnware_list) == 0, f"Statistical search failed!"

# delete key "Task" in semantic_spec, use homo search and print WARNING INFO with "User doesn't provide correct task type"
print(">> delele key 'Task' test:")
@@ -282,14 +292,16 @@ class TestMarket(unittest.TestCase):
mixture_learnware_list,
) = hetero_market.search_learnware(user_info)

assert(len(single_learnware_list)==0), f"Statistical search failed!"
assert len(single_learnware_list) == 0, f"Statistical search failed!"

# modify semantic info with mismatch dim, use homo search and print "User data feature dimensions mismatch with semantic specification."
print(">> mismatch dim test")
semantic_spec = copy.deepcopy(user_semantic)
semantic_spec["Input"]=copy.deepcopy(input_description_list[idx%2])
semantic_spec["Input"]['Dimension']=user_dim-2
semantic_spec["Input"]['Description']={key: semantic_spec["Input"]['Description'][str(key)] for key in range(user_dim)}
semantic_spec["Input"] = copy.deepcopy(input_description_list[idx % 2])
semantic_spec["Input"]["Dimension"] = user_dim - 2
semantic_spec["Input"]["Description"] = {
key: semantic_spec["Input"]["Description"][str(key)] for key in range(user_dim)
}

user_info = BaseUserInfo(semantic_spec=semantic_spec, stat_info={"RKMETableSpecification": user_spec})
(
@@ -299,8 +311,7 @@ class TestMarket(unittest.TestCase):
mixture_learnware_list,
) = hetero_market.search_learnware(user_info)

assert(len(single_learnware_list)==0), f"Statistical search failed!"

assert len(single_learnware_list) == 0, f"Statistical search failed!"

rmtree(test_folder) # rm -r test_folder

@@ -328,7 +339,7 @@ class TestMarket(unittest.TestCase):
mixture_learnware_list,
) = hetero_market.search_learnware(user_info)

target_spec_num=3 if idx%2==0 else 2
target_spec_num = 3 if idx % 2 == 0 else 2
assert len(single_learnware_list) == target_spec_num, f"Statistical search failed!"
print(f"search result of user{idx}:")
for score, learnware in zip(sorted_score_list, single_learnware_list):
@@ -349,7 +360,7 @@ class TestMarket(unittest.TestCase):
# generate specification
semantic_spec = copy.deepcopy(user_semantic)
semantic_spec["Input"] = user_description_list[0]
user_info=BaseUserInfo(semantic_spec=semantic_spec, stat_info={"RKMETableSpecification": user_spec})
user_info = BaseUserInfo(semantic_spec=semantic_spec, stat_info={"RKMETableSpecification": user_spec})

# learnware market search
hetero_market = self.test_train_market_model(learnware_num)
@@ -365,21 +376,21 @@ class TestMarket(unittest.TestCase):
print(f"score: {score}, learnware_id: {learnware.id}")

# model reuse
reuser=HeteroMapTableReuser(single_learnware_list[0], mode='regression')
reuser = HeteroMapTableReuser(single_learnware_list[0], mode="regression")
reuser.fit(user_spec)
reuser.finetune(X[:100], y[:100])
y_pred=reuser.predict(X)
rmse=mean_squared_error(y, y_pred, squared=False)
y_pred = reuser.predict(X)
rmse = mean_squared_error(y, y_pred, squared=False)
print(f"rmse finetune: {rmse}")


def suite():
_suite = unittest.TestSuite()
# _suite.addTest(TestMarket("test_prepare_learnware_randomly"))
# _suite.addTest(TestMarket("test_generated_learnwares"))
# _suite.addTest(TestMarket("test_upload_delete_learnware"))
# _suite.addTest(TestMarket("test_train_market_model"))
# _suite.addTest(TestMarket("test_search_semantics"))
_suite.addTest(TestMarket("test_prepare_learnware_randomly"))
_suite.addTest(TestMarket("test_generated_learnwares"))
_suite.addTest(TestMarket("test_upload_delete_learnware"))
_suite.addTest(TestMarket("test_train_market_model"))
_suite.addTest(TestMarket("test_search_semantics"))
_suite.addTest(TestMarket("test_stat_search"))
_suite.addTest(TestMarket("test_model_reuse"))
return _suite

+ 4
- 4
tests/test_workflow/test_workflow.py View File

@@ -232,10 +232,10 @@ class TestWorkflow(unittest.TestCase):

def suite():
_suite = unittest.TestSuite()
# _suite.addTest(TestWorkflow("test_prepare_learnware_randomly"))
# _suite.addTest(TestWorkflow("test_upload_delete_learnware"))
# _suite.addTest(TestWorkflow("test_search_semantics"))
# _suite.addTest(TestWorkflow("test_stat_search"))
_suite.addTest(TestWorkflow("test_prepare_learnware_randomly"))
_suite.addTest(TestWorkflow("test_upload_delete_learnware"))
_suite.addTest(TestWorkflow("test_search_semantics"))
_suite.addTest(TestWorkflow("test_stat_search"))
_suite.addTest(TestWorkflow("test_learnware_reuse"))
return _suite



Loading…
Cancel
Save