Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use functions for saving flow solutions #566

Open
gnawin opened this issue Mar 26, 2024 · 2 comments
Open

Use functions for saving flow solutions #566

gnawin opened this issue Mar 26, 2024 · 2 comments
Labels
Type: improvement Better way of doing something Zone: export & visualisation How data leaves model & is visualised

Comments

@gnawin
Copy link
Member

gnawin commented Mar 26, 2024

What and Why

The current implementation for saving flow solutions (see code below) in the save_solution_to_file function uses a block for DataFrames.flatten and DataFrames.transform.

    output_file = joinpath(output_folder, "flows.csv")
    output_table =
        DataFrames.select(dataframes[:flows], :from, :to, :rp, :timesteps_block => :timestep)
    output_table.value = solution.flow
    output_table = DataFrames.flatten(
        DataFrames.transform(
            output_table,
            [:timestep, :value] =>
                DataFrames.ByRow(
                    (timesteps_block, value) -> begin # transform each row using these two columns
                        n = length(timesteps_block)
                        (timesteps_block, Iterators.repeated(value, n)) # e.g., (3:5, [30, 30, 30])
                    end,
                ) => [:timestep, :value],
        ),
        [:timestep, :value], # flatten, e.g., [(3, 30), (4, 30), (5, 30)]
    )
    output_table |> CSV.write(output_file)

After #564, there is a similar function defined for storage (see code below).

function _interpolate_storage_level!(df, time_column)
    DataFrames.flatten(
        DataFrames.transform(
            df,
            [time_column, :value, :processed_value] =>
                DataFrames.ByRow(
                    (period, value, start_value) -> begin
                        n = length(period)
                        interpolated_values = range(start_value; stop = value, length = n + 1)
                        (period, value, interpolated_values[2:end])
                    end,
                ) => [time_column, :value, :processed_value],
        ),
        [time_column, :processed_value],
    )
end

Since this function are added for the storage, the first block code can be simplified further by also using this function. For that purpose, the function may need to be modified a bit.

Possible Drawbacks

It will make the code cleaner, but that may not save many codes (since some conditions would need to be added to the functions and currently there is only this part that needs to be refactored).

It may lose or improve readability.

Related Issues

No response

@gnawin gnawin added Type: improvement Better way of doing something Zone: export & visualisation How data leaves model & is visualised labels Mar 26, 2024
@datejada
Copy link
Member

datejada commented Apr 8, 2024

Maybe obsolete if we use dataframes for outputs #115

@datejada
Copy link
Member

datejada commented Jul 2, 2024

Also see comments on #688

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: improvement Better way of doing something Zone: export & visualisation How data leaves model & is visualised
Projects
None yet
Development

No branches or pull requests

2 participants