diff options
| author | Adam Janovsky | 2021-10-15 14:45:28 +0200 |
|---|---|---|
| committer | Adam Janovsky | 2021-10-15 14:45:28 +0200 |
| commit | 2cdd01e86d7e529214a4f6f04d699e0d384555eb (patch) | |
| tree | f7ac1b860f49401156690c6903354970df7ee8de /sec_certs | |
| parent | e6b306897482ad22890db28129ed650d16360ce6 (diff) | |
| download | sec-certs-2cdd01e86d7e529214a4f6f04d699e0d384555eb.tar.gz sec-certs-2cdd01e86d7e529214a4f6f04d699e0d384555eb.tar.zst sec-certs-2cdd01e86d7e529214a4f6f04d699e0d384555eb.zip | |
cleanup evaluation functions
Diffstat (limited to 'sec_certs')
| -rw-r--r-- | sec_certs/model/evaluation.py | 44 |
1 files changed, 2 insertions, 42 deletions
diff --git a/sec_certs/model/evaluation.py b/sec_certs/model/evaluation.py index 0f3855a9..77c66ddf 100644 --- a/sec_certs/model/evaluation.py +++ b/sec_certs/model/evaluation.py @@ -1,29 +1,19 @@ import json from pathlib import Path import logging -from typing import Dict, List, Set, Optional, Union +from typing import List, Set,Union import numpy as np -from sec_certs.dataset.common_criteria import CCDataset -from sec_certs.certificate.common_criteria import CommonCriteriaCert -from sec_certs.dataset.cve import CVEDataset, CVE -from sec_certs.serialization import CustomJSONEncoder -import sec_certs.helpers as helpers -import tqdm - logger = logging.getLogger(__name__) + def get_validation_dgsts(filepath: Union[str, Path]) -> Set[str]: with Path(filepath).open('r') as handle: data = json.load(handle) return set(data.keys()) -def get_y_true(certs: List[CommonCriteriaCert]) -> np.array: - return np.array([np.array([y.cve_id for y in cert.heuristics.related_cves]) if cert.heuristics.related_cves else np.array(['None']) for cert in certs], dtype='object') - - def compute_precision(y: np.array, y_pred: np.array, **kwargs): prec = [] for true, pred in zip(y, y_pred): @@ -41,33 +31,3 @@ def compute_precision(y: np.array, y_pred: np.array, **kwargs): else: prec.append(len(set_true.intersection(set_pred)) / len(set_true)) return np.mean(prec) - -def cpe_evaluate_classifier(x_valid, y_pred, y_true, outpath): - precision = compute_precision(y_true, y_pred) - - correctly_classified = [] - badly_classified = [] - n_new_certs_with_match = 0 - n_newly_identified = 0 - - for (vendor, cert_name), predicted_cpes, verified_cpes in zip(x_valid, y_pred, y_true): - record = {'certificate_name': cert_name, - 'vendor': vendor, - 'heuristic version': helpers.compute_heuristics_version(cert_name), - 'predicted_cpes': list(predicted_cpes), - 'manually_assigned_cpes': list(verified_cpes) - } - if set(verified_cpes).issubset(set(predicted_cpes)): - correctly_classified.append(record) - else: - badly_classified.append(record) - - if len(verified_cpes) == 1 and len(predicted_cpes) > 1: - n_new_certs_with_match += 1 - n_newly_identified += len(set(predicted_cpes) - set(verified_cpes)) - - results = {'Precision': precision, 'n_new_certs_with_match': n_new_certs_with_match, 'n_newly_identified': n_newly_identified, 'correctly_classified': correctly_classified, 'badly_classified': badly_classified} - print(f'While keeping precision: {precision}, the classifier identified {n_newly_identified} new CPE matches (Found match for {n_new_certs_with_match} certificates that were previously unmatched) compared to baseline.') - - with Path(outpath).open('w') as handle: - json.dump(results, handle, indent=4)
\ No newline at end of file |
