@@ -10,7 +10,7 @@ def _check_tilde_start(x):
1010 return bool (isinstance (x , str ) and x .startswith ("~" ))
1111
1212
13- def _var_names (var_names , data , filter_vars = None ):
13+ def _var_names (var_names , data , filter_vars = None , check_if_present = True ):
1414 """Handle var_names input across arviz.
1515
1616 Parameters
@@ -22,6 +22,9 @@ def _var_names(var_names, data, filter_vars=None):
2222 interpret var_names as substrings of the real variables names. If "regex",
2323 interpret var_names as regular expressions on the real variables names. A la
2424 `pandas.filter`.
25+ check_if_present : bool, optional
26+ If True (default), raise an error if any of the var_names is not present in
27+ the data. If False, ignore missing var_names.
2528
2629 Returns
2730 -------
@@ -52,14 +55,20 @@ def _var_names(var_names, data, filter_vars=None):
5255 )
5356
5457 try :
55- var_names = _subset_list (var_names , all_vars , filter_items = filter_vars , warn = False )
58+ var_names = _subset_list (
59+ var_names ,
60+ all_vars ,
61+ filter_items = filter_vars ,
62+ warn = False ,
63+ check_if_present = check_if_present ,
64+ )
5665 except KeyError as err :
5766 msg = " " .join (("var names:" , f"{ err } " , "in dataset" ))
5867 raise KeyError (msg ) from err
5968 return var_names
6069
6170
62- def _subset_list (subset , whole_list , filter_items = None , warn = True ):
71+ def _subset_list (subset , whole_list , filter_items = None , warn = True , check_if_present = True ):
6372 """Handle list subsetting (var_names, groups...) across arviz.
6473
6574 Parameters
@@ -125,7 +134,7 @@ def _subset_list(subset, whole_list, filter_items=None, warn=True):
125134 subset = [item for item in whole_list for name in subset if re .search (name , item )]
126135
127136 existing_items = np .isin (subset , whole_list )
128- if not np .all (existing_items ):
137+ if check_if_present and not np .all (existing_items ):
129138 raise KeyError (f"{ np .array (subset )[~ existing_items ]} are not present" )
130139
131140 return subset
0 commit comments