Update metric template with ISCO-08 csv download and accuracy calculation
Browse files- metric_template_1.py +11 -16
metric_template_1.py
CHANGED
|
@@ -11,14 +11,12 @@
|
|
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
# See the License for the specific language governing permissions and
|
| 13 |
# limitations under the License.
|
| 14 |
-
"""
|
| 15 |
|
| 16 |
import evaluate
|
| 17 |
import datasets
|
| 18 |
import ham
|
| 19 |
-
import os
|
| 20 |
import isco
|
| 21 |
-
import json
|
| 22 |
|
| 23 |
|
| 24 |
# TODO: Add BibTeX citation
|
|
@@ -58,8 +56,12 @@ Examples:
|
|
| 58 |
"""
|
| 59 |
|
| 60 |
# TODO: Define external resources urls if needed
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
|
| 65 |
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
|
@@ -89,11 +91,8 @@ class MetricTemplate1(evaluate.Metric):
|
|
| 89 |
)
|
| 90 |
|
| 91 |
def _download_and_prepare(self, dl_manager):
|
| 92 |
-
"""
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
# Download and prepare the ISCO structure csv file
|
| 96 |
-
isco_csv = dl_manager.download_and_extract(ISCO_CSV_URL)
|
| 97 |
print(f"ISCO CSV file downloaded")
|
| 98 |
self.isco_hierarchy = isco.create_hierarchy_dict(isco_csv)
|
| 99 |
print("ISCO hierarchy dictionary created")
|
|
@@ -101,8 +100,6 @@ class MetricTemplate1(evaluate.Metric):
|
|
| 101 |
|
| 102 |
def _compute(self, predictions, references):
|
| 103 |
"""Returns the scores"""
|
| 104 |
-
# TODO: Compute the different scores of the module
|
| 105 |
-
|
| 106 |
# Convert the inputs to strings
|
| 107 |
predictions = [str(p) for p in predictions]
|
| 108 |
references = [str(r) for r in references]
|
|
@@ -111,11 +108,9 @@ class MetricTemplate1(evaluate.Metric):
|
|
| 111 |
accuracy = sum(i == j for i, j in zip(predictions, references)) / len(
|
| 112 |
predictions
|
| 113 |
)
|
|
|
|
| 114 |
|
| 115 |
-
#
|
| 116 |
-
# hierarchy = {"G": ["E"], "E": ["B"], "F": ["C"], "C": ["B"], "B": []}
|
| 117 |
-
# true_labels = [{'G'}]
|
| 118 |
-
# predicted_labels = [{'F'}]
|
| 119 |
hierarchy = self.isco_hierarchy
|
| 120 |
hP, hR = ham.calculate_hierarchical_precision_recall(
|
| 121 |
references, predictions, hierarchy
|
|
|
|
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
# See the License for the specific language governing permissions and
|
| 13 |
# limitations under the License.
|
| 14 |
+
"""Hierarchical Accuracy Metric."""
|
| 15 |
|
| 16 |
import evaluate
|
| 17 |
import datasets
|
| 18 |
import ham
|
|
|
|
| 19 |
import isco
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
# TODO: Add BibTeX citation
|
|
|
|
| 56 |
"""
|
| 57 |
|
| 58 |
# TODO: Define external resources urls if needed
|
| 59 |
+
ISCO_CSV_MIRROR_URL = (
|
| 60 |
+
"https://storage.googleapis.com/isco-public/tables/ISCO_structure.csv"
|
| 61 |
+
)
|
| 62 |
+
ILO_ISCO_CSV_URL = (
|
| 63 |
+
"https://www.ilo.org/ilostat-files/ISCO/newdocs-08-2021/ISCO-08/ISCO-08%20EN.csv"
|
| 64 |
+
)
|
| 65 |
|
| 66 |
|
| 67 |
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
|
|
|
| 91 |
)
|
| 92 |
|
| 93 |
def _download_and_prepare(self, dl_manager):
|
| 94 |
+
"""Download external ISCO-08 csv file for creating the hierarchy dictionary."""
|
| 95 |
+
isco_csv = dl_manager.download_and_extract(ISCO_CSV_MIRROR_URL)
|
|
|
|
|
|
|
|
|
|
| 96 |
print(f"ISCO CSV file downloaded")
|
| 97 |
self.isco_hierarchy = isco.create_hierarchy_dict(isco_csv)
|
| 98 |
print("ISCO hierarchy dictionary created")
|
|
|
|
| 100 |
|
| 101 |
def _compute(self, predictions, references):
|
| 102 |
"""Returns the scores"""
|
|
|
|
|
|
|
| 103 |
# Convert the inputs to strings
|
| 104 |
predictions = [str(p) for p in predictions]
|
| 105 |
references = [str(r) for r in references]
|
|
|
|
| 108 |
accuracy = sum(i == j for i, j in zip(predictions, references)) / len(
|
| 109 |
predictions
|
| 110 |
)
|
| 111 |
+
print(f"Accuracy: {accuracy}")
|
| 112 |
|
| 113 |
+
# Calculate hierarchical precision, recall and f-measure
|
|
|
|
|
|
|
|
|
|
| 114 |
hierarchy = self.isco_hierarchy
|
| 115 |
hP, hR = ham.calculate_hierarchical_precision_recall(
|
| 116 |
references, predictions, hierarchy
|