Skip to content

Commit

Permalink
spliting reading into two reading tables and building ensemble from t…
Browse files Browse the repository at this point in the history
…ables (#165)
  • Loading branch information
eacharles authored Apr 11, 2023
1 parent 41fc50b commit f95e378
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/qp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .scipy_pdfs import *
from .packed_interp_pdf import *
from .ensemble import Ensemble
from .factory import instance, add_class, create, read, convert, concatenate, iterator, data_length
from .factory import instance, add_class, create, read, convert, concatenate, iterator, data_length, from_tables

from . import utils

Expand Down
48 changes: 31 additions & 17 deletions src/qp/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,36 @@ def create(self, class_name, data, method=None):
ctor_func = the_class.creation_method(method)
return Ensemble(ctor_func, data)

def from_tables(self, tables):
"""Build this ensemble from a tables
Parameters
----------
tables: `dict`
Notes
-----
This will use information in the meta data table to figure out how to construct the data
need to build the ensemble.
"""
md_table = tables['meta']
data_table = tables['data']
ancil_table = tables.get('ancil')

data = self._build_data_dict(md_table, data_table)

pdf_name = data.pop('pdf_name')
pdf_version = data.pop('pdf_version')
if pdf_name not in self: #pragma: no cover
raise KeyError("Class nameed %s is not in factory" % pdf_name)

the_class = self[pdf_name]
reader_convert = the_class.reader_method(pdf_version)
ctor_func = the_class.creation_method(None)
if reader_convert is not None: #pragma: no cover
data = reader_convert(data)
return Ensemble(ctor_func, data=data, ancil=ancil_table)

def read(self, filename):
"""Read this ensemble from a file
Expand All @@ -146,23 +175,7 @@ def read(self, filename):
tables = io.read(filename, NUMPY_DICT, keys=keys,
allow_missing_keys=allow_missing_keys) #pylint: disable=no-member

md_table = tables['meta']
data_table = tables['data']
ancil_table = tables.get('ancil')

data = self._build_data_dict(md_table, data_table)

pdf_name = data.pop('pdf_name')
pdf_version = data.pop('pdf_version')
if pdf_name not in self: #pragma: no cover
raise KeyError("Class nameed %s is not in factory" % pdf_name)

the_class = self[pdf_name]
reader_convert = the_class.reader_method(pdf_version)
ctor_func = the_class.creation_method(None)
if reader_convert is not None: #pragma: no cover
data = reader_convert(data)
return Ensemble(ctor_func, data=data, ancil=ancil_table)
return self.from_tables(tables)

def data_length(self, filename):
"""Get the size of data
Expand Down Expand Up @@ -322,3 +335,4 @@ def instance():
convert = _FACTORY.convert
concatenate = _FACTORY.concatenate
data_length = _FACTORY.data_length
from_tables = _FACTORY.from_tables

0 comments on commit f95e378

Please sign in to comment.