working on a longblock of code, the sum total of which is here. My issue comes in with this particular block:
# path_skaters is a string that gives the path to the csv file with the skaters information (see example file for suggested format)
path_skaters = "https://raw.githubusercontent.com/dscotthunter/Fantasy-Hockey-IP-Code/master/example_skaters.csv"
# path_goalies is a string that gives the path to the csv file with the goalies information (see example file for suggested format)
path_goalies = "https://raw.githubusercontent.com/dscotthunter/Fantasy-Hockey-IP-Code/673b0a5119ed746a8dc2347206d138c84407def1/example_goalies.csv"
goalies = DataFrame(CSV.File(HTTP.get(path_goalies).body, normalizenames=true, delim=","))
skaters = DataFrame(CSV.File(HTTP.get(path_skaters).body, normalizenames=true, delim=","))
the_lineup= formulation(skaters, goalies, hcat(zeros(Int, num_skaters + num_goalies), zeros(Int, num_skaters + num_goalies)), num_overlap, num_skaters, num_goalies, centers, wingers, defenders, num_teams, skaters_teams, goalie_opponents, team_lines, num_lines, P1_info)
the_lineup2 = formulation(skaters, goalies, hcat(the_lineup, zeros(Int, num_skaters + num_goalies)), num_overlap, num_skaters, num_goalies, centers, wingers, defenders, num_teams, skaters_teams, goalie_opponents, team_lines, num_lines, P1_info)
tracer = hcat(the_lineup, the_lineup2)
for i=1:(num_lineups-2)
try
thelineup=formulation(skaters, goalies, tracer, num_overlap, num_skaters, num_goalies, centers, wingers, defenders, num_teams, skaters_teams, goalie_opponents, team_lines, num_lines, P1_info)
tracer = hcat(tracer,thelineup)
catch
break
end
end
is producing this error:
DimensionMismatch: mismatch in dimension 1 (expected 1 got 190)
Stacktrace:
[1] _cs
# ./abstractarray.jl:1717 [inlined]
[2] _cshp
# ./abstractarray.jl:1713 [inlined]
[3] _cat_size_shape
# ./abstractarray.jl:1693 [inlined]
[4] cat_size_shape(dims::Tuple{Bool, Bool}, X::Nothing, tail::Vector{Int64})
# Base ./abstractarray.jl:1691
[5] _cat_t(::Val{2}, ::Type{Union{Nothing, Int64}}, ::Nothing, ::Vararg{Any})
# Base ./abstractarray.jl:1732
[6] _cat(::Val{2}, ::Nothing, ::Vararg{Any})
# Base ./abstractarray.jl:1728
[7] #cat#155
# ./abstractarray.jl:1916 [inlined]
[8] hcat
# ./abstractarray.jl:1878 [inlined]
[9] create_lineups(num_lineups::Int64, num_overlap::Int64, path_skaters::String, path_goalies::String, formulation::typeof(one_lineup_Type_4), path_to_output::String)
# Main ./In[20]:805
[10] top-level scope
# In[20]:874
[11] eval
# ./boot.jl:368 [inlined]
[12] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
# Base ./loading.jl:1428
I'm reasonably sure this is because the players data frame and goalies = 190, but I can't figure out why that's a problem.
The error is saying that you cannot call tracer = hcat(tracer,thelineup):
julia> x = zeros(190);
julia> hcat(0, x)
ERROR: DimensionMismatch("mismatch in dimension 1 (expected 1 got 190)")
Stacktrace:
[...]
What are you trying to achieve? If you want to concatenate two vectors, you might want vcat instead. Or perhaps `pushfirst!.
help?> vcat
search: vcat hvcat VecOrMat DenseVecOrMat StridedVecOrMat AbstractVecOrMat VectorConstraint
vcat(A...)
Concatenate along dimension 1.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> a = [1 2 3 4 5]
1×5 Matrix{Int64}:
1 2 3 4 5
julia> b = [6 7 8 9 10; 11 12 13 14 15]
2×5 Matrix{Int64}:
6 7 8 9 10
11 12 13 14 15
julia> vcat(a,b)
3×5 Matrix{Int64}:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
julia> c = ([1 2 3], [4 5 6])
([1 2 3], [4 5 6])
julia> vcat(c...)
2×3 Matrix{Int64}:
1 2 3
4 5 6
help?> pushfirst!
search: pushfirst!
pushfirst!(collection, items...) -> collection
Insert one or more items at the beginning of collection.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> pushfirst!([1, 2, 3, 4], 5, 6)
6-element Vector{Int64}:
5
6
1
2
3
4
Related
Simple question : in R, what's the best way to detect if there is a zero somewhere in a time series (ts class)? I run X13 (seasonal package) on hundreds of time series and I would like to identify those who contain zero values (since multiplicative models don't work when they encounter a zero). If I could detect those series, I could use a IF-THEN-ELSE statement with proper specs for the X13.
Thank you!
You can replace or delete them:
ts <- ts(0:10)
## Deleting
ts[ts != 0]
#> [1] 1 2 3 4 5 6 7 8 9 10
## Replacing
replace(ts, ts==0, 1)
#> Time Series:
#> Start = 1
#> End = 11
#> Frequency = 1
#> [1] 1 1 2 3 4 5 6 7 8 9 10
## Detecting
any(ts == 0)
#> [1] TRUE
Created on 2020-10-29 by the reprex package (v0.3.0)
I have a .tsv file, similar to the example given bellow.
index GLWSKIKEVGKEAAKA GLFDIIKKIAESI GLLDIVKKVVGAFGSL GLFDIVKKVVGALGSL
GLWSKIKEVGKEAAKA 33 7 10 11 9 9 11 10
GLFDIIKKIAESI 7 13 8 9 8 9 11 11
GLLDIVKKVVGAFGSL 10 8 16 14 13 11 11 10
GLFDIVKKVVGALGSL 11 9 14 16 14 12 12 11
file read perfectly:
m <- read.table("out_1.csv", sep=",",header=TRUE, row.names='index')
trying to build a network graph using the library given bellow.
library(igraph)
ig <- graph.adjacency(m, mode="undirected", weighted=TRUE)
raised an error
Error in mde(x): 'list' object cannot be coerced to type 'double'
Traceback:
1. graph.adjacency(m, mode = "undirected", weighted = TRUE)
2. graph.adjacency.dense(adjmatrix, mode = mode, weighted = weighted,
. diag = diag)
3. `mode<-`(`*tmp*`, value = "double")
how to solve this problem
The help pages say you need a square adjacency matrix.
You have a data.frame with 8 columns and 4 rows in your example.
(I assume you have empty columns in your header that do not show up in your pasted text.)
This would work:
m <- as.matrix(read.table("out_1.csv", sep=",", header=TRUE, row.names='index')[, 1:4])
library(igraph)
ig <- graph.adjacency(m, mode="undirected", weighted=TRUE)
ig
#> IGRAPH 80e1470 UNW- 4 10 --
#> + attr: name (v/c), weight (e/n)
#> + edges from 80e1470 (vertex names):
#> [1] GLWSKIKEVGKEAAKA--GLWSKIKEVGKEAAKA GLWSKIKEVGKEAAKA--GLFDIIKKIAESI
#> [3] GLWSKIKEVGKEAAKA--GLLDIVKKVVGAFGSL GLWSKIKEVGKEAAKA--GLFDIVKKVVGALGSL
#> [5] GLFDIIKKIAESI --GLFDIIKKIAESI GLFDIIKKIAESI --GLLDIVKKVVGAFGSL
#> [7] GLFDIIKKIAESI --GLFDIVKKVVGALGSL GLLDIVKKVVGAFGSL--GLLDIVKKVVGAFGSL
#> [9] GLLDIVKKVVGAFGSL--GLFDIVKKVVGALGSL GLFDIVKKVVGALGSL--GLFDIVKKVVGALGSL
I am using the igraph package and I am uncertain whether it is a bug or not, but the $father output makes no sense sometimes. Specifically, when I rename the vertex attributes.
h<-make_tree(10)
#with normal vertex attributes
graph.bfs(h, root="1", neimode='out', order=TRUE, father=TRUE,unreachable=FALSE) #father output seems correct
plot(h,layout=layout_as_tree)
#with renamed vertex attributes
set.seed(1)
h<-set.vertex.attribute(h, "name", value=sample(1:10,10))
plot(h,layout=layout_as_tree)
graph.bfs(h, root="3", neimode='out', order=TRUE, father=TRUE,unreachable=FALSE) #father output seems wrong
I obtain the output as below
#with normal vertex attributes
$order
+ 10/10 vertices, from ff55a96:
[1] 1 2 3 4 5 6 7 8 9 10
$rank
NULL
$father
+ 10/10 vertices, from ff55a96:
[1] NA 1 1 2 2 3 3 4 4 5
#with renamed vertex attributes
$order
+ 10/10 vertices, named, from 170f7a0:
[1] 3 4 5 7 2 8 9 6 10 1
$rank
NULL
$father
+ 10/10 vertices, named, from 170f7a0:
[1] 3 4 5 7 2 8 9 6 10 1
I do not understand why the father for the renamed vertex attributes case is wrong. For example, the first element should be NA but its not.
Can someone explain what is happening? If so how do I fix this such that my father elements reflects something similar to the first case.
It's a bit strange, but for some reason, the bfs function has a straight assignment of the vertex names to the names of the father vector. See the 54-55 line of code in the source code:
if (father)
names(res$father) <- V(graph)$name
Clearly, this simply overwrites the names of res$father with the vector of names in the graph. Notice that this conditional statement requires the argument igraph_opt("add.vertex.names") to be true.
So we can avoid this behavior by setting the global option for adding vertex names to false.
> igraph_options()$add.vertex.names
[1] TRUE
> igraph_options(add.vertex.names=F)
> igraph_options()$add.vertex.names
[1] FALSE
Now it should work:
h<-make_tree(10)
set.seed(1)
h<-set_vertex_attr(h, "name", value=sample(1:10,10))
bfs(h, root=1, neimode='out', order=TRUE, rank=TRUE, father=TRUE,unreachable=FALSE)
Output:
$root
[1] 1
$neimode
[1] "out"
$order
+ 10/10 vertices, named:
[1] 3 4 5 7 2 8 9 6 10 1
$rank
[1] 1 2 3 4 5 6 7 8 9 10
$father
+ 10/10 vertices, named:
[1] <NA> 3 3 4 4 5 5 7 7 2
$pred
NULL
$succ
NULL
$dist
NULL
Might be worth raising this on the igraph github, since this seems (at least to me) like undesirable behavior.
In Julia I can create 2D-arrays with
[1 2 3 4 ; 5 6 7 8]
2×4 Array{Int64,2}:
1 2 3 4
5 6 7 8
The problem is, that I need to parse a 2D-array supplied as an argument to a script - that is as a String.
For example
$ julia script.jl "[1 2 3 4 ; 5 6 7 8]"
and in the script something like:
c = parse.(ARGS[1])
and c should be a 2×4 array.
I am flexible regarding the format of the input String.
The usecase is, that I want to call an optimization problem implemented in Julia + JuMP from within Java.
Check out the readdlm function, which will allow you to parse the text received from ARGS as an array:
using DelimitedFiles
a = readdlm(IOBuffer(ARGS[1]),',',';')
display(a)
Running:
$ julia argscript.jl "1,2,3,4;5,6,7,8"
2×4 Array{Float64,2}:
1.0 2.0 3.0 4.0
5.0 6.0 7.0 8.0
You can force the array element type in the script:
a = readdlm(IOBuffer(ARGS[1]),',',Int,';')
You could even enforce the matrix dimensions by passing two more arguments:
using DelimitedFiles
n = parse(Int,ARGS[1])
m = parse(Int,ARGS[2])
a = readdlm(IOBuffer(ARGS[3]),',',Int,';',dims=(n,m))
Running:
$ julia argscript.jl 2 3 "3,2,1;2,6,8"
2×3 Array{Int64,2}:
3 2 1
2 6 8
$ julia argscript.jl 2 4 "3,2,1;2,6,8"
ERROR: LoadError: at row 2, column 1 : ErrorException("missing value at row 1 column 4"))
I have the following code to plot the minimum spanning tree of a graph
## g is an igraph graph
mst = minimum.spanning.tree(g)
E(g)$color <- "SkyBlue2"
## how to I make mst a different color
E(g)[E(mst)]$color = "red" ### <---- I WANT TO DO ESSENTIALLY THIS
plot(g, edge.label=E(g)$weight)
That is, for a simple graph, I find the mst. I want to change the mst to red and plot the mst as part of the main graph. To do this, I want to select the edges of g that are also in mst. How do I do this?
UPDATE:
More generally, I have a graph g0 which is the mst of g, which has n vertices. It was constructed as follows
## implementing the Dijkstra-Prim algorithm
v0 = sample(1:n, 1)
g0 = graph.empty(n=n, directed=FALSE)
weight.g0 = 0
while(length(setdiff(1:n, v0) > 0)) {
## chose the shortest edge in the cut set of g
## to find the cut, figure out the set of edges where vertex is
## in v0 and the other is not
cutset = E(g)[ v0 %->% setdiff(1:n, v0)]
## find the lightest weight edge
cutweights = E(g)$weight[cutset]
lightest_edge_idx = which(cutweights == min(cutweights))[1]
weight.g0 = weight.g0 + min(cutweights)
## get the vertices of the lightest weight edge, add to path
lightest_edge = cutset[as.numeric(cutset)[lightest_edge_idx]]
vertices = get.edges(g, as.numeric(lightest_edge))
g0 <- add.edges(g0, vertices, weight=min(cutweights))
## now that we have the vertices, add the one that is not in the
## graph already
for(vtx in vertices) {
if(!(vtx %in% v0)) {
v0 = c(vtx, v0)
}
}
}
I know I am probably not using a lot of useful features of igraph, but I do get g0 to be a mst at the end of this loop. Given this, I have
E(g0)
Edge sequence:
[1] 8 -- 1
[2] 2 -- 1
[3] 9 -- 8
[4] 9 -- 5
[5] 3 -- 2
[6] 4 -- 3
[7] 7 -- 3
[8] 11 -- 4
[9] 7 -- 6
[10] 11 -- 10
> E(g)
Edge sequence:
[1] 2 -- 1
[2] 5 -- 1
[3] 8 -- 1
[4] 3 -- 2
[5] 5 -- 2
[6] 6 -- 2
[7] 4 -- 3
[8] 6 -- 3
[9] 7 -- 3
[10] 7 -- 4
[11] 11 -- 4
[12] 6 -- 5
[13] 8 -- 5
[14] 9 -- 5
[15] 7 -- 6
[16] 9 -- 6
[17] 10 -- 6
[18] 10 -- 7
[19] 11 -- 7
[20] 9 -- 8
[21] 10 -- 9
[22] 11 -- 10
My question was, how do I assign an attribute to the edges in E(g) that are also in E(g0)?
This is actually quite easy because minimum.spanning.tree() keeps edge attributes. So you just need to assign an edge id attribute, and you'll see which edges to color red. It goes like this:
# Some test data, no edge weights, quite boring
g <- erdos.renyi.game(20,2/20)
g
# IGRAPH U--- 20 24 -- Erdos renyi (gnp) graph
# + attr: name (g/c), type (g/c), loops (g/l), p (g/n)
E(g)$id <- seq_len(ecount(g))
mst <- minimum.spanning.tree(g)
mst
# IGRAPH U--- 20 18 -- Erdos renyi (gnp) graph
# + attr: name (g/c), type (g/c), loops (g/l), p (g/n), id (e/n)
E(mst)$id
# [1] 1 2 3 6 7 8 9 10 11 12 13 16 18 19 20 22 23 24
E(g)$color <- "black"
E(g)$color[E(mst)$id] <- "red"
plot(g)