66@author: prmiles
77"""
88import numpy as np
9- from scipy .interpolate import interp1d
109from scipy import pi ,sin ,cos
1110import sys
1211import math
1312
1413def check_settings (default_settings , user_settings = None ):
15-
16- settings = default_settings .copy ()
17-
18- options = list (default_settings .keys ())
19- if user_settings is None :
20- user_settings = {}
21- user_options = list (user_settings .keys ())
22- for ii in range (len (user_options )):
23- if user_options [ii ] in options :
24- # check if checking a dictionary
25- if isinstance (settings [user_options [ii ]], dict ):
26- settings [user_options [ii ]] = check_settings (settings [user_options [ii ]], user_settings [user_options [ii ]])
27- else :
28- settings [user_options [ii ]] = user_settings [user_options [ii ]]
29- if user_options [ii ] not in options :
30- settings [user_options [ii ]] = user_settings [user_options [ii ]]
31-
32- return settings
14+ '''
15+ Check user settings with default.
16+
17+ Recursively checks elements of user settings against the defaults and updates settings
18+ as it goes. If a user setting does not exist in the default, then the user setting
19+ is added to the settings. If the setting is defined in both the user and default
20+ settings, then the user setting overrides the default. Otherwise, the default
21+ settings persist.
3322
34- def generate_plotly_subplot_coords (nparam , ns1 , ns2 ):
35- sprow = []
36- spcol = []
37- counter = 0
38- for ii in range (ns1 ):
39- for jj in range (ns2 ):
40- sprow .append (ii + 1 )
41- spcol .append (jj + 1 )
42- counter += 1
43- if counter is nparam :
44- break
45- return sprow , spcol
23+ Args:
24+ * **default_settings** (:py:class:`dict`): Default settings for particular method.
25+ * **user_settings** (:py:class:`dict`): User defined settings.
26+
27+ Returns:
28+ * (:py:class:`dict`): Updated settings.
29+ '''
30+ settings = default_settings .copy () # initially define settings as default
31+
32+ options = list (default_settings .keys ()) # get default settings
33+ if user_settings is None : # convert to empty dict
34+ user_settings = {}
35+ user_options = list (user_settings .keys ()) # get user settings
36+ for uo in user_options : # iterate through settings
37+ if uo in options :
38+ # check if checking a dictionary
39+ if isinstance (settings [uo ], dict ):
40+ settings [uo ] = check_settings (settings [uo ], user_settings [uo ])
41+ else :
42+ settings [uo ] = user_settings [uo ]
43+ if uo not in options :
44+ settings [uo ] = user_settings [uo ]
45+
46+ return settings
4647
4748def generate_subplot_grid (nparam = 2 ):
4849 '''
@@ -85,30 +86,6 @@ def generate_names(nparam, names):
8586 names = extend_names_to_match_nparam (names , nparam )
8687 return names
8788
88- def setup_plot_features (nparam , names , figsizeinches ):
89- '''
90- Setup plot features.
91-
92- Args:
93- * **nparam** (:py:class:`int`): Number of parameters
94- * **names** (:py:class:`list`): Names of parameters provided by user
95- * **figsizeinches** (:py:class:`list`): [Width, Height]
96-
97- Returns:
98- * **ns1** (:py:class:`int`): Number of rows in subplot
99- * **ns2** (:py:class:`int`): Number of columns in subplot
100- * **names** (:py:class:`list`): List of strings - parameter names
101- * **figsizeiches** (:py:class:`list`): [Width, Height]
102- '''
103- ns1 , ns2 = generate_subplot_grid (nparam = nparam )
104-
105- names = generate_names (nparam = nparam , names = names )
106-
107- if figsizeinches is None :
108- figsizeinches = [5 ,4 ]
109-
110- return ns1 , ns2 , names , figsizeinches
111-
11289def generate_default_names (nparam ):
11390 '''
11491 Generate generic parameter name set.
@@ -348,86 +325,4 @@ def append_to_nrow_ncol_based_on_shape(sh, nrow, ncol):
348325 else :
349326 nrow .append (sh [0 ])
350327 ncol .append (sh [1 ])
351- return nrow , ncol
352-
353- # --------------------------------------------
354- def convert_flag_to_boolean (flag ):
355- '''
356- Convert flag to boolean for backwards compatibility.
357-
358- Args:
359- * **flag** (:py:class:`bool` or :py:class:`int`): Flag to specify something.
360-
361- Returns:
362- * **flag** (:py:class:`bool`): Flag to converted to boolean.
363- '''
364- if flag is 'on' :
365- flag = True
366- elif flag is 'off' :
367- flag = False
368-
369- return flag
370-
371- # --------------------------------------------
372- def set_local_parameters (ii , local ):
373- '''
374- Set local parameters based on tests.
375-
376- :Test 1:
377- * `local == 0`
378- :Test 2:
379- * `local == ii`
380-
381- Args:
382- * **ii** (:py:class:`int`): Index.
383- * **local** (:class:`~numpy.ndarray`): Local flags.
384-
385- Returns:
386- * **test** (:class:`~numpy.ndarray`): Array of Booleans indicated test results.
387- '''
388- # some parameters may only apply to certain batch sets
389- test1 = local == 0
390- test2 = local == ii
391- test = test1 + test2
392- return test .reshape (test .size ,)
393-
394- # --------------------------------------------
395- def empirical_quantiles (x , p = np .array ([0.25 , 0.5 , 0.75 ])):
396- '''
397- Calculate empirical quantiles.
398-
399- Args:
400- * **x** (:class:`~numpy.ndarray`): Observations from which to generate quantile.
401- * **p** (:class:`~numpy.ndarray`): Quantile limits.
402-
403- Returns:
404- * (:class:`~numpy.ndarray`): Interpolated quantiles.
405- '''
406-
407- # extract number of rows/cols from np.array
408- n = x .shape [0 ]
409- # define vector valued interpolation function
410- xpoints = range (n )
411- interpfun = interp1d (xpoints , np .sort (x , 0 ), axis = 0 )
412-
413- # evaluation points
414- itpoints = (n - 1 )* p
415-
416- return interpfun (itpoints )
417-
418- # --------------------------------------------
419- def check_defaults (kwargs , defaults ):
420- '''
421- Check if defaults are defined in kwargs
422-
423- Args:
424- * **kwargs** (:py:class:`dict`): Keyword arguments.
425- * **defaults** (:py:class:`dict`): Default settings.
426-
427- Returns:
428- * **kwargs** (:py:class:`dict`): Updated keyword arguments with at least defaults set.
429- '''
430- for ii in defaults :
431- if ii not in kwargs :
432- kwargs [ii ] = defaults [ii ]
433- return kwargs
328+ return nrow , ncol
0 commit comments