Keegan Skeate commited on
Commit ·
43ca93b
1
Parent(s): 1dd369e
Added 👨🌾 Raw Garden lab result data + algorithm
Browse files- README.md +13 -4
- algorithms/get_all_rawgarden_data.py +440 -0
- cannabis_tests.py +198 -48
- dataset_infos.json +1 -0
README.md
CHANGED
|
@@ -56,21 +56,29 @@ The dataset is partitioned into the various sources of lab results.
|
|
| 56 |
|
| 57 |
| Source | Observations |
|
| 58 |
|--------|--------------|
|
| 59 |
-
| Raw Gardens | 2,
|
| 60 |
| MCR Labs | Coming soon! |
|
| 61 |
| PSI Labs | Coming soon! |
|
| 62 |
| SC Labs | Coming soon! |
|
| 63 |
|
| 64 |
### Data Instances
|
| 65 |
|
| 66 |
-
You can load
|
| 67 |
|
| 68 |
```py
|
| 69 |
from datasets import load_dataset
|
| 70 |
|
| 71 |
-
#
|
| 72 |
dataset = 'cannlytics/cannabis_tests'
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
```
|
| 75 |
|
| 76 |
### Data Fields
|
|
@@ -199,6 +207,7 @@ The data represents only a subset of the population of cannabis lab results. Non
|
|
| 199 |
| `'N/A'` | `None` |
|
| 200 |
| `'na'` | `None` |
|
| 201 |
| `'NT'` | `None` |
|
|
|
|
| 202 |
## Additional Information
|
| 203 |
|
| 204 |
### Dataset Curators
|
|
|
|
| 56 |
|
| 57 |
| Source | Observations |
|
| 58 |
|--------|--------------|
|
| 59 |
+
| Raw Gardens | 2,667 |
|
| 60 |
| MCR Labs | Coming soon! |
|
| 61 |
| PSI Labs | Coming soon! |
|
| 62 |
| SC Labs | Coming soon! |
|
| 63 |
|
| 64 |
### Data Instances
|
| 65 |
|
| 66 |
+
You can load `details`, `results`, and `values` for each of the dataset files. For example:
|
| 67 |
|
| 68 |
```py
|
| 69 |
from datasets import load_dataset
|
| 70 |
|
| 71 |
+
# Specify the Cannabis Tests dataset.
|
| 72 |
dataset = 'cannlytics/cannabis_tests'
|
| 73 |
+
|
| 74 |
+
# Load Raw Garden lab test details.
|
| 75 |
+
rawgarden_details = load_dataset(dataset, 'rawgarden_details')
|
| 76 |
+
|
| 77 |
+
# Load Raw Garden lab test results.
|
| 78 |
+
rawgarden_results = load_dataset(dataset, 'rawgarden_results')
|
| 79 |
+
|
| 80 |
+
# Load Raw Garden lab test values.
|
| 81 |
+
rawgarden_values = load_dataset(dataset, 'rawgarden_values')
|
| 82 |
```
|
| 83 |
|
| 84 |
### Data Fields
|
|
|
|
| 207 |
| `'N/A'` | `None` |
|
| 208 |
| `'na'` | `None` |
|
| 209 |
| `'NT'` | `None` |
|
| 210 |
+
|
| 211 |
## Additional Information
|
| 212 |
|
| 213 |
### Dataset Curators
|
algorithms/get_all_rawgarden_data.py
ADDED
|
@@ -0,0 +1,440 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Get Raw Garden Test Result Data
|
| 3 |
+
Copyright (c) 2022 Cannlytics
|
| 4 |
+
|
| 5 |
+
Authors:
|
| 6 |
+
Keegan Skeate <https://github.com/keeganskeate>
|
| 7 |
+
Candace O'Sullivan-Sutherland <https://github.com/candy-o>
|
| 8 |
+
Created: 8/23/2022
|
| 9 |
+
Updated: 9/13/2022
|
| 10 |
+
License: <https://github.com/cannlytics/cannlytics/blob/main/LICENSE>
|
| 11 |
+
|
| 12 |
+
Description:
|
| 13 |
+
|
| 14 |
+
Curate Raw Garden's publicly published lab results by:
|
| 15 |
+
|
| 16 |
+
1. Finding products and their COA URLS on Raw Garden's website.
|
| 17 |
+
2. Downloading COA PDFs from their URLs.
|
| 18 |
+
3. Using CoADoc to parse the COA PDFs (with OCR).
|
| 19 |
+
4. Archiving the COA data in Firestore.
|
| 20 |
+
|
| 21 |
+
Data Source:
|
| 22 |
+
|
| 23 |
+
- Raw Garden Lab Results
|
| 24 |
+
URL: <https://rawgarden.farm/lab-results/>
|
| 25 |
+
|
| 26 |
+
Command line usage:
|
| 27 |
+
|
| 28 |
+
python ai/curation/get_rawgarden_data/get_rawgarden_data.py \
|
| 29 |
+
--days_ago=1 --get_all=False
|
| 30 |
+
|
| 31 |
+
"""
|
| 32 |
+
# Standard imports.
|
| 33 |
+
from datetime import datetime, timedelta
|
| 34 |
+
import gc
|
| 35 |
+
import os
|
| 36 |
+
from time import sleep
|
| 37 |
+
from typing import Any, List, Optional, Tuple
|
| 38 |
+
|
| 39 |
+
# External imports.
|
| 40 |
+
from bs4 import BeautifulSoup
|
| 41 |
+
import pandas as pd
|
| 42 |
+
import requests
|
| 43 |
+
|
| 44 |
+
# Internal imports.
|
| 45 |
+
from cannlytics.data.coas import CoADoc
|
| 46 |
+
from cannlytics.data.data import create_hash
|
| 47 |
+
from cannlytics.firebase import (
|
| 48 |
+
get_document,
|
| 49 |
+
initialize_firebase,
|
| 50 |
+
update_documents,
|
| 51 |
+
upload_file,
|
| 52 |
+
)
|
| 53 |
+
from cannlytics.utils import kebab_case, rmerge
|
| 54 |
+
from cannlytics.utils.constants import DEFAULT_HEADERS
|
| 55 |
+
|
| 56 |
+
# Specify where your data lives.
|
| 57 |
+
BUCKET_NAME = 'cannlytics-company.appspot.com'
|
| 58 |
+
COLLECTION = 'public/data/lab_results'
|
| 59 |
+
STORAGE_REF = 'data/lab_results/raw_garden'
|
| 60 |
+
|
| 61 |
+
# Create directories if they don't already exist.
|
| 62 |
+
# TODO: Edit `ENV_FILE` and `DATA_DIR` as needed for your desired setup.
|
| 63 |
+
ENV_FILE = '../.env'
|
| 64 |
+
DATA_DIR = '.././'
|
| 65 |
+
COA_DATA_DIR = f'{DATA_DIR}/raw_garden'
|
| 66 |
+
COA_PDF_DIR = f'{COA_DATA_DIR}/pdfs'
|
| 67 |
+
TEMP_PATH = f'{COA_DATA_DIR}/tmp'
|
| 68 |
+
if not os.path.exists(DATA_DIR): os.makedirs(DATA_DIR)
|
| 69 |
+
if not os.path.exists(COA_DATA_DIR): os.makedirs(COA_DATA_DIR)
|
| 70 |
+
if not os.path.exists(COA_PDF_DIR): os.makedirs(COA_PDF_DIR)
|
| 71 |
+
if not os.path.exists(TEMP_PATH): os.makedirs(TEMP_PATH)
|
| 72 |
+
|
| 73 |
+
# Define constants.
|
| 74 |
+
BASE = 'https://rawgarden.farm/lab-results/'
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def get_rawgarden_products(
|
| 78 |
+
start: Optional[Any] = None,
|
| 79 |
+
end: Optional[Any] = None,
|
| 80 |
+
) -> pd.DataFrame:
|
| 81 |
+
"""Get Raw Garden's lab results page. Then get all of the product
|
| 82 |
+
categories. Finally, get all product data, including: `coa_pdf`,
|
| 83 |
+
`lab_results_url`, `product_name`, `product_subtype`, `date_retail`.
|
| 84 |
+
Args:
|
| 85 |
+
start (str or datetime): A point in time to begin restricting
|
| 86 |
+
the product list by `date_retail` (optional).
|
| 87 |
+
end (str or datetime): A point in time to end restricting
|
| 88 |
+
the product list by `date_retail` (optional).
|
| 89 |
+
Returns:
|
| 90 |
+
(DataFrame): Returns a DataFrame of product data.
|
| 91 |
+
"""
|
| 92 |
+
|
| 93 |
+
# Get the website.
|
| 94 |
+
response = requests.get(BASE, headers=DEFAULT_HEADERS)
|
| 95 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 96 |
+
|
| 97 |
+
# Get all product data listed on the website.
|
| 98 |
+
observations = []
|
| 99 |
+
categories = soup.find_all('div', attrs={'class': 'category-content'})
|
| 100 |
+
for category in categories:
|
| 101 |
+
subtype = category.find('h3').text
|
| 102 |
+
dates = category.findAll('h5', attrs={'class': 'result-date'})
|
| 103 |
+
names = category.findAll('h5')
|
| 104 |
+
names = [div for div in names if div.get('class') is None]
|
| 105 |
+
links = category.findAll('a')
|
| 106 |
+
for i, link in enumerate(links):
|
| 107 |
+
try:
|
| 108 |
+
href = link.get('href')
|
| 109 |
+
date = pd.to_datetime(dates[i].text)
|
| 110 |
+
name = names[i].text
|
| 111 |
+
if href.endswith('.pdf'):
|
| 112 |
+
observations.append({
|
| 113 |
+
'coa_pdf': href.split('/')[-1],
|
| 114 |
+
'lab_results_url': href,
|
| 115 |
+
'product_name': name,
|
| 116 |
+
'product_subtype': subtype,
|
| 117 |
+
'date_retail': date,
|
| 118 |
+
})
|
| 119 |
+
except AttributeError:
|
| 120 |
+
continue
|
| 121 |
+
|
| 122 |
+
# Restrict the observations to the desired time frame.
|
| 123 |
+
results = pd.DataFrame(observations)
|
| 124 |
+
dates = results['date_retail']
|
| 125 |
+
if start:
|
| 126 |
+
if isinstance(start, str):
|
| 127 |
+
latest = pd.to_datetime(start)
|
| 128 |
+
else:
|
| 129 |
+
latest = start
|
| 130 |
+
results = results.loc[dates >= latest]
|
| 131 |
+
if end:
|
| 132 |
+
if isinstance(end, str):
|
| 133 |
+
earliest = pd.to_datetime(end)
|
| 134 |
+
else:
|
| 135 |
+
earliest = end
|
| 136 |
+
results = results.loc[dates <= earliest]
|
| 137 |
+
results['date_retail'] = dates.apply(lambda x: x.isoformat()[:19])
|
| 138 |
+
return results
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def download_rawgarden_coas(
|
| 142 |
+
items: pd.DataFrame,
|
| 143 |
+
pause: Optional[float] = 0.24,
|
| 144 |
+
verbose: Optional[bool] = True,
|
| 145 |
+
) -> None:
|
| 146 |
+
"""Download Raw Garden product COAs to `product_subtype` folders.
|
| 147 |
+
Args:
|
| 148 |
+
items: (DataFrame): A DataFrame of products with `product_subtype`
|
| 149 |
+
and `lab_results_url` to download.
|
| 150 |
+
pause (float): A pause to respect the server serving the PDFs,
|
| 151 |
+
`0.24` seconds by default (optional).
|
| 152 |
+
verbose (bool): Whether or not to print status, `True` by
|
| 153 |
+
default (optional).
|
| 154 |
+
"""
|
| 155 |
+
if verbose:
|
| 156 |
+
total = len(items)
|
| 157 |
+
print('Downloading %i PDFs, ETA > %.2fs' % (total, total * pause))
|
| 158 |
+
|
| 159 |
+
# Create a folder of each of the subtypes.
|
| 160 |
+
subtypes = list(items['product_subtype'].unique())
|
| 161 |
+
for subtype in subtypes:
|
| 162 |
+
folder = kebab_case(subtype)
|
| 163 |
+
subtype_folder = f'{COA_PDF_DIR}/{folder}'
|
| 164 |
+
if not os.path.exists(subtype_folder):
|
| 165 |
+
os.makedirs(subtype_folder)
|
| 166 |
+
|
| 167 |
+
# Download each COA PDF from its URL to a `product_subtype` folder.
|
| 168 |
+
for i, row in enumerate(items.iterrows()):
|
| 169 |
+
item = row[1]
|
| 170 |
+
url = item['lab_results_url']
|
| 171 |
+
subtype = item['product_subtype']
|
| 172 |
+
filename = url.split('/')[-1]
|
| 173 |
+
folder = kebab_case(subtype)
|
| 174 |
+
outfile = os.path.join(COA_PDF_DIR, folder, filename)
|
| 175 |
+
response = requests.get(url, headers=DEFAULT_HEADERS)
|
| 176 |
+
with open(outfile, 'wb') as pdf:
|
| 177 |
+
pdf.write(response.content)
|
| 178 |
+
if verbose:
|
| 179 |
+
message = 'Downloaded {}/{} | {}/{}'
|
| 180 |
+
message = message.format(str(i + 1), str(total), folder, filename)
|
| 181 |
+
print(message)
|
| 182 |
+
sleep(pause)
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def parse_rawgarden_coas(
|
| 186 |
+
directory: str,
|
| 187 |
+
filenames: Optional[list] = None,
|
| 188 |
+
temp_path: Optional[str] = '/tmp',
|
| 189 |
+
verbose: Optional[bool] = True,
|
| 190 |
+
**kwargs,
|
| 191 |
+
) -> Tuple[list]:
|
| 192 |
+
"""Parse Raw Garden lab results with CoADoc.
|
| 193 |
+
Args:
|
| 194 |
+
directory (str): The directory of files to parse.
|
| 195 |
+
filenames (list): A list of files to parse (optional).
|
| 196 |
+
temp_path (str): A temporary directory to use for any OCR (optional).
|
| 197 |
+
verbose (bool): Whether or not to print status, `True` by
|
| 198 |
+
default (optional).
|
| 199 |
+
Returns:
|
| 200 |
+
(tuple): Returns both a list of parsed and unidentified COA data.
|
| 201 |
+
"""
|
| 202 |
+
parsed, unidentified = [], []
|
| 203 |
+
started = False
|
| 204 |
+
for path, _, files in os.walk(directory):
|
| 205 |
+
if verbose and not started:
|
| 206 |
+
started = True
|
| 207 |
+
if filenames:
|
| 208 |
+
total = len(filenames)
|
| 209 |
+
else:
|
| 210 |
+
total = len(files)
|
| 211 |
+
print('Parsing %i COAs, ETA > %.2fm' % (total, total * 25 / 60))
|
| 212 |
+
for filename in files:
|
| 213 |
+
if not filename.endswith('.pdf'):
|
| 214 |
+
continue
|
| 215 |
+
if filenames is not None:
|
| 216 |
+
if filename not in filenames:
|
| 217 |
+
continue
|
| 218 |
+
file_path = os.path.join(path, filename)
|
| 219 |
+
|
| 220 |
+
# Parse the COA, by any means necessary!
|
| 221 |
+
parser = CoADoc()
|
| 222 |
+
try:
|
| 223 |
+
new_data = parser.parse_pdf(
|
| 224 |
+
file_path,
|
| 225 |
+
temp_path=temp_path,
|
| 226 |
+
**kwargs
|
| 227 |
+
)
|
| 228 |
+
except:
|
| 229 |
+
try:
|
| 230 |
+
# FIXME: This should work without directly calling OCR.
|
| 231 |
+
temp_file = f'{temp_path}/ocr_coa.pdf'
|
| 232 |
+
parser.pdf_ocr(
|
| 233 |
+
file_path,
|
| 234 |
+
temp_file,
|
| 235 |
+
temp_path,
|
| 236 |
+
resolution=180,
|
| 237 |
+
)
|
| 238 |
+
new_data = parser.parse_pdf(
|
| 239 |
+
temp_file,
|
| 240 |
+
temp_path=temp_path,
|
| 241 |
+
**kwargs
|
| 242 |
+
)
|
| 243 |
+
except Exception as e:
|
| 244 |
+
# Hot-fix: Remove temporary `magick-*` files.
|
| 245 |
+
for i in os.listdir(temp_path):
|
| 246 |
+
magick_path = os.path.join(temp_path, i)
|
| 247 |
+
if os.path.isfile(magick_path) and i.startswith('magick-'):
|
| 248 |
+
os.remove(magick_path)
|
| 249 |
+
unidentified.append({'coa_pdf': filename})
|
| 250 |
+
if verbose:
|
| 251 |
+
print('Error:', filename)
|
| 252 |
+
print(e)
|
| 253 |
+
continue
|
| 254 |
+
|
| 255 |
+
# Add the subtype key and record the data.
|
| 256 |
+
subtype = path.split('\\')[-1]
|
| 257 |
+
if isinstance(new_data, dict):
|
| 258 |
+
new_data = [new_data]
|
| 259 |
+
new_data[0]['product_subtype'] = subtype
|
| 260 |
+
parsed.extend(new_data)
|
| 261 |
+
parser.quit()
|
| 262 |
+
gc.collect()
|
| 263 |
+
if verbose:
|
| 264 |
+
print('Parsed:', filename)
|
| 265 |
+
|
| 266 |
+
return parsed, unidentified
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
def upload_lab_results(
|
| 270 |
+
observations: List[dict],
|
| 271 |
+
collection: Optional[str] = None,
|
| 272 |
+
database: Optional[Any] = None,
|
| 273 |
+
update: Optional[bool] = True,
|
| 274 |
+
verbose: Optional[bool] = True,
|
| 275 |
+
) -> None:
|
| 276 |
+
"""Upload lab results to Firestore.
|
| 277 |
+
Args:
|
| 278 |
+
observations (list): A list of lab results to upload.
|
| 279 |
+
collection (str): The Firestore collection where lab results live,
|
| 280 |
+
`'public/data/lab_results'` by default (optional).
|
| 281 |
+
database (Client): A Firestore database instance (optional).
|
| 282 |
+
update (bool): Whether or not to update existing entries, `True`
|
| 283 |
+
by default (optional).
|
| 284 |
+
verbose (bool): Whether or not to print status, `True` by
|
| 285 |
+
default (optional).
|
| 286 |
+
"""
|
| 287 |
+
if collection is None:
|
| 288 |
+
collection = COLLECTION
|
| 289 |
+
if database is None:
|
| 290 |
+
database = initialize_firebase()
|
| 291 |
+
refs, updates = [], []
|
| 292 |
+
for obs in observations:
|
| 293 |
+
sample_id = obs['sample_id']
|
| 294 |
+
ref = f'{collection}/{sample_id}'
|
| 295 |
+
if not update:
|
| 296 |
+
doc = get_document(ref)
|
| 297 |
+
if doc is not None:
|
| 298 |
+
continue
|
| 299 |
+
refs.append(ref)
|
| 300 |
+
updates.append(obs)
|
| 301 |
+
if updates:
|
| 302 |
+
if verbose:
|
| 303 |
+
print('Uploading %i lab results.' % len(refs))
|
| 304 |
+
update_documents(refs, updates, database=database)
|
| 305 |
+
if verbose:
|
| 306 |
+
print('Uploaded %i lab results.' % len(refs))
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
#-----------------------------------------------------------------------
|
| 310 |
+
# EXAMPLE: Collect Raw Garden lab results data by:
|
| 311 |
+
#
|
| 312 |
+
# 1. Finding products and their COA URLS.
|
| 313 |
+
# 2. Downloading COA PDFs from their URLs.
|
| 314 |
+
# 3. Using CoADoc to parse the COA PDFs (with OCR).
|
| 315 |
+
# 4. Saving the data to datafiles, Firebase Storage, and Firestore.
|
| 316 |
+
#
|
| 317 |
+
#-----------------------------------------------------------------------
|
| 318 |
+
if __name__ == '__main__':
|
| 319 |
+
|
| 320 |
+
# === Setup ===
|
| 321 |
+
|
| 322 |
+
# Support command line usage.
|
| 323 |
+
# Future work: Allow data dirs to be specified from the command line.
|
| 324 |
+
import argparse
|
| 325 |
+
try:
|
| 326 |
+
parser = argparse.ArgumentParser()
|
| 327 |
+
parser.add_argument('--days_ago', dest='days_ago', type=int)
|
| 328 |
+
parser.add_argument('--get_all', dest='get_all', type=bool)
|
| 329 |
+
args = parser.parse_args()
|
| 330 |
+
except SystemExit:
|
| 331 |
+
args = {}
|
| 332 |
+
|
| 333 |
+
# Specify collection period.
|
| 334 |
+
DAYS_AGO = args.get('days_ago', 1)
|
| 335 |
+
GET_ALL = args.get('get_all', True)
|
| 336 |
+
|
| 337 |
+
# === Data Collection ===
|
| 338 |
+
|
| 339 |
+
# Get the most recent Raw Garden products.
|
| 340 |
+
start = datetime.now() - timedelta(days=DAYS_AGO)
|
| 341 |
+
if GET_ALL:
|
| 342 |
+
start = datetime(year=2018, month=1, day=1)
|
| 343 |
+
products = get_rawgarden_products(start=start)
|
| 344 |
+
filenames = products['coa_pdf'].to_list()
|
| 345 |
+
|
| 346 |
+
# Download Raw Garden product COAs to `product_subtype` folders.
|
| 347 |
+
download_rawgarden_coas(products, pause=0.24, verbose=True)
|
| 348 |
+
|
| 349 |
+
# === Data Curation ===
|
| 350 |
+
|
| 351 |
+
# Parse COA PDFs with CoADoc.
|
| 352 |
+
coa_data, unidentified_coas = parse_rawgarden_coas(
|
| 353 |
+
COA_PDF_DIR,
|
| 354 |
+
filenames=filenames,
|
| 355 |
+
temp_path=TEMP_PATH,
|
| 356 |
+
verbose=True,
|
| 357 |
+
)
|
| 358 |
+
|
| 359 |
+
# Merge the `products`'s `product_subtype` with the COA data.
|
| 360 |
+
coa_df = rmerge(
|
| 361 |
+
pd.DataFrame(coa_data),
|
| 362 |
+
products,
|
| 363 |
+
on='coa_pdf',
|
| 364 |
+
how='left',
|
| 365 |
+
replace='right',
|
| 366 |
+
)
|
| 367 |
+
|
| 368 |
+
# Create hashes.
|
| 369 |
+
coa_df = coa_df.where(pd.notnull(coa_df), None)
|
| 370 |
+
coa_df['results_hash'] = coa_df['results'].apply(
|
| 371 |
+
lambda x: create_hash(x),
|
| 372 |
+
)
|
| 373 |
+
coa_df['sample_hash'] = coa_df.loc[:, coa_df.columns != 'sample_hash'].apply(
|
| 374 |
+
lambda x: create_hash(x.to_dict()),
|
| 375 |
+
axis=1,
|
| 376 |
+
)
|
| 377 |
+
datafile_hash = create_hash(coa_df)
|
| 378 |
+
|
| 379 |
+
# === Data Archiving ===
|
| 380 |
+
|
| 381 |
+
# Create custom column order.
|
| 382 |
+
column_order = ['sample_hash', 'results_hash']
|
| 383 |
+
column_order += list(parser.column_order)
|
| 384 |
+
index = column_order.index('product_type') + 1
|
| 385 |
+
column_order.insert(index, 'product_subtype')
|
| 386 |
+
|
| 387 |
+
# Optional: Save the COA data to a workbook.
|
| 388 |
+
parser = CoADoc()
|
| 389 |
+
datafile = f'{COA_DATA_DIR}/{datafile_hash}.xlsx'
|
| 390 |
+
parser.save(coa_df, datafile, column_order=column_order)
|
| 391 |
+
|
| 392 |
+
# Optional: Save the unidentified COA data.
|
| 393 |
+
errors = [x['coa_pdf'] for x in unidentified_coas]
|
| 394 |
+
timestamp = datetime.now().isoformat()[:19].replace(':', '-')
|
| 395 |
+
error_file = f'{COA_DATA_DIR}/rawgarden-unidentified-coas-{timestamp}.xlsx'
|
| 396 |
+
products.loc[products['coa_pdf'].isin(errors)].to_excel(error_file)
|
| 397 |
+
|
| 398 |
+
# === Firebase Database and Storage ===
|
| 399 |
+
|
| 400 |
+
# Optional: Initialize Firebase.
|
| 401 |
+
initialize_firebase(ENV_FILE)
|
| 402 |
+
|
| 403 |
+
# Optional: Upload the lab results to Firestore.
|
| 404 |
+
upload_lab_results(
|
| 405 |
+
coa_df.to_dict(orient='records'),
|
| 406 |
+
update=True,
|
| 407 |
+
verbose=True
|
| 408 |
+
)
|
| 409 |
+
|
| 410 |
+
# Optional: Upload datafiles to Firebase Storage.
|
| 411 |
+
storage_datafile = '/'.join([STORAGE_REF, datafile.split('/')[-1]])
|
| 412 |
+
storage_error_file = '/'.join([STORAGE_REF, error_file.split('/')[-1]])
|
| 413 |
+
upload_file(storage_datafile, datafile, bucket_name=BUCKET_NAME)
|
| 414 |
+
upload_file(storage_error_file, error_file, bucket_name=BUCKET_NAME)
|
| 415 |
+
|
| 416 |
+
# == Data Aggregation ===
|
| 417 |
+
|
| 418 |
+
# # Initialize the COA parser.
|
| 419 |
+
# parser = CoADoc()
|
| 420 |
+
|
| 421 |
+
# # Stack COA datafiles, re-hash, and re-save!
|
| 422 |
+
# datafiles = [
|
| 423 |
+
# f'{COA_DATA_DIR}/d7815fd2a097d06b719aadcc00233026f86076a680db63c532a11b67d7c8bc70.xlsx',
|
| 424 |
+
# f'{COA_DATA_DIR}/01880e30f092cf5739f9f2b58de705fc4c245d6859c00b50505a3a802ff7c2b2.xlsx',
|
| 425 |
+
# f'{COA_DATA_DIR}/154de9b1992a1bfd9a07d2e52c702e8437596923f34bee43f62f3e24f042b81c.xlsx',
|
| 426 |
+
# ]
|
| 427 |
+
|
| 428 |
+
# # Create custom column order.
|
| 429 |
+
# column_order = ['sample_hash', 'results_hash']
|
| 430 |
+
# column_order += list(parser.column_order)
|
| 431 |
+
# index = column_order.index('product_type') + 1
|
| 432 |
+
# column_order.insert(index, 'product_subtype')
|
| 433 |
+
|
| 434 |
+
# # Aggregate the datafiles.
|
| 435 |
+
# master_data = parser.aggregate(
|
| 436 |
+
# datafiles,
|
| 437 |
+
# output=COA_DATA_DIR,
|
| 438 |
+
# sheet_name='Details',
|
| 439 |
+
# column_order=column_order,
|
| 440 |
+
# )
|
cannabis_tests.py
CHANGED
|
@@ -6,11 +6,11 @@ Authors:
|
|
| 6 |
Keegan Skeate <https://github.com/keeganskeate>
|
| 7 |
Candace O'Sullivan-Sutherland <https://github.com/candy-o>
|
| 8 |
Created: 9/10/2022
|
| 9 |
-
Updated: 9/
|
| 10 |
License: <https://github.com/cannlytics/cannlytics/blob/main/LICENSE>
|
| 11 |
"""
|
| 12 |
-
import json
|
| 13 |
import datasets # pip install datasets
|
|
|
|
| 14 |
|
| 15 |
# Citation for using the dataset.
|
| 16 |
CANNABIS_TESTS_CITATION = """\
|
|
@@ -31,7 +31,10 @@ dataset of curated cannabis lab test results.
|
|
| 31 |
CANNABIS_TESTS_URL = 'https://huggingface.co/datasets/cannlytics/cannabis_tests'
|
| 32 |
|
| 33 |
# Raw Garden constants.
|
| 34 |
-
RAWGARDEN_DATA_URL = 'https://cannlytics.page.link/rawgarden'
|
|
|
|
|
|
|
|
|
|
| 35 |
RAWGARDEN_URL = 'https://github.com/cannlytics/cannlytics/tree/main/ai/curation/get_rawgarden_data'
|
| 36 |
RAWGARDEN_DESCRIPTION = """\
|
| 37 |
Raw Garden lab test results (https://cannlytics.com/data/tests) is a
|
|
@@ -39,6 +42,131 @@ dataset of curated cannabis lab test results from Raw Garden, a large
|
|
| 39 |
cannabis processor in California.
|
| 40 |
"""
|
| 41 |
RAWGARDEN_FEATURES = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# MCR Labs constants.
|
| 44 |
MCRLABS_DATA_URL = 'https://cannlytics.page.link/mcrlabs'
|
|
@@ -110,72 +238,94 @@ class CannabisTests(datasets.GeneratorBasedBuilder):
|
|
| 110 |
|
| 111 |
BUILDER_CONFIGS = [
|
| 112 |
CannabisTestsConfig(
|
| 113 |
-
name='
|
| 114 |
description=RAWGARDEN_DESCRIPTION,
|
| 115 |
-
features=
|
| 116 |
-
data_url=
|
| 117 |
citation=CANNABIS_TESTS_CITATION,
|
| 118 |
url=RAWGARDEN_URL,
|
| 119 |
),
|
| 120 |
-
CannabisTestsConfig(
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
),
|
| 128 |
-
CannabisTestsConfig(
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
),
|
| 136 |
-
CannabisTestsConfig(
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
]
|
| 145 |
|
| 146 |
-
DEFAULT_CONFIG_NAME = '
|
| 147 |
|
| 148 |
def _info(self):
|
| 149 |
-
features = {feature: datasets.Value('string') for feature in self.config.features}
|
| 150 |
|
| 151 |
# TODO: Define all numeric features.
|
| 152 |
-
# features['span1_index'] = datasets.Value('int32')
|
| 153 |
|
| 154 |
# TODO: Define all image features.
|
| 155 |
|
| 156 |
# TODO: Define all sequence features.
|
| 157 |
# features["answers"] = datasets.features.Sequence(
|
| 158 |
-
# {"text": datasets.Value("string"), "answer_start": datasets.Value("int32"),}
|
| 159 |
# )
|
| 160 |
|
| 161 |
return datasets.DatasetInfo(
|
| 162 |
citation=CANNABIS_TESTS_CITATION,
|
| 163 |
description=CANNABIS_TESTS_DESCRIPTION,
|
| 164 |
-
features=
|
| 165 |
homepage=CANNABIS_TESTS_URL,
|
| 166 |
supervised_keys=None,
|
| 167 |
)
|
| 168 |
|
| 169 |
-
def _generate_examples(self, filepath):
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
Keegan Skeate <https://github.com/keeganskeate>
|
| 7 |
Candace O'Sullivan-Sutherland <https://github.com/candy-o>
|
| 8 |
Created: 9/10/2022
|
| 9 |
+
Updated: 9/13/2022
|
| 10 |
License: <https://github.com/cannlytics/cannlytics/blob/main/LICENSE>
|
| 11 |
"""
|
|
|
|
| 12 |
import datasets # pip install datasets
|
| 13 |
+
# from datasets import Sequence, datasets.Value
|
| 14 |
|
| 15 |
# Citation for using the dataset.
|
| 16 |
CANNABIS_TESTS_CITATION = """\
|
|
|
|
| 31 |
CANNABIS_TESTS_URL = 'https://huggingface.co/datasets/cannlytics/cannabis_tests'
|
| 32 |
|
| 33 |
# Raw Garden constants.
|
| 34 |
+
# RAWGARDEN_DATA_URL = 'https://cannlytics.page.link/rawgarden'
|
| 35 |
+
RAWGARDEN_DETAILS_URL = 'https://cannlytics.page.link/rawgarden-details'
|
| 36 |
+
RAWGARDEN_RESULTS_URL = 'https://cannlytics.page.link/rawgarden-results'
|
| 37 |
+
RAWGARDEN_VALUES_URL = 'https://cannlytics.page.link/rawgarden-values'
|
| 38 |
RAWGARDEN_URL = 'https://github.com/cannlytics/cannlytics/tree/main/ai/curation/get_rawgarden_data'
|
| 39 |
RAWGARDEN_DESCRIPTION = """\
|
| 40 |
Raw Garden lab test results (https://cannlytics.com/data/tests) is a
|
|
|
|
| 42 |
cannabis processor in California.
|
| 43 |
"""
|
| 44 |
RAWGARDEN_FEATURES = []
|
| 45 |
+
RAWGARDEN_RESULTS = {
|
| 46 |
+
'sample_hash': datasets.Value(dtype='string', id=None),
|
| 47 |
+
'results_hash': datasets.Value(dtype='string', id=None),
|
| 48 |
+
'sample_id': datasets.Value(dtype='string', id=None),
|
| 49 |
+
'product_name': datasets.Value(dtype='string', id=None),
|
| 50 |
+
'producer': datasets.Value(dtype='string', id=None),
|
| 51 |
+
'product_type': datasets.Value(dtype='string', id=None),
|
| 52 |
+
'product_subtype': datasets.Value(dtype='string', id=None),
|
| 53 |
+
'date_tested': datasets.Value(dtype='string', id=None),
|
| 54 |
+
'analysis': datasets.Value(dtype='string', id=None),
|
| 55 |
+
'key': datasets.Value(dtype='string', id=None),
|
| 56 |
+
'limit': datasets.Value(dtype='double', id=None),
|
| 57 |
+
'lod': datasets.Value(dtype='double', id=None),
|
| 58 |
+
'lodloq': datasets.Value(dtype='double', id=None),
|
| 59 |
+
'loq': datasets.Value(dtype='double', id=None),
|
| 60 |
+
'margin_of_error': datasets.Value(dtype='double', id=None),
|
| 61 |
+
'mg_g': datasets.Value(dtype='double', id=None),
|
| 62 |
+
'name': datasets.Value(dtype='string', id=None),
|
| 63 |
+
'status': datasets.Value(dtype='string', id=None),
|
| 64 |
+
'units': datasets.Value(dtype='string', id=None),
|
| 65 |
+
'value': datasets.Value(dtype='double', id=None),
|
| 66 |
+
}
|
| 67 |
+
RAWGARDEN_DETAILS = {
|
| 68 |
+
'sample_hash': datasets.Value(dtype='string', id=None),
|
| 69 |
+
'results_hash': datasets.Value(dtype='string', id=None),
|
| 70 |
+
'sample_id': datasets.Value(dtype='string', id=None),
|
| 71 |
+
'product_name': datasets.Value(dtype='string', id=None),
|
| 72 |
+
'producer': datasets.Value(dtype='string', id=None),
|
| 73 |
+
'product_type': datasets.Value(dtype='string', id=None),
|
| 74 |
+
'product_subtype': datasets.Value(dtype='string', id=None),
|
| 75 |
+
'date_tested': datasets.Value(dtype='string', id=None),
|
| 76 |
+
'analyses': datasets.Value(dtype='string', id=None),
|
| 77 |
+
'batch_number': datasets.Value(dtype='string', id=None),
|
| 78 |
+
'batch_size': datasets.Value(dtype='string', id=None),
|
| 79 |
+
'batch_units': datasets.Value(dtype='string', id=None),
|
| 80 |
+
'cannabinoids_method': datasets.Value(dtype='string', id=None),
|
| 81 |
+
'cannabinoids_status': datasets.Value(dtype='string', id=None),
|
| 82 |
+
'coa_algorithm': datasets.Value(dtype='string', id=None),
|
| 83 |
+
'coa_algorithm_entry_point': datasets.Value(dtype='string', id=None),
|
| 84 |
+
'coa_parsed_at': datasets.Value(dtype='string', id=None),
|
| 85 |
+
'coa_pdf': datasets.Value(dtype='string', id=None),
|
| 86 |
+
'coa_urls': datasets.Value(dtype='string', id=None),
|
| 87 |
+
'date_collected': datasets.Value(dtype='string', id=None),
|
| 88 |
+
'date_produced': datasets.Value(dtype='string', id=None),
|
| 89 |
+
'date_received': datasets.Value(dtype='string', id=None),
|
| 90 |
+
'date_retail': datasets.Value(dtype='string', id=None),
|
| 91 |
+
'delta_9_thc_per_unit': datasets.Value(dtype='string', id=None),
|
| 92 |
+
'distributor': datasets.Value(dtype='string', id=None),
|
| 93 |
+
'distributor_address': datasets.Value(dtype='string', id=None),
|
| 94 |
+
'distributor_city': datasets.Value(dtype='string', id=None),
|
| 95 |
+
'distributor_license_number': datasets.Value(dtype='string', id=None),
|
| 96 |
+
'distributor_state': datasets.Value(dtype='string', id=None),
|
| 97 |
+
'distributor_street': datasets.Value(dtype='string', id=None),
|
| 98 |
+
'distributor_zipcode': datasets.Value(dtype='float64', id=None),
|
| 99 |
+
'foreign_matter_method': datasets.Value(dtype='string', id=None),
|
| 100 |
+
'foreign_matter_status': datasets.Value(dtype='string', id=None),
|
| 101 |
+
'heavy_metals_method': datasets.Value(dtype='string', id=None),
|
| 102 |
+
'heavy_metals_status': datasets.Value(dtype='string', id=None),
|
| 103 |
+
'images': datasets.Value(dtype='string', id=None),
|
| 104 |
+
'lab': datasets.Value(dtype='string', id=None),
|
| 105 |
+
'lab_address': datasets.Value(dtype='string', id=None),
|
| 106 |
+
'lab_city': datasets.Value(dtype='string', id=None),
|
| 107 |
+
'lab_county': datasets.Value(dtype='string', id=None),
|
| 108 |
+
'lab_email': datasets.Value(dtype='string', id=None),
|
| 109 |
+
'lab_id': datasets.Value(dtype='string', id=None),
|
| 110 |
+
'lab_image_url': datasets.Value(dtype='string', id=None),
|
| 111 |
+
'lab_latitude': datasets.Value(dtype='float64', id=None),
|
| 112 |
+
'lab_license_number': datasets.Value(dtype='string', id=None),
|
| 113 |
+
'lab_longitude': datasets.Value(dtype='float64', id=None),
|
| 114 |
+
'lab_phone': datasets.Value(dtype='string', id=None),
|
| 115 |
+
'lab_results_url': datasets.Value(dtype='string', id=None),
|
| 116 |
+
'lab_state': datasets.Value(dtype='string', id=None),
|
| 117 |
+
'lab_street': datasets.Value(dtype='string', id=None),
|
| 118 |
+
'lab_website': datasets.Value(dtype='string', id=None),
|
| 119 |
+
'lab_zipcode': datasets.Value(dtype='int64', id=None),
|
| 120 |
+
'lims': datasets.Value(dtype='string', id=None),
|
| 121 |
+
'metrc_ids': datasets.Value(dtype='string', id=None),
|
| 122 |
+
'metrc_lab_id': datasets.Value(dtype='string', id=None),
|
| 123 |
+
'metrc_source_id': datasets.Value(dtype='string', id=None),
|
| 124 |
+
'microbes_method': datasets.Value(dtype='string', id=None),
|
| 125 |
+
'microbes_status': datasets.Value(dtype='string', id=None),
|
| 126 |
+
'moisture_content': datasets.Value(dtype='string', id=None),
|
| 127 |
+
'moisture_method': datasets.Value(dtype='string', id=None),
|
| 128 |
+
'mycotoxins_method': datasets.Value(dtype='string', id=None),
|
| 129 |
+
'mycotoxins_status': datasets.Value(dtype='string', id=None),
|
| 130 |
+
'notes': datasets.Value(dtype='string', id=None),
|
| 131 |
+
'pesticides_method': datasets.Value(dtype='string', id=None),
|
| 132 |
+
'pesticides_status': datasets.Value(dtype='string', id=None),
|
| 133 |
+
'producer_address': datasets.Value(dtype='string', id=None),
|
| 134 |
+
'producer_city': datasets.Value(dtype='string', id=None),
|
| 135 |
+
'producer_image_url': datasets.Value(dtype='string', id=None),
|
| 136 |
+
'producer_license_number': datasets.Value(dtype='string', id=None),
|
| 137 |
+
'producer_state': datasets.Value(dtype='string', id=None),
|
| 138 |
+
'producer_street': datasets.Value(dtype='string', id=None),
|
| 139 |
+
'producer_url': datasets.Value(dtype='string', id=None),
|
| 140 |
+
'producer_zipcode': datasets.Value(dtype='float64', id=None),
|
| 141 |
+
'product_size': datasets.Value(dtype='string', id=None),
|
| 142 |
+
'public': datasets.Value(dtype='float64', id=None),
|
| 143 |
+
'residual_solvents_method': datasets.Value(dtype='string', id=None),
|
| 144 |
+
'residual_solvents_status': datasets.Value(dtype='string', id=None),
|
| 145 |
+
'results': datasets.Value(dtype='string', id=None),
|
| 146 |
+
'sample_number': datasets.Value(dtype='float64', id=None),
|
| 147 |
+
'sample_size': datasets.Value(dtype='string', id=None),
|
| 148 |
+
'sampling_method': datasets.Value(dtype='string', id=None),
|
| 149 |
+
'serving_size': datasets.Value(dtype='string', id=None),
|
| 150 |
+
'status': datasets.Value(dtype='string', id=None),
|
| 151 |
+
'sum_of_cannabinoids': datasets.Value(dtype='float64', id=None),
|
| 152 |
+
'terpenes_method': datasets.Value(dtype='string', id=None),
|
| 153 |
+
'terpenes_status': datasets.Value(dtype='string', id=None),
|
| 154 |
+
'total_cannabinoids': datasets.Value(dtype='float64', id=None),
|
| 155 |
+
'total_cbc': datasets.Value(dtype='float64', id=None),
|
| 156 |
+
'total_cbd': datasets.Value(dtype='float64', id=None),
|
| 157 |
+
'total_cbdv': datasets.Value(dtype='float64', id=None),
|
| 158 |
+
'total_cbg': datasets.Value(dtype='float64', id=None),
|
| 159 |
+
'total_terpenes': datasets.Value(dtype='float64', id=None),
|
| 160 |
+
'total_terpenes_mg_g': datasets.Value(dtype='float64', id=None),
|
| 161 |
+
'total_thc': datasets.Value(dtype='float64', id=None),
|
| 162 |
+
'total_thcv': datasets.Value(dtype='float64', id=None),
|
| 163 |
+
'url': datasets.Value(dtype='string', id=None),
|
| 164 |
+
'water_activity_method': datasets.Value(dtype='string', id=None),
|
| 165 |
+
'water_activity_status': datasets.Value(dtype='string', id=None)
|
| 166 |
+
}
|
| 167 |
+
# RAWGARDEN_VALUES = {
|
| 168 |
+
# 'delta_9_thc': datasets.Value(dtype='float64', id=None),
|
| 169 |
+
# }
|
| 170 |
|
| 171 |
# MCR Labs constants.
|
| 172 |
MCRLABS_DATA_URL = 'https://cannlytics.page.link/mcrlabs'
|
|
|
|
| 238 |
|
| 239 |
BUILDER_CONFIGS = [
|
| 240 |
CannabisTestsConfig(
|
| 241 |
+
name='rawgarden_details',
|
| 242 |
description=RAWGARDEN_DESCRIPTION,
|
| 243 |
+
features=RAWGARDEN_DETAILS,
|
| 244 |
+
data_url=RAWGARDEN_DETAILS_URL,
|
| 245 |
citation=CANNABIS_TESTS_CITATION,
|
| 246 |
url=RAWGARDEN_URL,
|
| 247 |
),
|
| 248 |
+
# CannabisTestsConfig(
|
| 249 |
+
# name='rawgarden_results',
|
| 250 |
+
# description=RAWGARDEN_DESCRIPTION,
|
| 251 |
+
# features=RAWGARDEN_RESULTS,
|
| 252 |
+
# data_url=RAWGARDEN_RESULTS_URL,
|
| 253 |
+
# citation=CANNABIS_TESTS_CITATION,
|
| 254 |
+
# url=RAWGARDEN_URL,
|
| 255 |
+
# ),
|
| 256 |
+
# CannabisTestsConfig(
|
| 257 |
+
# name='rawgarden_values',
|
| 258 |
+
# description=RAWGARDEN_DESCRIPTION,
|
| 259 |
+
# features=RAWGARDEN_VALUES,
|
| 260 |
+
# data_url=RAWGARDEN_VALUES_URL,
|
| 261 |
+
# citation=CANNABIS_TESTS_CITATION,
|
| 262 |
+
# url=RAWGARDEN_URL,
|
| 263 |
+
# ),
|
| 264 |
+
# CannabisTestsConfig(
|
| 265 |
+
# name='mcrlabs',
|
| 266 |
+
# description=MCRLABS_DESCRIPTION,
|
| 267 |
+
# features=MCRLABS_FEATURES,
|
| 268 |
+
# data_url=MCRLABS_DATA_URL,
|
| 269 |
+
# citation=CANNABIS_TESTS_CITATION,
|
| 270 |
+
# url=MCRLABS_URL,
|
| 271 |
+
# ),
|
| 272 |
+
# CannabisTestsConfig(
|
| 273 |
+
# name='psilabs',
|
| 274 |
+
# description=PSILABS_DESCRIPTION,
|
| 275 |
+
# features=PSILABS_FEATURES,
|
| 276 |
+
# data_url=PSILABS_DATA_URL,
|
| 277 |
+
# citation=CANNABIS_TESTS_CITATION,
|
| 278 |
+
# url=PSILABS_URL,
|
| 279 |
+
# ),
|
| 280 |
+
# CannabisTestsConfig(
|
| 281 |
+
# name='sclabs',
|
| 282 |
+
# description=SCLABS_DESCRIPTION,
|
| 283 |
+
# features=SCLABS_FEATURES,
|
| 284 |
+
# data_url=SCLABS_DATA_URL,
|
| 285 |
+
# citation=CANNABIS_TESTS_CITATION,
|
| 286 |
+
# url=SCLABS_URL,
|
| 287 |
+
# ),
|
| 288 |
]
|
| 289 |
|
| 290 |
+
DEFAULT_CONFIG_NAME = 'rawgarden_details'
|
| 291 |
|
| 292 |
def _info(self):
|
| 293 |
+
# features = {feature: datasets.datasets.Value('string') for feature in self.config.features}
|
| 294 |
|
| 295 |
# TODO: Define all numeric features.
|
| 296 |
+
# features['span1_index'] = datasets.datasets.Value('int32')
|
| 297 |
|
| 298 |
# TODO: Define all image features.
|
| 299 |
|
| 300 |
# TODO: Define all sequence features.
|
| 301 |
# features["answers"] = datasets.features.Sequence(
|
| 302 |
+
# {"text": datasets.datasets.Value("string"), "answer_start": datasets.datasets.Value("int32"),}
|
| 303 |
# )
|
| 304 |
|
| 305 |
return datasets.DatasetInfo(
|
| 306 |
citation=CANNABIS_TESTS_CITATION,
|
| 307 |
description=CANNABIS_TESTS_DESCRIPTION,
|
| 308 |
+
features=RAWGARDEN_DETAILS,
|
| 309 |
homepage=CANNABIS_TESTS_URL,
|
| 310 |
supervised_keys=None,
|
| 311 |
)
|
| 312 |
|
| 313 |
+
# def _generate_examples(self, filepath):
|
| 314 |
+
# """This function returns the examples in raw (text) form."""
|
| 315 |
+
# with open(filepath, encoding='utf-8') as f:
|
| 316 |
+
# for line in f:
|
| 317 |
+
# row = json.loads(line)
|
| 318 |
+
# product_name = row.get('product_name', '').strip()
|
| 319 |
+
# for i, result in enumerate(row['results']):
|
| 320 |
+
# _id = str(i)
|
| 321 |
+
# yield _id, {
|
| 322 |
+
# 'product_name': product_name,
|
| 323 |
+
# 'analyte': result.get('key', ''),
|
| 324 |
+
# 'value': result.get('value', 0),
|
| 325 |
+
# }
|
| 326 |
+
|
| 327 |
+
|
| 328 |
+
# if __name__ == '__main__':
|
| 329 |
+
|
| 330 |
+
# from datasets import load_dataset
|
| 331 |
+
# dataset = load_dataset('cannabis_tests.py')
|
dataset_infos.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{".": {"description": "", "citation": "", "homepage": "", "license": "", "features": {"sample_hash": {"dtype": "string", "id": null, "_type": "Value"}, "results_hash": {"dtype": "string", "id": null, "_type": "Value"}, "sample_id": {"dtype": "string", "id": null, "_type": "Value"}, "product_name": {"dtype": "string", "id": null, "_type": "Value"}, "producer": {"dtype": "string", "id": null, "_type": "Value"}, "product_type": {"dtype": "string", "id": null, "_type": "Value"}, "product_subtype": {"dtype": "string", "id": null, "_type": "Value"}, "date_tested": {"dtype": "string", "id": null, "_type": "Value"}, "analyses": {"dtype": "string", "id": null, "_type": "Value"}, "batch_number": {"dtype": "string", "id": null, "_type": "Value"}, "batch_size": {"dtype": "string", "id": null, "_type": "Value"}, "batch_units": {"dtype": "string", "id": null, "_type": "Value"}, "cannabinoids_method": {"dtype": "string", "id": null, "_type": "Value"}, "cannabinoids_status": {"dtype": "string", "id": null, "_type": "Value"}, "coa_algorithm": {"dtype": "string", "id": null, "_type": "Value"}, "coa_algorithm_entry_point": {"dtype": "string", "id": null, "_type": "Value"}, "coa_parsed_at": {"dtype": "string", "id": null, "_type": "Value"}, "coa_pdf": {"dtype": "string", "id": null, "_type": "Value"}, "coa_urls": {"dtype": "string", "id": null, "_type": "Value"}, "date_collected": {"dtype": "string", "id": null, "_type": "Value"}, "date_produced": {"dtype": "string", "id": null, "_type": "Value"}, "date_received": {"dtype": "string", "id": null, "_type": "Value"}, "date_retail": {"dtype": "string", "id": null, "_type": "Value"}, "delta_9_thc_per_unit": {"dtype": "string", "id": null, "_type": "Value"}, "distributor": {"dtype": "string", "id": null, "_type": "Value"}, "distributor_address": {"dtype": "string", "id": null, "_type": "Value"}, "distributor_city": {"dtype": "string", "id": null, "_type": "Value"}, "distributor_license_number": {"dtype": "string", "id": null, "_type": "Value"}, "distributor_state": {"dtype": "string", "id": null, "_type": "Value"}, "distributor_street": {"dtype": "string", "id": null, "_type": "Value"}, "distributor_zipcode": {"dtype": "float64", "id": null, "_type": "Value"}, "foreign_matter_method": {"dtype": "string", "id": null, "_type": "Value"}, "foreign_matter_status": {"dtype": "string", "id": null, "_type": "Value"}, "heavy_metals_method": {"dtype": "string", "id": null, "_type": "Value"}, "heavy_metals_status": {"dtype": "string", "id": null, "_type": "Value"}, "images": {"dtype": "string", "id": null, "_type": "Value"}, "lab": {"dtype": "string", "id": null, "_type": "Value"}, "lab_address": {"dtype": "string", "id": null, "_type": "Value"}, "lab_city": {"dtype": "string", "id": null, "_type": "Value"}, "lab_county": {"dtype": "string", "id": null, "_type": "Value"}, "lab_email": {"dtype": "string", "id": null, "_type": "Value"}, "lab_id": {"dtype": "string", "id": null, "_type": "Value"}, "lab_image_url": {"dtype": "string", "id": null, "_type": "Value"}, "lab_latitude": {"dtype": "float64", "id": null, "_type": "Value"}, "lab_license_number": {"dtype": "string", "id": null, "_type": "Value"}, "lab_longitude": {"dtype": "float64", "id": null, "_type": "Value"}, "lab_phone": {"dtype": "string", "id": null, "_type": "Value"}, "lab_results_url": {"dtype": "string", "id": null, "_type": "Value"}, "lab_state": {"dtype": "string", "id": null, "_type": "Value"}, "lab_street": {"dtype": "string", "id": null, "_type": "Value"}, "lab_website": {"dtype": "string", "id": null, "_type": "Value"}, "lab_zipcode": {"dtype": "int64", "id": null, "_type": "Value"}, "lims": {"dtype": "string", "id": null, "_type": "Value"}, "metrc_ids": {"dtype": "string", "id": null, "_type": "Value"}, "metrc_lab_id": {"dtype": "string", "id": null, "_type": "Value"}, "metrc_source_id": {"dtype": "string", "id": null, "_type": "Value"}, "microbes_method": {"dtype": "string", "id": null, "_type": "Value"}, "microbes_status": {"dtype": "string", "id": null, "_type": "Value"}, "moisture_content": {"dtype": "string", "id": null, "_type": "Value"}, "moisture_method": {"dtype": "string", "id": null, "_type": "Value"}, "mycotoxins_method": {"dtype": "string", "id": null, "_type": "Value"}, "mycotoxins_status": {"dtype": "string", "id": null, "_type": "Value"}, "notes": {"dtype": "string", "id": null, "_type": "Value"}, "pesticides_method": {"dtype": "string", "id": null, "_type": "Value"}, "pesticides_status": {"dtype": "string", "id": null, "_type": "Value"}, "producer_address": {"dtype": "string", "id": null, "_type": "Value"}, "producer_city": {"dtype": "string", "id": null, "_type": "Value"}, "producer_image_url": {"dtype": "string", "id": null, "_type": "Value"}, "producer_license_number": {"dtype": "string", "id": null, "_type": "Value"}, "producer_state": {"dtype": "string", "id": null, "_type": "Value"}, "producer_street": {"dtype": "string", "id": null, "_type": "Value"}, "producer_url": {"dtype": "string", "id": null, "_type": "Value"}, "producer_zipcode": {"dtype": "float64", "id": null, "_type": "Value"}, "product_size": {"dtype": "string", "id": null, "_type": "Value"}, "public": {"dtype": "float64", "id": null, "_type": "Value"}, "residual_solvents_method": {"dtype": "string", "id": null, "_type": "Value"}, "residual_solvents_status": {"dtype": "string", "id": null, "_type": "Value"}, "results": {"dtype": "string", "id": null, "_type": "Value"}, "sample_number": {"dtype": "float64", "id": null, "_type": "Value"}, "sample_size": {"dtype": "string", "id": null, "_type": "Value"}, "sampling_method": {"dtype": "string", "id": null, "_type": "Value"}, "serving_size": {"dtype": "string", "id": null, "_type": "Value"}, "status": {"dtype": "string", "id": null, "_type": "Value"}, "sum_of_cannabinoids": {"dtype": "float64", "id": null, "_type": "Value"}, "terpenes_method": {"dtype": "string", "id": null, "_type": "Value"}, "terpenes_status": {"dtype": "string", "id": null, "_type": "Value"}, "total_cannabinoids": {"dtype": "float64", "id": null, "_type": "Value"}, "total_cbc": {"dtype": "float64", "id": null, "_type": "Value"}, "total_cbd": {"dtype": "float64", "id": null, "_type": "Value"}, "total_cbdv": {"dtype": "float64", "id": null, "_type": "Value"}, "total_cbg": {"dtype": "float64", "id": null, "_type": "Value"}, "total_terpenes": {"dtype": "float64", "id": null, "_type": "Value"}, "total_terpenes_mg_g": {"dtype": "float64", "id": null, "_type": "Value"}, "total_thc": {"dtype": "float64", "id": null, "_type": "Value"}, "total_thcv": {"dtype": "float64", "id": null, "_type": "Value"}, "url": {"dtype": "string", "id": null, "_type": "Value"}, "water_activity_method": {"dtype": "string", "id": null, "_type": "Value"}, "water_activity_status": {"dtype": "string", "id": null, "_type": "Value"}}, "post_processed": null, "supervised_keys": null, "task_templates": null, "builder_name": "csv", "config_name": ".", "version": {"version_str": "0.0.0", "description": null, "major": 0, "minor": 0, "patch": 0}, "splits": {"train": {"name": "train", "num_bytes": 67727955, "num_examples": 2667, "dataset_name": "csv"}}, "download_checksums": {"C:\\Users\\keega\\Documents\\cannlytics\\datasets\\cannabis_tests\\train.csv": {"num_bytes": 78073452, "checksum": "7999de7a5ab1bc71b122aeaa3e25f16bc81846cdedb282b0196dd69e77829b2c"}}, "download_size": 78073452, "post_processing_size": null, "dataset_size": 67727955, "size_in_bytes": 145801407}}
|