cannabis_results / cannabis_tests.py
Keegan Skeate
📜Cannabis Tests dataset docs + loading script
ab43142
raw
history blame
6.38 kB
"""
Cannabis Tests
Copyright (c) 2022 Cannlytics
Authors:
Keegan Skeate <https://github.com/keeganskeate>
Candace O'Sullivan-Sutherland <https://github.com/candy-o>
Created: 9/10/2022
Updated: 9/10/2022
License: <https://github.com/cannlytics/cannlytics/blob/main/LICENSE>
"""
import json
import datasets # pip install datasets
# Citation for using the dataset.
CANNABIS_TESTS_CITATION = """\
@inproceedings{cannlytics2022cannabis_tests,
author = {Skeate, Keegan and O'Sullivan-Sutherland, Candace},
title = {Cannabis Tests: Curated Cannabis Lab Test Results},
booktitle = {Cannabis Data Science},
month = {September},
year = {2022},
address = {United States of America},
publisher = {Cannlytics}
}
"""
CANNABIS_TESTS_DESCRIPTION = """\
Cannabis lab test results (https://cannlytics.com/data/tests) is a
dataset of curated cannabis lab test results.
"""
CANNABIS_TESTS_URL = 'https://huggingface.co/datasets/cannlytics/cannabis_tests'
# Raw Garden constants.
RAWGARDEN_DATA_URL = 'https://cannlytics.page.link/rawgarden'
RAWGARDEN_URL = 'https://github.com/cannlytics/cannlytics/tree/main/ai/curation/get_rawgarden_data'
RAWGARDEN_DESCRIPTION = """\
Raw Garden lab test results (https://cannlytics.com/data/tests) is a
dataset of curated cannabis lab test results from Raw Garden, a large
cannabis processor in California.
"""
RAWGARDEN_FEATURES = []
# MCR Labs constants.
MCRLABS_DATA_URL = 'https://cannlytics.page.link/mcrlabs'
MCRLABS_URL = 'https://github.com/cannlytics/cannlytics/tree/main/ai/curation/get_mcr_labs_data'
MCRLABS_DESCRIPTION = """\
MCR Labs lab test results (https://cannlytics.com/data/tests) is a
dataset of curated cannabis lab test results from MCR Labs, a lab that
tests cannabis in Massachusetts.
"""
MCRLABS_FEATURES = []
# PSI Labs constants.
PSILABS_DATA_URL = 'https://cannlytics.page.link/psilabs'
PSILABS_URL = 'https://github.com/cannlytics/cannlytics/tree/main/ai/curation/get_psi_labs_data'
PSILABS_DESCRIPTION = """\
PSI Labs lab test results (https://cannlytics.com/data/tests) is a
dataset of curated cannabis lab test results from PSI Labs, a lab that
tests cannabis in Michigan.
"""
PSILABS_FEATURES = []
# SC Labs constants.
SCLABS_DATA_URL = 'https://cannlytics.page.link/sclabs'
SCLABS_URL = 'https://github.com/cannlytics/cannlytics/tree/main/ai/curation/get_sc_labs_data'
SCLABS_DESCRIPTION = """\
SC Labs lab test results (https://cannlytics.com/data/tests) is a
dataset of curated cannabis lab test results from SC Labs, a lab that
tests cannabis in California.
"""
SCLABS_FEATURES = []
class CannabisTestsConfig(datasets.BuilderConfig):
"""BuilderConfig for Cannabis Tests."""
def __init__(
self,
features,
data_url,
citation,
url,
label_classes=('False', 'True'),
**kwargs
):
"""BuilderConfig for Cannabis Tests.
Args:
features (list[str]): A list of the features that will appear in
the feature dict. Should not include "label".
data_url (str): A URL to download the zip file from.
citation (str): A citation for the data set.
url (str): A URL for information about the data set.
label_classes (list[str]): The list of classes for the label if
the label is present as a string. Non-string labels will be
cast to either 'False' or 'True'.
**kwargs: Keyword arguments forwarded to super.
"""
super(CannabisTestsConfig, self).__init__(version=datasets.Version("1.0.2"), **kwargs)
self.features = features
self.label_classes = label_classes
self.data_url = data_url
self.citation = citation
self.url = url
class CannabisTests(datasets.GeneratorBasedBuilder):
"""The Cannabis Tests dataset."""
VERSION = datasets.Version('1.0.0')
BUILDER_CONFIGS = [
CannabisTestsConfig(
name='rawgarden',
description=RAWGARDEN_DESCRIPTION,
features=RAWGARDEN_FEATURES,
data_url=RAWGARDEN_DATA_URL,
citation=CANNABIS_TESTS_CITATION,
url=RAWGARDEN_URL,
),
CannabisTestsConfig(
name='mcrlabs',
description=MCRLABS_DESCRIPTION,
features=MCRLABS_FEATURES,
data_url=MCRLABS_DATA_URL,
citation=CANNABIS_TESTS_CITATION,
url=MCRLABS_URL,
),
CannabisTestsConfig(
name='psilabs',
description=PSILABS_DESCRIPTION,
features=PSILABS_FEATURES,
data_url=PSILABS_DATA_URL,
citation=CANNABIS_TESTS_CITATION,
url=PSILABS_URL,
),
CannabisTestsConfig(
name='sclabs',
description=SCLABS_DESCRIPTION,
features=SCLABS_FEATURES,
data_url=SCLABS_DATA_URL,
citation=CANNABIS_TESTS_CITATION,
url=SCLABS_URL,
),
]
DEFAULT_CONFIG_NAME = 'rawgarden'
def _info(self):
features = {feature: datasets.Value('string') for feature in self.config.features}
# TODO: Define all numeric features.
# features['span1_index'] = datasets.Value('int32')
# TODO: Define all image features.
# TODO: Define all sequence features.
# features["answers"] = datasets.features.Sequence(
# {"text": datasets.Value("string"), "answer_start": datasets.Value("int32"),}
# )
return datasets.DatasetInfo(
citation=CANNABIS_TESTS_CITATION,
description=CANNABIS_TESTS_DESCRIPTION,
features=features,
homepage=CANNABIS_TESTS_URL,
supervised_keys=None,
)
def _generate_examples(self, filepath):
"""This function returns the examples in raw (text) form."""
with open(filepath, encoding='utf-8') as f:
for line in f:
row = json.loads(line)
product_name = row.get('product_name', '').strip()
for i, result in enumerate(row['results']):
_id = str(i)
yield _id, {
'product_name': product_name,
'analyte': result.get('key', ''),
'value': result.get('value', 0),
}