Skip to content

Instantly share code, notes, and snippets.

"""
run_training(loss_change,
learn,
x_in,
y_in)
Run training epochs until the Δloss ≤ loss_change.
"""
function run_training(loss_change,
learn,
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
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
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)
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
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"])...)
function get_request(endpoint_name::String)
url = URL
headers = ["Content-Type" => "application/json"]
response = HTTP.request(
"GET",
join([URL, endpoint_name]),
headers,
verbose = 0,
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
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,
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(?, ?, ?)")