1515from __future__ import annotations
1616
1717from collections .abc import Mapping
18- from datetime import datetime , timedelta
19- from typing import TYPE_CHECKING , Any
20- from typing import SupportsFloat as Numeric
21- from typing import overload
18+ from typing import TYPE_CHECKING , Any , overload
2219
2320import pandas as pd
2421from layered_config_tree import LayeredConfigTree
2522
2623from vivarium .framework .event import Event
2724from vivarium .framework .lifecycle import lifecycle_states
28- from vivarium .framework .lookup .table import (
29- DEFAULT_VALUE_COLUMN ,
30- CategoricalTable ,
31- InterpolatedTable ,
32- LookupTable ,
33- ScalarTable ,
34- )
25+ from vivarium .framework .lookup .table import DEFAULT_VALUE_COLUMN , LookupTable
3526from vivarium .manager import Manager
3627from vivarium .types import LookupTableData
3728
@@ -62,14 +53,14 @@ def __init__(self) -> None:
6253 super ().__init__ ()
6354 self .tables : dict [str , LookupTable [pd .Series [Any ]] | LookupTable [pd .DataFrame ]] = {}
6455
65- def setup (self , builder : " Builder" ) -> None :
56+ def setup (self , builder : Builder ) -> None :
6657 self ._logger = builder .logging .get_logger (self .name )
6758 self ._configuration = builder .configuration
68- self ._pop_view_builder = builder .population .get_view
59+ self ._get_view = builder .population .get_view
6960 self .clock = builder .time .clock ()
70- self ._interpolation_order = builder .configuration .interpolation .order
71- self ._extrapolate = builder .configuration .interpolation .extrapolate
72- self ._validate = builder .configuration .interpolation .validate
61+ self .interpolation_order = builder .configuration .interpolation .order
62+ self .extrapolate = builder .configuration .interpolation .extrapolate
63+ self .validate_interpolation = builder .configuration .interpolation .validate
7364 self ._add_resources = builder .resources .add_resources
7465 self ._add_constraint = builder .lifecycle .add_constraint
7566 self ._get_current_component = builder .components .get_current_component
@@ -125,7 +116,7 @@ def build_table(
125116 table = self ._build_table (component , data , name , value_columns )
126117 self ._add_resources (component , table , table .required_resources )
127118 self ._add_constraint (
128- table .call ,
119+ table ._call ,
129120 restrict_during = [
130121 lifecycle_states .INITIALIZATION ,
131122 lifecycle_states .SETUP ,
@@ -150,107 +141,19 @@ def _build_table(
150141 data = pd .DataFrame (data )
151142
152143 value_columns_ = value_columns if value_columns else DEFAULT_VALUE_COLUMN
153- validate_build_table_parameters (data , value_columns_ )
154144
155- table : LookupTable [pd .Series [Any ]] | LookupTable [pd .DataFrame ]
156- if isinstance (data , pd .DataFrame ):
157- parameter_columns , key_columns = self ._get_columns (value_columns_ , data )
158- if parameter_columns :
159- table = InterpolatedTable (
160- name = name ,
161- component = component ,
162- data = data ,
163- population_view_builder = self ._pop_view_builder ,
164- key_columns = key_columns ,
165- parameter_columns = parameter_columns ,
166- value_columns = value_columns_ ,
167- interpolation_order = self ._interpolation_order ,
168- clock = self .clock ,
169- extrapolate = self ._extrapolate ,
170- validate = self ._validate ,
171- )
172- else :
173- table = CategoricalTable (
174- name = name ,
175- component = component ,
176- data = data ,
177- population_view_builder = self ._pop_view_builder ,
178- key_columns = key_columns ,
179- value_columns = value_columns_ ,
180- )
181- else :
182- table = ScalarTable (
183- name = name , component = component , data = data , value_columns = value_columns_
184- )
145+ table = LookupTable (
146+ name = name ,
147+ component = component ,
148+ data = data ,
149+ value_columns = value_columns_ ,
150+ manager = self ,
151+ population_view = self ._get_view (),
152+ )
185153
186154 self .tables [table .name ] = table
187155
188156 return table
189157
190158 def __repr__ (self ) -> str :
191159 return "LookupTableManager()"
192-
193- @staticmethod
194- def _get_columns (
195- value_columns : list [str ] | tuple [str , ...] | str , data : pd .DataFrame
196- ) -> tuple [list [str ], list [str ]]:
197- if isinstance (value_columns , str ):
198- value_columns = [value_columns ]
199-
200- all_columns = list (data .columns )
201-
202- potential_parameter_columns = [
203- str (col ).removesuffix ("_start" )
204- for col in all_columns
205- if str (col ).endswith ("_start" )
206- ]
207- parameter_columns = []
208- bin_edge_columns = []
209- for column in potential_parameter_columns :
210- if f"{ column } _end" in all_columns :
211- parameter_columns .append (column )
212- bin_edge_columns += [f"{ column } _start" , f"{ column } _end" ]
213-
214- key_columns = [
215- col
216- for col in all_columns
217- if col not in value_columns and col not in bin_edge_columns
218- ]
219-
220- return parameter_columns , key_columns
221-
222-
223- def validate_build_table_parameters (
224- data : LookupTableData ,
225- value_columns : list [str ] | tuple [str , ...] | str ,
226- ) -> None :
227- """Makes sure the data format agrees with the provided column layout."""
228- if (
229- data is None
230- or (isinstance (data , pd .DataFrame ) and data .empty )
231- or (isinstance (data , (list , tuple )) and not data )
232- ):
233- raise ValueError ("Must supply some data" )
234-
235- acceptable_types = (Numeric , datetime , timedelta , list , tuple , pd .DataFrame )
236- if not isinstance (data , acceptable_types ):
237- raise TypeError (
238- f"The only allowable types for data are { acceptable_types } . "
239- f"You passed { type (data )} ."
240- )
241-
242- if isinstance (data , (list , tuple )):
243- if isinstance (value_columns , str ):
244- raise ValueError (
245- "When supplying multiple values, value_columns must be a list or tuple of strings."
246- )
247- if len (value_columns ) != len (data ):
248- raise ValueError (
249- "The number of value columns must match the number of values."
250- f"You supplied values: { data } and value_columns: { value_columns } "
251- )
252- elif not isinstance (data , pd .DataFrame ):
253- if not isinstance (value_columns , str ):
254- raise ValueError (
255- "When supplying a single value, value_columns must be a string if provided."
256- )
0 commit comments