How to save Sankey diagram node positions after manual arrangement? - r-plotly

I am trying to create Sankey diagrams with node positions using R and plotly package (https://plotly.com/r/sankey-diagram/#define-node-position).
library(plotly)
fig <- plot_ly(
type = "sankey",
arrangement = "snap",
node = list(
label = c("A", "B", "C", "D", "E", "F"),
x = c(0.2, 0.1, 0.5, 0.7, 0.3, 0.5),
y = c(0.7, 0.5, 0.2, 0.4, 0.2, 0.3),
pad = 10), # 10 Pixel
link = list(
source = c(0, 0, 1, 2, 5, 4, 3, 5),
target = c(5, 3, 4, 3, 0, 2, 2, 3),
value = c(1, 2, 1, 1, 1, 1, 1, 2)))
fig <- fig %>% layout(title = "Sankey with manually positioned node")
fig
However, the default node positions are not good enough. So I need to manually adjust the node positions from the interactive plot. However, I need to save the new positions and update the positions in the script. Then next time when I run the script again, I don't need to adjust the positions manually again?
Is there anyway to save node positions after manual arrangement?

Related

How to construct a sobel filter for kernel initialization in input layer for images of size 128x128x3?

This is my code for sobel filter:
def init_f(shape, dtype=None):
sobel_x = tf.constant([[-5, -4, 0, 4, 5], [-8, -10, 0, 10, 8], [-10, -20, 0, 20, 10], [-8, -10, 0, 10, 8], [-5, -4, 0, 4, 5]])
ker = np.zeros(shape, dtype)
ker_shape = tf.shape(ker)
kernel = tf.tile(sobel_x, ker_shape)//*Is this correct?*
return kernel
model.add(Conv2D(filters=30, kernel_size=(5,5), kernel_initializer=init_f, strides=(1,1), activation='relu'))
So far I have managed to do this.
But, this gives me error:
Shape must be rank 2 but is rank 4 for 'conv2d_17/Tile' (op: 'Tile') with input shapes: [5,5], [4].
Tensorflow Version: 2.1.0
You're close, but the args to tile don't appear to be correct. That is why you're getting the error "Shape must be rank 2 but is rank 4 for..." You're sobel_x must be a rank 4 tensor, so you need to add two more dimensions. I used reshape in this example.
from tensorflow import keras
import tensorflow as tf
import numpy
def kernelInitializer(shape, dtype=None):
print(shape)
sobel_x = tf.constant(
[
[-5, -4, 0, 4, 5],
[-8, -10, 0, 10, 8],
[-10, -20, 0, 20, 10],
[-8, -10, 0, 10, 8],
[-5, -4, 0, 4, 5]
], dtype=dtype )
#create the missing dims.
sobel_x = tf.reshape(sobel_x, (5, 5, 1, 1))
print(tf.shape(sobel_x))
#tile the last 2 axis to get the expected dims.
sobel_x = tf.tile(sobel_x, (1, 1, shape[-2],shape[-1]))
print(tf.shape(sobel_x))
return sobel_x
x1 = keras.layers.Input((128, 128, 3))
cvl = keras.layers.Conv2D(30, kernel_size=(5,5), kernel_initializer=kernelInitializer, strides=(2,2), activation='relu')
model = keras.Sequential();
model.add(x1)
model.add(cvl)
data = numpy.ones((1, 128, 128, 3))
data[:, 0:64, 0:64, :] = 0
pd = model.predict(data)
print(pd.shape)
d = pd[0, :, :, 0]
for row in d:
for col in row:
m = '0'
if col != 0:
m = 'X'
print(m, end="")
print("")
I looked at using expand_dims instead of reshape but there didn't appear any advantage. broadcast_to seems ideal, but you still have to add the dimensions, so I don't think it was better than tile.
Why 30 filters of the same filter though? Are they going to be changed afterwards?

MiniZinc Geocode not printing all solutions to CSP with "all" solutions enabled

The Issue
With solve minimize I only get one solution, even though there are multiple optimal solutions. I have enabled printout of multiple solutions in the solver configurations. The other optimal solutions are found with solve satisfy, along with non-optimal solutions.
Possible causes
Could it be that the cardinality function card() ranks by enum value where size of two sets are equal? In other words that card(A, B) > card(B, C)? If so, do I have to switch the representation of my vertices?
The Program
I am creating a MiniZinc program for finding the minimum vertex cover of a given graph. The graph in this example is this:
With Minimal Vertex Cover solutions being:
[{A, B, C, E}, {A, B, E, F}, {A, C, D, E}, {B, C, D, E}, {B, C, D, F}, {B, D, E, F}]. My code only outputs {A, B, C, E}.
Data file:
VERTEX = {A, B, C, D, E, F};
edges = [|1, 0, 1, 0, 0, 0, 0, 0, 0
|1, 1, 0, 1, 1, 0, 0, 0, 0
|0, 1, 0, 0, 0, 1, 1, 0, 0
|0, 0, 1, 1, 0, 0, 0, 1, 0
|0, 0, 0, 0, 1, 1, 0, 1, 1
|0, 0, 0, 0, 0, 0, 1, 0, 1|];
Solver program:
% Vertices in graph
enum VERTEX;
% Edges between vertices
array[VERTEX, int] of int: edges;
int: num_edges = (length(edges) div card(VERTEX));
% Set of vertices to find
var set of VERTEX: span;
% Number of vertices connected to edge resulting from span
array[1..num_edges] of var 0..num_edges: conn;
% All edges must be connected with at least one vertex from span
constraint forall(i in 1..num_edges)
(conn[i] >= 1);
% The number of connections to each edge is the number of vertices
% in span with a connection to that edge
constraint forall(i in 1..num_edges)
(conn[i] = sum([edges[vert,i]| vert in span]));
% Minimize the number of vertices in span
solve minimize card(span);
solve minimize only show one optimal solution (in some cases, intermediate values might also be shown).
If you want all optimal solutions you must use solve satisfy and add the constraint with the optimal value:
constraint card(span) = 4;
Then the model outputs all the 6 optimal solutions:
card(cpan): 4
span: {A, B, C, E}
conn: [2, 2, 1, 1, 2, 2, 1, 1, 1]
----------
card(cpan): 4
span: {B, C, D, F}
conn: [1, 2, 1, 2, 1, 1, 2, 1, 1]
----------
card(cpan): 4
span: {A, C, D, E}
conn: [1, 1, 2, 1, 1, 2, 1, 2, 1]
----------
card(cpan): 4
span: {B, C, D, E}
conn: [1, 2, 1, 2, 2, 2, 1, 2, 1]
----------
card(cpan): 4
span: {A, B, E, F}
conn: [2, 1, 1, 1, 2, 1, 1, 1, 2]
----------
card(cpan): 4
span: {B, D, E, F}
conn: [1, 1, 1, 2, 2, 1, 1, 2, 2]
----------
==========
Note: I added the output section to show all the values:
output [
"card(cpan): \(card(span))\n",
"span: \(span)\n",
"conn: \(conn)"
];
An alternative solution is to use OptiMathSAT (v. 1.6.3).
When asking for all solutions in optimization mode, the solver returns all solutions (with respect to the output variables) with the same optimal value.
Example:
~$ mzn2fzn test.mzn test.dzn # your instance
~$ optimathsat -input=fzn -opt.fzn.all_solutions=True < test.fzn
% allsat model
span = {2, 4, 5, 6};
conn = array1d(1..9, [1, 1, 1, 2, 2, 1, 1, 2, 2]);
----------
% allsat model
span = {1, 3, 4, 5};
conn = array1d(1..9, [1, 1, 2, 1, 1, 2, 1, 2, 1]);
----------
% allsat model
span = {1, 2, 3, 5};
conn = array1d(1..9, [2, 2, 1, 1, 2, 2, 1, 1, 1]);
----------
% allsat model
span = {1, 2, 5, 6};
conn = array1d(1..9, [2, 1, 1, 1, 2, 1, 1, 1, 2]);
----------
% allsat model
span = {2, 3, 4, 5};
conn = array1d(1..9, [1, 2, 1, 2, 2, 2, 1, 2, 1]);
----------
% allsat model
span = {2, 3, 4, 6};
conn = array1d(1..9, [1, 2, 1, 2, 1, 1, 2, 1, 1]);
----------
=========
The main advantage wrt. the approach presented in the accepted answer is that OptiMathSAT is incremental, meaning that the tool searches for other solutions without being restarted, so that it can re-use any useful information that has been previously generated to speed-up the search (e.g. theory lemmas). [CAVEAT: this may not be relevant for small instances; also, other MiniZinc solvers may still be faster depending on the input problem]
Note: please notice that OptiMathSAT does not print the labels of each VERTEX, because the mzn2fzn compiler removes these labels when compiling the file. However, the mapping among numbers and labels should be obvious.
Disclosure: I am one of the developers of this tool.

Labeling x axis on qicharts

I have produced the Individuals control chart depicted below. The data set has a variable named date that is in R format YMD. I would like to have the x axis display each YMD with a label identifying the specific YMD.
Here is my current code to produce the individuals control chart: qic(data$records, chart=c("i"), x.format="%Y-%m-%d").
Individuals Control Chart
Thanks!
You need to specify the x and xlab arguments. Here's an example using the updated qicharts2 package:
library(qicharts2)
# Build data frame for example
df <- data.frame(x = rep(1:24, 4),
ReportMonth = (rep(seq(as.Date('2014-1-1'),
length.out = 24,
by = 'month'),
4)),
num = rbinom(4 * 24, 100, 0.5),
denom = round(runif(4 * 24, 90, 110)),
grp1 = rep(c('g', 'h'), each = 48),
grp2 = rep(c('A', 'B'), each = 24))
#now plot an 'i' chart :
qic(
x= ReportMonth,
y= num,
# n= denom,
data=df,
chart= "i",
x.format="%Y-%m-%d",
x.angle = 90,
y.expand = 40, # where to start y axis from
xlab = "Month",
ylab= "Value")
Output:

Looping over combinations of several variables for Design Of Experiments in MATLAB

I need to loop over all the possible combinations of the different elements of a vector between lower and upper bounds.
NUMBEROFVARIABLES stores the total number of variables.
tlb < t < tub and o1b < o < oub
The elements t and o can vary in steps of tStep and oStep respectively.
The vector is: x = [t1, t2, t3, ... tn, o1, o2, o3, ... on]
For 1 < t < 10, 1.5 < o < 5, tStep = 1, oStep = 0.5 and NUMBEROFVARIABLES = 10 (divided equally/half-half between 't's and 'o's), the generated combination vectors should be something like:
x1 = [1, 1, 1, 1, 1, 1.5, 1.5, 1.5, 1.5, 1.5]
x2 = [1, 1, 1, 1, 1, 1.5, 1.5, 1.5, 1.5, 2.0]
x3 = [1, 1, 1, 1, 1, 1.5, 1.5, 1.5, 1.5, 2.5]
.
.
.
xi = [1, 1, 1, 1, 2, 5.0, 5.0, 5.0, 5.0, 5.0]
xii = [1, 1, 1, 1, 3, 5.0, 5.0, 5.0, 5.0, 5.0]
xiii = [1, 1, 1, 1, 4, 5.0, 5.0, 5.0, 5.0, 5.0]
.
.
.
xn = [10, 10, 10, 10, 10, 5.0, 5.0, 5.0, 5.0, 5.0]]
How do I loop over each parameter (like in the above example) and store the vectors xi in MATLAB?

MATLAB, frequency table with a class of interval of size 2

The following data display the number of errors per book for 20 publisher
2, 5, 2, 8, 2, 3, 5, 6, 1, 0, 2, 0, 1, 5, 0, 0, 4, 5, 1, 2
Now i want to compute a frequency table with a class of interval of size 2 and relative frequency by using MATLAB.
I can make a frequency table by the command tabulate(x) but do not finding any reference that clarify how to compute a frequency table with a class of interval of size 2.
You can use histc, which allows to specify the edges of the histogram bins. It doesn't compute relative frequencies or print a table though, you have to do this yourself:
% error data
e = [2, 5, 2, 8, 2, 3, 5, 6, 1, 0, 2, 0, 1, 5, 0, 0, 4, 5, 1, 2];
% bin edges
be = 0 :2: ceil(max(e) / 2) * 2;
% absolute frequencies
af = histc(e, be);
% relative frequencies
rf = af / sum(af);
% print table
fprintf(' Value Count Percent\n')
fprintf(' %d-%d\t %d\t %5.2f%%\n', [be; be + 1; af; rf * 100])
The result is:
Value Count Percent
0-1 7 35.00%
2-3 6 30.00%
4-5 5 25.00%
6-7 1 5.00%
8-9 1 5.00%