|
| 1 | +""" |
| 2 | +Author: Scott Yun Ho |
| 3 | +Date: Oct. 29, 2024 |
| 4 | +Description: Functions that get specific information from OpenM++. |
| 5 | +""" |
| 6 | +import Functions.Get as Get |
| 7 | + |
| 8 | +import requests |
| 9 | +import pandas as pd |
| 10 | +import csv |
| 11 | + |
| 12 | +""" |
| 13 | +Description: Retrieve specific model run based on model_name and model_digest. Requires model_run_status from Create_model_run.py. |
| 14 | +Parameters: |
| 15 | +Return: |
| 16 | +""" |
| 17 | +def load_model_run(model_name, model_digest, model_run_status, oms_url, tables): |
| 18 | + |
| 19 | + # Create model run object. Run has now been completed, so create an object that gathers the finished information in one place. |
| 20 | + model_run = { |
| 21 | + 'model': model_name, |
| 22 | + 'digest': Get.get_model_runs(oms_url, model_digest).iloc[-1]['RunDigest'], |
| 23 | + 'status': model_run_status |
| 24 | + } |
| 25 | + |
| 26 | + # Retrieve specified output tables after model run. |
| 27 | + print(tables) |
| 28 | + for table in tables: |
| 29 | + print('Retrieving ' + table + ' table...') |
| 30 | + model_run[table] = load_output_table(model_digest=model_digest, model_run_digest=model_run['digest'], table=table, oms_url=oms_url) |
| 31 | + |
| 32 | + return model_run |
| 33 | + |
| 34 | +#To-do: finished logic here but test |
| 35 | +""" |
| 36 | +Description: |
| 37 | +Parameters: |
| 38 | +Return: |
| 39 | +""" |
| 40 | +def load_model(model_name): |
| 41 | + x = requests.get(oms_url + '/api/model-list').json() |
| 42 | + model_list = pd.DataFrame([item['Model'] for item in x]) |
| 43 | + return model_list.loc[model_list['ModelName'] == model_name] |
| 44 | + |
| 45 | +#To-do: get multiple specific runs. Take array as input and output info for multiple model runs. |
| 46 | +""" |
| 47 | +Description: |
| 48 | +Parameters: |
| 49 | +Return: |
| 50 | +""" |
| 51 | +def load_model_runs(): |
| 52 | + return 1 |
| 53 | + |
| 54 | +""" |
| 55 | +Description: |
| 56 | +Parameters: |
| 57 | +Return: |
| 58 | +""" |
| 59 | +def load_output_table(model_digest, model_run_digest, table, oms_url): |
| 60 | + # Get list from CSV format (data is neater). URL returns CSV file attachment in a response stream, which is why stream is necessary below. Informed by: https://stackoverflow.com/questions/35371043/use-python-requests-to-download-csv#:~:text=CSV_URL%20%3D%20%27http%3A//samplecsvs.s3.amazonaws.com/Sacramentorealestatetransactions.csv%27-,with%20requests,-.Session()%20as%20s%3A%0A%20%20%20%20download%20%3D%20s.get(CSV_URL)%0A%0A%20%20%20%20decoded_content |
| 61 | + print("Getting response stream output and processing into a dataframe...") |
| 62 | + raw_csv_data = [] |
| 63 | + with requests.Session() as stream: |
| 64 | + download = stream.get(oms_url + '/api/model/' + model_digest + '/run/' + model_run_digest + '/table/' + table + '/expr/csv') |
| 65 | + decoded_content = download.content.decode('utf-8') |
| 66 | + cr = csv.reader(decoded_content.splitlines(), delimiter=',') |
| 67 | + raw_csv_data = list(cr) |
| 68 | + |
| 69 | + # Convert list of lists table output (raw_csv_data) to pandas dataframe. |
| 70 | + headers = raw_csv_data.pop(0) |
| 71 | + long_format_df = pd.DataFrame(raw_csv_data, columns=headers) |
| 72 | + |
| 73 | + # Clean data to remove negative and positive infinity. regex=True needed to recognize the unicode for infinity: \u221e |
| 74 | + #print("Cleaning infinity symbols from data...") |
| 75 | + #long_format_df = long_format_df.replace("-"+"\u221e", "MIN", regex=True) |
| 76 | + #long_format_df = long_format_df.replace("\u221e", "MAX", regex=True) |
| 77 | + |
| 78 | + # Previous format that returned everything in dims col. |
| 79 | + """ |
| 80 | + #response = pd.DataFrame(requests.get(oms_url + '/api/model/' + model_digest + '/run/' + model_run_digest + '/table/' + table + '/expr').json()) |
| 81 | + #print(pd.DataFrame(requests.get(oms_url + '/api/model/' + model_digest + '/run/' + model_run_digest + '/table/' + table + '/expr/csv'))) |
| 82 | + #response = requests.get(oms_url + '/api/model/' + model_digest + '/run/' + model_run_digest + '/table/' + table + '/expr/csv') |
| 83 | + #print(response.headers) |
| 84 | + #output_df = pd.DataFrame(response.content) |
| 85 | + #print(response) |
| 86 | + #output_df = response |
| 87 | +
|
| 88 | + #output_df["Dims"] = output_df["Dims"].astype(str) # Program cannot pivot using a list as index, so Dims col must be converted to string first. |
| 89 | + #output_df = output_df.pivot(index="Dims", columns="ExprId", values="Value") |
| 90 | + #print(output_df) |
| 91 | + """ |
| 92 | + |
| 93 | + # Copy headers to use as columns during pivot, but not including expr_name and expr_value since we're pivoting on these values. |
| 94 | + wide_format_indices = headers |
| 95 | + wide_format_indices.remove("expr_name") |
| 96 | + wide_format_indices.remove("expr_value") |
| 97 | + |
| 98 | + # Pivot dataframe to be wide format. |
| 99 | + print("Converting data to wide format...\n") |
| 100 | + wide_format_df = long_format_df |
| 101 | + wide_format_df = pd.pivot(wide_format_df, |
| 102 | + index=wide_format_indices, |
| 103 | + columns="expr_name", |
| 104 | + values="expr_value" |
| 105 | + ) |
| 106 | + |
| 107 | + return wide_format_df |
| 108 | + |
| 109 | +""" |
| 110 | +Description: |
| 111 | +Parameters: |
| 112 | +Return: |
| 113 | +""" |
| 114 | +def load_scenario(): |
| 115 | + return 1 |
| 116 | + |
| 117 | + |
| 118 | +def load_model_digest(df, model_name) : |
| 119 | + return df['Digest'][df.loc[df['Name'] == model_name].index.values[0]] |
0 commit comments