Coverage for /home/runner/work/pycax/pycax/pycax/hli/hli.py: 84%

45 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-26 00:27 +0000

1""" 

2Query for a HLI base or XPort table 

3 

4/ca?table_id=<id> API endpoint with id equal to one of the HLI or HLI_XPort table ids 

5""" 

6import pandas as pd 

7 

8from pycax.caxutils import get_data 

9 

10from pycax import tables 

11 

12# Set up the variables 

13 

14HLI_SHORT = {"NOSA", "SAR", "PNI", "JuvOut", "PreSmolt", "RperS"} 

15HLI_TABLENAMES = { 

16 'base': { 

17 'NOSA': 'NOSA', 'SAR': 'SAR', 'PNI': 'PNI', 

18 'JuvOut': 'JuvenileOutmigrants', 'PreSmolt': 'PresmoltAbundance', 

19 'RperS': 'RperS' 

20 }, 

21 'xport': {} 

22 } 

23for key in HLI_TABLENAMES['base']: 

24 HLI_TABLENAMES['xport'][key] = 'XPortCA_'+HLI_TABLENAMES['base'][key]+'_01' 

25 

26import json 

27import pkgutil 

28 

29#data = pkgutil.get_data(__name__, "data/xport_colnames_json.txt") 

30data = get_data("xport_colnames_json.txt") 

31HLI_XPORT_COLNAMES = json.loads(data) 

32 

33 

34def return_tablename(hli, tabletype): 

35 """ 

36 Retrieve a table name for a hli short code for base or xport table type 

37 

38 :param hli: [String] A HLI short code: NOSA, SAR, PNI, JuvOut, PreSmolt, RperS 

39 :param tabletype: [String] "base" or "xport" 

40 :return: A table name as a string 

41 

42 Usage:: 

43 

44 from pycax import hli 

45 hli.tablename('NOSA', 'base') 

46 """ 

47 valid = HLI_SHORT 

48 if hli not in valid: 

49 raise ValueError("hli must be one of %r." % valid) 

50 valid = ["base", "xport"] 

51 if tabletype not in valid: 

52 raise ValueError("tabletype must be one of %r." % valid) 

53 

54 return HLI_TABLENAMES[tabletype][hli] 

55 

56def get(hli, tabletype="xport", qargs={}, fargs={}, **kwargs): 

57 """ 

58 Get a table using the table name from /ca/tables API: pycax.datasets.getdf() 

59 

60 :param hli: [String] A HLI short code: NOSA, SAR, PNI, JuvOut, PreSmolt, RperS 

61 :param tabletype: [String] 'base' or 'xport'; default is xport 

62 :param qargs: [dict] a dictionary of query parameters. The default is no parameters. See usage for common query parameters 

63 :param fargs: [dict] a dictionary of filter values as {colname1: value}, {colname1: [value1, value2]} or {colname1: value, colname2: value}. The default is no filter. See usage for examples. 

64 

65 :return: A dictionary of class HliResponse ready for execution 

66 

67 Usage:: 

68 

69 from pycax import hli 

70 query = hli.get('NOSA', 'base', qargs={'limit': 1}) 

71 query.execute() 

72 query.data # get the data 

73 query.api_url # get the API url 

74 

75 """ 

76 if not isinstance(qargs, dict): 

77 raise TypeError('qargs must be a dictionary; got %s' % type(qargs).__name__) 

78 

79 if not isinstance(fargs, dict): 

80 raise TypeError('fargs must be a dictionary; got %s' % type(fargs).__name__) 

81 

82 tablename = return_tablename(hli, tabletype) 

83 if len(fargs)>1: args['filter'] = tables.dict_to_json(fargs) 

84 

85 return tables.TablesResponse(tablename, qargs) 

86 

87def getdf(hli, tabletype="xport", qargs={}, fargs={}, **kwargs): 

88 """ 

89 Get a HLI table and return a data frame sorted in the same order as the HLI Tabular Query 

90 

91 This uses the json file for the column names in in the data directory. 

92 

93 :param hli: [String] A HLI short code: NOSA, SAR, PNI, JuvOut, PreSmolt, RperS 

94 :param tabletype: [String] 'base' or 'xport'; default is xport 

95 :param qargs: [dict] a dictionary of query parameters. The default is no parameters. See usage for common query parameters 

96 :param fargs: [dict] a dictionary of filter values as {colname1: value}, {colname1: [value1, value2]} or {colname1: value, colname2: value}. The default is no filter. See usage for examples. 

97 

98 :return: A dictionary of class HliResponse ready for execution 

99 

100 Usage:: 

101 

102 from pycax import hli 

103 res = hli.getdf('NOSA', 'xport', fargs={'popid': 7}) 

104 res.head() 

105 

106 """ 

107 if not isinstance(tabletype, str): 

108 raise TypeError('tabletype must be a string with value base or xport; got %s' % type(tabletype).__name__) 

109 if not tabletype in ('base', 'xport'): 

110 raise ValueError('tabletype must be a string with value base or xport') 

111 if not isinstance(hli, str): 

112 raise TypeError('hli must be a string with value base or xport; got %s' % type(tabletype).__name__) 

113 if not hli in ('NOSA', 'SAR', 'PNI', 'JuvOut', 'PreSmolt', 'RperS'): 

114 raise ValueError('hli must be a string with value NOSA, SAR, PNI, JuvOut, PreSmolt, or RperS') 

115 if not isinstance(qargs, dict): 

116 raise TypeError('qargs must be a dictionary; got %s' % type(qargs).__name__) 

117 if not isinstance(fargs, dict): 

118 raise TypeError('fargs must be a dictionary; got %s' % type(fargs).__name__) 

119 

120 tablename = return_tablename(hli, tabletype) 

121 df = tables.getdf(tablename, qargs=qargs, fargs=fargs, **kwargs) 

122 col_order = HLI_XPORT_COLNAMES[hli].extend(["species", "agency", "hli", "publish"]) 

123 df = df.reindex(columns=col_order) 

124 

125 return df 

126