Coverage for webgeodyn/models/models.py: 0%

36 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-12-18 09:33 +0000

1import os.path 

2from itertools import cycle 

3from .model import Model 

4 

5 

6class Models(dict): 

7 """ 

8 Dictionnary containing the loaded models 

9 """ 

10 def __init__(self): 

11 super().__init__() 

12 # ui_colors are taken so that consecutive colors are visually very different (10-class Paired on http://colorbrewer2.org) 

13 self.ui_colors = cycle(['#1f78b4', '#33a02c', '#e31a1c', '#ff7f00', '#6a3d9a', '#a6cee3', '#b2df8a', '#fb9a99', '#fdbf6f', '#cab2d6']) 

14 self.model_colors = {"archeomag": "#ffaaaa", 

15 "ced": "#ff0000", 

16 "chaos": "#4daf4a", 

17 "covobs": "#984ea3", 

18 "enscore": "#af694a", 

19 "pygeodyn_asc": "#ff00ff", 

20 "pygeodyn_hdf5": "#ff00ff", 

21 "midpath": "#00ff00", 

22 "nath": "#0000ff", 

23 "s1fs": "#aaaaaa", 

24 "s1fs2": "#aaaaaa", 

25 "zforecast": "#e41a1c"} 

26 

27 def addModel(self, modelName, model, color=None): 

28 """ 

29 Add a model to the list of loaded model. 

30 

31 :param modelName: name of the model 

32 :type modelName: str 

33 :param model: Model to add 

34 :type model: Model 

35 :param color: color to use for plots of the model (default: None, in this case, uses a dedicated color for each model) 

36 :type color: str (hexadecimal form) 

37 """ 

38 if color is None: 

39 try: 

40 color = self.model_colors[model.dataFormat] 

41 except KeyError: 

42 # Use ui_colors if this fails 

43 color = 'ui' 

44 

45 if color == 'ui': 

46 color = next(self.ui_colors) 

47 

48 # Checking hexadecimal form of the color 

49 if len(color) != 7 or not color.startswith('#'): 

50 raise ValueError('Color of {} should be given in hexadecimal form ! Got "{}" instead.'.format(modelName, color)) 

51 

52 model.color = color 

53 self[modelName] = model 

54 

55 def loadModel(self, dataDirectory, modelName=None, dataFormat='default', color=None, keepRealisations=True, state_type='analysed', export=True): 

56 """ 

57 Creates a Model by loading the files in dataDirectory using the dataFormat and the calls addModel. Note that it sets first the allocated space in the dict to False in case of asynchronous thread checking the loading state. 

58 

59 :param dataDirectory: Directory where the model files are located 

60 :type dataDirectory: str (path) 

61 :param modelName: name of the model to use. If None, the basename of the dataDirectory will be used. 

62 :type modelName: str or None 

63 :param dataFormat: format to use to load the model data. Default is 'default'. 

64 :type dataFormat: str 

65 :param color: color of the model to use for plots (in hex form) 

66 :type color: str 

67 :param keepRealisations: indicates with realisations should be kept or averaged 

68 :type keepRealisations: bool 

69 :param export: whether the model should be available for download (export = True) or not (export = False) 

70 :type expot: bool 

71 """ 

72 if modelName is None: 

73 modelName = os.path.basename(dataDirectory) 

74 

75 self[modelName] = False 

76 print('adding model', modelName) 

77 self.addModel(modelName, Model(dataDirectory, dataFormat, keepRealisations=keepRealisations, state_type=state_type, export=export), color) 

78 

79 def isModelLoading(self, modelName): 

80 """ 

81 Checks if the model is present in Models and if it is being loaded. 

82 

83 :param modelName: name of the model to check 

84 :type modelName: str 

85 :return: the state of loading of the model (True means loading) 

86 :rtype: bool 

87 """ 

88 return (modelName in self) and (self[modelName] is False) 

89 

90 def getModelList(self): 

91 """ 

92 Gets the list of model names. 

93 

94 :return: list of model names 

95 :rtype: list 

96 """ 

97 return list(self.keys()) 

98 

99 def isCached(self, modelName): 

100 """ 

101 Checks if a model is present and loaded. 

102 

103 :param modelName: name of the model to check 

104 :type modelName: str 

105 :return: the presence of the model in Models (True means present and loaded) 

106 :rtype: bool 

107 """ 

108 if modelName not in self: 

109 return False 

110 if self[modelName] is False: 

111 return False 

112 return True