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
« 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
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
8from pycax.caxutils import get_data
10from pycax import tables
12# Set up the variables
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'
26import json
27import pkgutil
29#data = pkgutil.get_data(__name__, "data/xport_colnames_json.txt")
30data = get_data("xport_colnames_json.txt")
31HLI_XPORT_COLNAMES = json.loads(data)
34def return_tablename(hli, tabletype):
35 """
36 Retrieve a table name for a hli short code for base or xport table type
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
42 Usage::
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)
54 return HLI_TABLENAMES[tabletype][hli]
56def get(hli, tabletype="xport", qargs={}, fargs={}, **kwargs):
57 """
58 Get a table using the table name from /ca/tables API: pycax.datasets.getdf()
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.
65 :return: A dictionary of class HliResponse ready for execution
67 Usage::
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
75 """
76 if not isinstance(qargs, dict):
77 raise TypeError('qargs must be a dictionary; got %s' % type(qargs).__name__)
79 if not isinstance(fargs, dict):
80 raise TypeError('fargs must be a dictionary; got %s' % type(fargs).__name__)
82 tablename = return_tablename(hli, tabletype)
83 if len(fargs)>1: args['filter'] = tables.dict_to_json(fargs)
85 return tables.TablesResponse(tablename, qargs)
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
91 This uses the json file for the column names in in the data directory.
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.
98 :return: A dictionary of class HliResponse ready for execution
100 Usage::
102 from pycax import hli
103 res = hli.getdf('NOSA', 'xport', fargs={'popid': 7})
104 res.head()
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__)
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)
125 return df