This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
run_training(loss_change, | |
learn, | |
x_in, | |
y_in) | |
Run training epochs until the Δloss ≤ loss_change. | |
""" | |
function run_training(loss_change, | |
learn, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function update_model!(learn, | |
flux_model, | |
x_in, | |
y_in) | |
dLdm, _, _ = gradient(get_loss, flux_model, x_in, y_in) | |
@. flux_model.weight = flux_model.weight - Float32(learn * dLdm.weight) | |
@. flux_model.bias = flux_model.bias - Float32(learn * dLdm.bias) | |
return flux_model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function get_loss(flux_model, x_in, y_in) | |
y_model = flux_model(x_in) | |
mse_error = Flux.mse(y_model, y_in) | |
return mse_error | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function plot_daily_gas_usage(df_all_gas::DataFrame, | |
month::String, | |
year::Int64) | |
df_gas = process_gas_df(df_all_gas) | |
df_gas = filter(row -> occursin(month, Dates.monthname(row.Date)) && | |
Dates.year(row.Date) == year, | |
df_gas) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function process_gas_df(df_all_gas::DataFrame) | |
df_gas = deepcopy(df_all_gas) | |
# Temperature is 10x in original data | |
df_gas[!, :T] = df_gas[!, :T] ./ 10 | |
# Convert from kWh to m^3 | |
conversion_factor = 0.10058 | |
df_gas[!, :ch] = df_gas[!, :ch] .* conversion_factor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function get_hist_gas_usage() | |
gas_pointer_dict = get_request(GAS_POINTER) | |
page_end = (gas_pointer_dict["value"] / 32) |> ceil |> Int | |
all_gas = DataFrame[] | |
for page = 1:page_end | |
gas_usage_dict = get_request(gas_usage_ep(page)) | |
df_gas = vcat(DataFrame.(gas_usage_dict["value"])...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function get_request(endpoint_name::String) | |
url = URL | |
headers = ["Content-Type" => "application/json"] | |
response = HTTP.request( | |
"GET", | |
join([URL, endpoint_name]), | |
headers, | |
verbose = 0, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function delete_rows(db_name::String, table_name::String, price::Float64) | |
conn_db = connect_db(db_name) | |
try | |
DBInterface.execute(conn_db, | |
"DELETE FROM $table_name WHERE Price > $price") | |
catch | |
error("Unable to delete from $table_name") | |
finally |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function remove_duplicate_fruits(db_name::String, table_name::String) | |
conn_db = connect_db(db_name) | |
# Remove duplicate rows using a common table expression (cte) | |
try | |
DBInterface.execute(conn_db, | |
"WITH cte AS ( | |
SELECT | |
Name, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add_to_fruit_table(conn_master, | |
db_name::String, | |
table_name::String, | |
df_input::DataFrame) | |
conn = create_fruit_table(conn_master, db_name, table_name) | |
stmt = try | |
DBInterface.prepare(conn, | |
"INSERT INTO $(table_name) VALUES(?, ?, ?)") |