def run(self): ''' Make a key-value map of certain attributes in the Open FDA dataset ''' print('Acquiring Records') for record in self.source.acquire_labels(): if 'openfda' in record and 'product_ndc' in record['openfda']: for entry in record['openfda']['product_ndc']: ndc = ProductNdc.parse(entry) id = ndc.format() for op in self.features: op['feature'].accumulate(id, record) print('Writing Features') for op in self.features: feature = op['feature'] baseName = '-'.join(feature.fields) fileName = io.relativeToAbsolute('../../data/'+baseName+'.txt') with open(fileName, 'w', encoding='utf-8') as f: print('product_ndc', op['column'], sep='\t', file=f) for pair in sorted(feature.data, key=itemgetter(0, 1)): value = pair[1] for fn in op['transform']: value = fn(value) print(pair[0],value,sep='\t',file=f)
def run(self): ''' Make a key-value map of certain attributes in the Open FDA dataset ''' print('Acquiring Records') for record in self.source.acquire_labels(): if 'openfda' in record and 'product_ndc' in record['openfda']: for entry in record['openfda']['product_ndc']: ndc = ProductNdc.parse(entry) id = ndc.format() for op in self.features: op['feature'].accumulate(id, record) print('Writing Features') for op in self.features: feature = op['feature'] baseName = '-'.join(feature.fields) fileName = io.relativeToAbsolute('../../data/' + baseName + '.txt') with open(fileName, 'w', encoding='utf-8') as f: print('product_ndc', op['column'], sep='\t', file=f) for pair in sorted(feature.data, key=itemgetter(0, 1)): value = pair[1] for fn in op['transform']: value = fn(value) print(pair[0], value, sep='\t', file=f)
def map_fda(self, x): ''' Project an element from the FDA product list ''' ndc = ProductNdc.parse(x['PRODUCTNDC']) return {'product_ndc': ndc.format(), 'proprietary_name': self.title(x['PROPRIETARYNAME']), 'nonproprietary_name': self.title(x['NONPROPRIETARYNAME']), 'dea_schedule': x['DEASCHEDULE'] }
def map_fda(self, x): ''' Project an element from the FDA product list ''' ndc = ProductNdc.parse(x['PRODUCTNDC']) return { 'product_ndc': ndc.format(), 'proprietary_name': self.title(x['PROPRIETARYNAME']), 'nonproprietary_name': self.title(x['NONPROPRIETARYNAME']), 'dea_schedule': x['DEASCHEDULE'] }
def test_fda(self): self.suite[4]['formatted'] = '99999-009' self.suite[9]['formatted'] = '99999-099' self.suite[14]['formatted'] = '99999-999' for s in self.suite: actual = ProductNdc.parse(s['formatted']) self.assertEqual(s['l'], actual.labeler) self.assertEqual(s['prc'], actual.productCode) self.assertEqual(s['formatted'], actual.format_fda())
def _map(self, label): ''' A generator function that produces multiple elements from each OpenFDA record ''' if ('openfda' in label and 'product_ndc' in label['openfda'] and 'brand_name' in label['openfda']): asString = json.dumps(label) for x in label['openfda']['brand_name']: brand = self.sourceNames.title(x) for y in label['openfda']['product_ndc']: ndc = ProductNdc.parse(y).format() yield {'proprietary_name': brand, 'ndc':ndc, 'size':len(asString)}
def _map(self, x): ''' Extend the manufacturer record to include the labeler value from the product NDC and the partition key to use ''' ndc = ProductNdc.parse(x['product_ndc']) partition = x['name'] for fn in [self._stripThe, self._fixEDotFO, self._defaultPartition]: partition = fn(partition) return {'name': x['name'], 'labeler': ndc.labeler, 'product_ndc': x['product_ndc'], 'partition' : partition }
def _map(self, x): ''' Extend the manufacturer record to include the labeler value from the product NDC and the partition key to use ''' ndc = ProductNdc.parse(x['product_ndc']) partition = x['name'] for fn in [self._stripThe, self._fixEDotFO, self._defaultPartition]: partition = fn(partition) return { 'name': x['name'], 'labeler': ndc.labeler, 'product_ndc': x['product_ndc'], 'partition': partition }
def test_nutricel(self): actual = ProductNdc.parse('53157-AS3') self.assertEqual(53157, actual.labeler) self.assertEqual(120, actual.productCode) self.assertEqual('53157-0120', actual.format())
def test_canon(self): for s in self.suite: actual = ProductNdc.parse(s['formatted']) self.assertEqual(s['l'], actual.labeler) self.assertEqual(s['prc'], actual.productCode) self.assertEqual(s['formatted'], actual.format())