PowerSystems.jl icon indicating copy to clipboard operation
PowerSystems.jl copied to clipboard

Selecting more than 1 column of PTDF matrix

Open AbrahamAlvarezB opened this issue 4 years ago • 3 comments

Error when selecting specific columns of the PTDF matrix I would expect that the following commands work:

julia> a = rand(4,5)
4×5 Array{Float64,2}:
 0.0246739  0.483415  0.0234004  0.378329  0.965849
 0.665427   0.575655  0.843898   0.457372  0.492012
 0.441002   0.719667  0.619103   0.252873  0.80892
 0.647832   0.472036  0.784917   0.577866  0.25087

julia> b = a[:,[2,4]]
4×2 Array{Float64,2}:
 0.483415  0.378329
 0.575655  0.457372
 0.719667  0.252873
 0.472036  0.577866

However for a system sys the PTDF matrix

julia> PTDF_matrix = PTDF(sys)
PowerNetworkMatrix
    Dimension 1, ["Line1", "Line2", "Line3", "Line4", "Line5"]
    Dimension 2, [1, 2, 3, 4]
And data, a (5, 4):
 0.0  -0.625  -0.375  -0.5
 0.0  -0.375  -0.625  -0.5
 0.0   0.25   -0.25    1.38778e-17
 0.0   0.125  -0.125  -0.5
 0.0  -0.125   0.125  -0.5

julia> b = PTDF_matrix[:,[2,4]]
ERROR: KeyError: key [2, 4] not found
Stacktrace:
 [1] getindex at ./dict.jl:467 [inlined]
 [2] lookup_index at /Users/abrahamalvarezb/.julia/packages/PowerSystems/r86iN/src/utils/network_calculations/common.jl:38 [inlined]
 [3] _to_index_tuple at /Users/abrahamalvarezb/.julia/packages/PowerSystems/r86iN/src/utils/network_calculations/common.jl:52 [inlined] (repeats 2 times)
 [4] to_index at /Users/abrahamalvarezb/.julia/packages/PowerSystems/r86iN/src/utils/network_calculations/common.jl:71 [inlined]
 [5] getindex(::PTDF{Tuple{Array{String,1},Array{Int64,1}},Tuple{Dict{String,Int64},Dict{Int64,Int64}}}, ::Function, ::Array{Int64,1}) at /Users/abrahamalvarezb/.julia/packages/PowerSystems/r86iN/src/utils/network_calculations/common.jl:89
 [6] top-level scope at REPL[107]:1

Is this an issue or can it be improved?

AbrahamAlvarezB avatar Mar 16 '21 12:03 AbrahamAlvarezB

You can access the matrix using PTDF_matrix.data, see https://github.com/NREL-SIIP/PowerSystems.jl/blob/master/src/utils/network_calculations/ptdf_calculations.jl#L6

raphaelsaavedra avatar Mar 16 '21 12:03 raphaelsaavedra

I agree that if we use the struct call as below we get the data,

julia> b = PTDF_matrix.data[:,[2,4]]
5×2 Array{Float64,2}:
 -0.625  -0.5
 -0.375  -0.5
  0.25    1.38778e-17
  0.125  -0.5
 -0.125  -0.5

but we loose the parent format. I would expect something like this, which may be useful:

b = PTDF_matrix[:,[2,4]]
PowerNetworkMatrix
   Dimension 1, ["Line1", "Line2", "Line3", "Line4", "Line5"]
   Dimension 2, [2, 4]
And data, a (5, 2):
-0.625    -0.5
-0.375    -0.5
 0.25      1.38778e-17
 0.125    -0.5
-0.125    -0.5

AbrahamAlvarezB avatar Mar 16 '21 12:03 AbrahamAlvarezB

@AbrahamAlvarezB we don't have a great way to do slicing at the moment. Since we implemented the code to optimize a single value or single column fetching we never implemented additional methods to do the slicing.

jd-lara avatar Mar 18 '21 16:03 jd-lara