|
| 1 | +export cluster!, dummy_cluster!, transform_wide_to_long! |
| 2 | + |
| 3 | +""" |
| 4 | + cluster!( |
| 5 | + connection, |
| 6 | + period_duration, |
| 7 | + num_rps; |
| 8 | + input_profile_table_name = "input_profiles", |
| 9 | + kwargs..., |
| 10 | + ) |
| 11 | +
|
| 12 | +Convenience function to cluster the table named in `input_profile_table_name` |
| 13 | +using `period_duration` and `num_rps`. The resulting tables |
| 14 | +`cluster_profiles_rep_periods`, `cluster_rep_periods_mapping`, and |
| 15 | +`cluster_rep_periods_data` are loaded into `connection`, enriched with `year` information. |
| 16 | +
|
| 17 | +This function extract the table, then calls [`split_into_periods!`](@ref), |
| 18 | +[`find_representative_periods`](@ref), [`fit_rep_period_weights!`](@ref), and |
| 19 | +finally `write_clustering_result_to_tables`. |
| 20 | +""" |
| 21 | +function cluster!( |
| 22 | + connection, |
| 23 | + period_duration, |
| 24 | + num_rps; |
| 25 | + input_profile_table_name = "input_profiles", |
| 26 | + drop_incomplete_last_period::Bool = false, |
| 27 | + method::Symbol = :k_means, |
| 28 | + distance::SemiMetric = SqEuclidean(), |
| 29 | + weight_type::Symbol = :convex, |
| 30 | + tol::Float64 = 1e-2, |
| 31 | +) |
| 32 | + df = DuckDB.query( |
| 33 | + connection, |
| 34 | + "SELECT * FROM $input_profile_table_name |
| 35 | + ", |
| 36 | + ) |> DataFrame |
| 37 | + split_into_periods!(df; period_duration) |
| 38 | + clusters = |
| 39 | + find_representative_periods(df, num_rps; drop_incomplete_last_period, method, distance) |
| 40 | + fit_rep_period_weights!(clusters; weight_type, tol) |
| 41 | + |
| 42 | + write_clustering_result_to_tables(connection, clusters) |
| 43 | + |
| 44 | + # enrich the cluster_ data with year information because TulipaClustering |
| 45 | + # currently does not support multi-year |
| 46 | + years = [ |
| 47 | + row.year for |
| 48 | + row in DuckDB.query(connection, "SELECT DISTINCT year FROM $input_profile_table_name") |
| 49 | + ] |
| 50 | + years_str = join(years, ", ") |
| 51 | + for table_name in ("cluster_rep_periods_data", "cluster_rep_periods_mapping") |
| 52 | + DuckDB.query( |
| 53 | + connection, |
| 54 | + "CREATE OR REPLACE TEMP TABLE t_new_$table_name AS |
| 55 | + SELECT unnest([$years_str]) AS year, $table_name.* |
| 56 | + FROM $table_name", |
| 57 | + ) |
| 58 | + # DROP TABLE OR VIEW |
| 59 | + is_table = |
| 60 | + only([ |
| 61 | + row.count for row in DuckDB.query( |
| 62 | + connection, |
| 63 | + "SELECT COUNT(*) AS count FROM duckdb_tables WHERE table_name='$table_name'", |
| 64 | + ) |
| 65 | + ]) > 0 |
| 66 | + if is_table |
| 67 | + DuckDB.query(connection, "DROP TABLE $table_name") |
| 68 | + else |
| 69 | + DuckDB.query(connection, "DROP VIEW $table_name") |
| 70 | + end |
| 71 | + DuckDB.query( |
| 72 | + connection, |
| 73 | + "ALTER TABLE t_new_$table_name |
| 74 | + RENAME TO $table_name", |
| 75 | + ) |
| 76 | + end |
| 77 | + |
| 78 | + return clusters |
| 79 | +end |
| 80 | + |
| 81 | +function dummy_cluster!(conncetion) end |
| 82 | + |
| 83 | +""" |
| 84 | + transform_wide_to_long!( |
| 85 | + connection, |
| 86 | + wide_table_name, |
| 87 | + long_table_name; |
| 88 | + ) |
| 89 | +
|
| 90 | +Convenience function to convert a table in wide format to long format using DuckDB. |
| 91 | +Originally aimed at converting a profile table like the following: |
| 92 | +
|
| 93 | +| year | timestep | name1 | name2 | ⋯ | name2 | |
| 94 | +| ---- | -------- | ----- | ----- | -- | ----- | |
| 95 | +| 2030 | 1 | 1.0 | 2.5 | ⋯ | 0.0 | |
| 96 | +| 2030 | 2 | 1.5 | 2.6 | ⋯ | 0.0 | |
| 97 | +| 2030 | 3 | 2.0 | 2.6 | ⋯ | 0.0 | |
| 98 | +
|
| 99 | +To a table like the following: |
| 100 | +
|
| 101 | +| year | timestep | profile_name | value | |
| 102 | +| ---- | -------- | ------------ | ----- | |
| 103 | +| 2030 | 1 | name1 | 1.0 | |
| 104 | +| 2030 | 2 | name1 | 1.5 | |
| 105 | +| 2030 | 3 | name1 | 2.0 | |
| 106 | +| 2030 | 1 | name2 | 2.5 | |
| 107 | +| 2030 | 2 | name2 | 2.6 | |
| 108 | +| 2030 | 3 | name2 | 2.6 | |
| 109 | +| ⋮ | ⋮ | ⋮ | ⋮ | |
| 110 | +| 2030 | 1 | name3 | 0.0 | |
| 111 | +| 2030 | 2 | name3 | 0.0 | |
| 112 | +| 2030 | 3 | name3 | 0.0 | |
| 113 | +
|
| 114 | +This conversion is done using the `UNPIVOT` SQL command from DuckDB. |
| 115 | +
|
| 116 | +## Keyword arguments |
| 117 | +
|
| 118 | +- `exclude_columns = ["year", "timestep"]`: Which tables to exclude from the conversion |
| 119 | +- `name_column = "profile_name"`: Name of the new column that contains the names of the old columns |
| 120 | +- `value_column = "value"`: Name of the new column that holds the values from the old columns |
| 121 | +""" |
| 122 | +function transform_wide_to_long!( |
| 123 | + connection, |
| 124 | + wide_table_name, |
| 125 | + long_table_name; |
| 126 | + exclude_columns = ["year", "timestep"], |
| 127 | + name_column = "profile_name", |
| 128 | + value_column = "value", |
| 129 | +) |
| 130 | + @assert length(exclude_columns) > 0 |
| 131 | + exclude_str = join(exclude_columns, ", ") |
| 132 | + DuckDB.query( |
| 133 | + connection, |
| 134 | + "CREATE TABLE $long_table_name AS |
| 135 | + UNPIVOT $wide_table_name |
| 136 | + ON COLUMNS(* EXCLUDE ($exclude_str)) |
| 137 | + INTO |
| 138 | + NAME $name_column |
| 139 | + VALUE $value_column |
| 140 | + ORDER BY $name_column, $exclude_str |
| 141 | + ", |
| 142 | + ) |
| 143 | + |
| 144 | + return |
| 145 | +end |
0 commit comments