|
""" |
|
Curate CCRS Lab Results |
|
Copyright (c) 2023-2024 Cannlytics |
|
|
|
Authors: |
|
Keegan Skeate <https://github.com/keeganskeate> |
|
Candace O'Sullivan-Sutherland <https://github.com/candy-o> |
|
Created: 1/1/2023 |
|
Updated: 6/1/2024 |
|
License: CC-BY 4.0 <https://huggingface.co/datasets/cannlytics/cannabis_tests/blob/main/LICENSE> |
|
|
|
Original author: Cannabis Data |
|
Original license: MIT <https://github.com/cannabisdata/cannabisdata/blob/main/LICENSE> |
|
|
|
Data Sources: |
|
|
|
- [Public records request](https://portal.lcb.wa.gov/s/public-record-request-form) |
|
|
|
""" |
|
|
|
from datetime import datetime |
|
import gc |
|
import os |
|
from typing import Optional |
|
|
|
|
|
from cannlytics.data.ccrs import ( |
|
CCRS, |
|
CCRS_ANALYTES, |
|
CCRS_ANALYSES, |
|
CCRS_DATASETS, |
|
anonymize, |
|
get_datafiles, |
|
find_detections, |
|
format_test_value, |
|
save_dataset, |
|
unzip_datafiles, |
|
) |
|
from cannlytics.utils import convert_to_numeric, camel_to_snake |
|
import pandas as pd |
|
|
|
|
|
def read_lab_results( |
|
data_dir: str, |
|
value_key: Optional[str] = 'TestValue', |
|
) -> pd.DataFrame: |
|
"""Read CCRS lab results.""" |
|
lab_results = pd.DataFrame() |
|
lab_result_files = get_datafiles(data_dir, 'LabResult_') |
|
fields = CCRS_DATASETS['lab_results']['fields'] |
|
parse_dates = CCRS_DATASETS['lab_results']['date_fields'] |
|
usecols = list(fields.keys()) + parse_dates |
|
dtype = {k: v for k, v in fields.items() if v != 'datetime64'} |
|
dtype[value_key] = 'string' |
|
for datafile in lab_result_files: |
|
data = pd.read_csv( |
|
datafile, |
|
sep='\t', |
|
encoding='utf-16', |
|
engine='python', |
|
parse_dates=parse_dates, |
|
dtype=dtype, |
|
usecols=usecols, |
|
on_bad_lines='skip', |
|
|
|
|
|
) |
|
lab_results = pd.concat([lab_results, data]) |
|
if 'TestValue' in lab_results.columns: |
|
lab_results[value_key] = lab_results[value_key].apply(convert_to_numeric) |
|
|
|
return lab_results |
|
|
|
|
|
def format_result( |
|
item_results, |
|
manager: Optional[CCRS] = None, |
|
drop: Optional[list] = [] |
|
) -> dict: |
|
"""Format results for a lab sample.""" |
|
|
|
|
|
if item_results.empty: |
|
return None |
|
|
|
|
|
item = item_results.iloc[0].to_dict() |
|
[item.pop(key) for key in drop] |
|
entry = { |
|
**item, |
|
'delta_9_thc': format_test_value(item_results, 'delta_9_thc'), |
|
'thca': format_test_value(item_results, 'thca'), |
|
'total_thc': format_test_value(item_results, 'total_thc'), |
|
'cbd': format_test_value(item_results, 'cbd'), |
|
'cbda': format_test_value(item_results, 'cbda'), |
|
'total_cbd': format_test_value(item_results, 'total_cbd'), |
|
'moisture_content': format_test_value(item_results, 'moisture_content'), |
|
'water_activity': format_test_value(item_results, 'water_activity'), |
|
} |
|
|
|
|
|
statuses = list(item_results['LabTestStatus'].unique()) |
|
if 'Fail' in statuses: |
|
entry['status'] = 'Fail' |
|
else: |
|
entry['status'] = 'Pass' |
|
|
|
|
|
entry_results = [] |
|
for _, item_result in item_results.iterrows(): |
|
test_name = item_result['TestName'] |
|
analyte = CCRS_ANALYTES[test_name] |
|
try: |
|
analysis = CCRS_ANALYSES[analyte['type']] |
|
except KeyError: |
|
if manager is not None: |
|
manager.create_log('Unidentified analysis: ' + str(analyte['type'])) |
|
else: |
|
print('Unidentified analysis:', analyte['type']) |
|
analysis = analyte['type'] |
|
entry_results.append({ |
|
'analysis': analysis, |
|
'key': analyte['key'], |
|
'name': item_result['TestName'], |
|
'units': analyte['units'], |
|
'value': item_result['TestValue'], |
|
}) |
|
entry['results'] = entry_results |
|
|
|
|
|
entry['pesticides'] = find_detections(entry_results, 'pesticides') |
|
entry['residual_solvents'] = find_detections(entry_results, 'residual_solvents') |
|
entry['heavy_metals'] = find_detections(entry_results, 'heavy_metals') |
|
|
|
|
|
return entry |
|
|
|
|
|
def augment_lab_results( |
|
manager: CCRS, |
|
results: pd.DataFrame, |
|
item_key: Optional[str] = 'InventoryId', |
|
analysis_name: Optional[str] = 'TestName', |
|
analysis_key: Optional[str] = 'TestValue', |
|
verbose: Optional[str] = True, |
|
) -> pd.DataFrame: |
|
"""Format CCRS lab results to merge into another dataset.""" |
|
|
|
|
|
results[analysis_name] = results[analysis_name].apply( |
|
lambda x: x.replace('Pesticides - ', '').replace(' (ppm) (ppm)', '') |
|
) |
|
|
|
|
|
|
|
test_names = list(results[analysis_name].unique()) |
|
known_analytes = list(CCRS_ANALYTES.keys()) |
|
missing = list(set(test_names) - set(known_analytes)) |
|
try: |
|
assert len(missing) == 0 |
|
del test_names, known_analytes, missing |
|
gc.collect() |
|
except: |
|
manager.create_log('Unidentified analytes: ' + ', '.join(missing)) |
|
raise ValueError(f'Unidentified analytes. Add missing analytes to `CCRS_ANALYTES`: {", ".join(missing)}') |
|
|
|
|
|
analyte_data = results[analysis_name].map(CCRS_ANALYTES).values.tolist() |
|
results = results.join(pd.DataFrame(analyte_data)) |
|
results['type'] = results['type'].map(CCRS_ANALYSES) |
|
results[item_key] = results[item_key].astype(str) |
|
|
|
|
|
item_ids = list(results[item_key].unique()) |
|
drop = [analysis_name, analysis_key, 'LabTestStatus', 'key', 'type', 'units'] |
|
N = len(item_ids) |
|
if verbose: |
|
manager.create_log(f'Curating {N} items...') |
|
manager.create_log('Estimated runtime: ' + str(round(N * 0.00011, 2)) + ' minutes') |
|
|
|
|
|
group = results.groupby(item_key).apply(format_result, drop=drop, manager=manager).dropna() |
|
return pd.DataFrame(group.tolist()) |
|
|
|
|
|
def curate_ccrs_lab_results( |
|
manager: CCRS, |
|
data_dir: str, |
|
stats_dir: str |
|
) -> pd.DataFrame: |
|
"""Curate CCRS lab results.""" |
|
|
|
|
|
manager.create_log('Curating lab results...') |
|
start = datetime.now() |
|
|
|
|
|
unzip_datafiles(data_dir) |
|
|
|
|
|
lab_results = read_lab_results(data_dir) |
|
|
|
|
|
lab_results = augment_lab_results(manager, lab_results) |
|
|
|
|
|
|
|
columns = { |
|
'ExternalIdentifier': 'lab_id', |
|
'inventory_type': 'product_type', |
|
'test_date': 'date_tested', |
|
} |
|
lab_results.rename(columns=columns, inplace=True) |
|
|
|
|
|
|
|
lab_results = anonymize(lab_results) |
|
|
|
|
|
lab_results.rename(columns=lambda x: camel_to_snake(x), inplace=True) |
|
|
|
|
|
|
|
timestamp = lab_results['created_date'].max().strftime('%Y-%m-%d') |
|
lab_results_dir = os.path.join(stats_dir, 'lab_results') |
|
outfile = save_dataset(lab_results, lab_results_dir, f'wa-lab-results-{timestamp}') |
|
manager.create_log('Saved lab results: ' + str(outfile)) |
|
|
|
|
|
end = datetime.now() |
|
manager.create_log('✓ Finished curating lab results in ' + str(end - start)) |
|
return lab_results |
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
|
item_key = 'InventoryId' |
|
analysis_name = 'TestName' |
|
analysis_key = 'TestValue' |
|
value_key = 'TestValue' |
|
verbose = True |
|
drop = [] |
|
|
|
|
|
base = 'D://data/washington/' |
|
stats_dir = 'D://data/washington/stats' |
|
manager = CCRS() |
|
|
|
|
|
releases = [ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'CCRS PRR (7-2-24)', |
|
] |
|
for release in releases: |
|
data_dir = os.path.join(base, release, release) |
|
try: |
|
lab_results = curate_ccrs_lab_results(manager, data_dir, stats_dir) |
|
manager.create_log('Curated %i WA lab results.' % len(lab_results)) |
|
except: |
|
manager.create_log('Failed to curate WA lab results:' + data_dir) |
|
|
|
|
|
all_results = [] |
|
datafiles = os.listdir(os.path.join(stats_dir, 'lab_results')) |
|
datafiles = [os.path.join(stats_dir, 'lab_results', x) for x in datafiles if \ |
|
not x.startswith('~') and \ |
|
not 'aggregate' in x and \ |
|
not 'latest' in x and \ |
|
not 'inventory' in x] |
|
for datafile in datafiles: |
|
data = pd.read_excel(datafile) |
|
all_results.append(data) |
|
results = pd.concat(all_results) |
|
results.drop_duplicates(subset=['lab_result_id', 'updated_date'], inplace=True) |
|
results.sort_values(by=['created_date'], inplace=True) |
|
print('Total number of results:', len(results)) |
|
outfile = os.path.join(stats_dir, 'lab_results', 'wa-lab-results-aggregate.xlsx') |
|
results.to_excel(outfile, index=False) |
|
manager.create_log('Saved aggregate lab results to: ' + outfile) |
|
|