How can I create a customized colormap for geoviews (bokeh)? - bokeh

I'm trying to plot an xarray dataset in Geoviews, like this:
https://geoviews.org/gallery/bokeh/xarray_image.html#bokeh-gallery-xarray-image
There I can define a colormap by cmap.
The cmap is just a list of hex-codes, like:
['#150b00',
'#9b4e00',
'#f07800',
'#ffa448',
'#a8a800',
'#dddd00',
'#ffff00',
'#ffffb3',
'#ffffff',
'#b0ffff',
'#00e8e8',
'#00bfbf',
'#008a8a',
'#79bcff',
'#0683ff',
'#0000c1',
'#000048']
I want to define to levels of values for these color, like this list:
[-10.0,
-5.0,
-2.5,
-1.0,
-0.5,
-0.2,
-0.1,
-0.05,
0.05,
0.1,
0.2,
0.5,
1.0,
2.5,
5.0,
10.0]
How can I define these levels?

Please try to set the parameter color_levels to the wanted values. This is explained in HoloViews Styling Plots in the section Custom color intervals. HoloVies is the source where the gv.Image comes from. Therefore this should work.
cmap = ['#150b00', '#9b4e00', '#f07800', '#ffa448', '#a8a800', '#dddd00', '#ffff00', '#ffffb3', '#ffffff', '#b0ffff', '#00e8e8', '#00bfbf', '#008a8a', '#79bcff', '#0683ff', '#0000c1', '#000048']
levels = [-10.0, -5.0, -2.5, -1.0, -0.5, -0.2, -0.1, -0.05, 0.05, 0.1, 0.2, 0.5, 1.0, 2.5, 5.0, 10.0]
images.opts(
cmap=cmap,
color_levels=levels,
colorbar=True,
width=600,
height=500) * gf.coastline
Comment
If this is not working, then I apologize. In the moment I am not able to install GeoViews on my machine.

Related

Why do the results of Dunn's test in GraphPad Prism and R differ?

I have three sets of data, to which I want to apply Dunn's test. However, the test shows different results when performed in GraphPad Prism and R. I've been reading a little bit about the test here, but I couldn't understand why there is a difference in the p-values. I even tested in R all the methods to adjust the p-value, but none of them matched the GrapPad Prism result.
Below I present screenshots of the step-by-step in GraphPad Prism and the code I used in R.
library(rstatix)
Day <- rep(1:10, 3)
FLs <- c(rep("FL1", 10), rep("FL2", 10), rep("FL3", 10))
Value <- c(0.2, 0.4, 0.3, 0.2, 0.3, 0.4, 0.2, 0.25, 0.32, 0.21,
0.9, 0.6, 0.7, 0.78, 0.74, 0.81, 0.76, 0.77, 0.79, 0.79,
0.6, 0.58, 0.54, 0.52, 0.39, 0.6, 0.52, 0.67, 0.65, 0.56)
DF <- data.frame(FLs, Day, Value)
Dunn <- DF %>%
dunn_test(Value ~ FLs,
p.adjust.method = "bonferroni",
detailed = TRUE) %>%
add_significance()

bnlearn Error: Wrong number of conditional probability distributions

I am learning to work with bnlearn and I keep running into the following error in the last line of my code below:
Error in custom.fit(dag, cpt) : wrong number of conditional probability distributions
What am I doing wrong?
modelstring(dag)= "[s][r][nblw|r][nblg|nblw][mlw|s:r][f|s:r:mlw][mlg|mlw:f]
[mlgr|mlg:nblg]"
###View DAG Specifics
dag
arcs(dag)
nodes(dag)
# Create Levels
State <- c("State0", "State1")
##Create probability distributions given; these are all 2d b/c they have 1 or 2 nodes
cptS <- matrix(c(0.6, 0.4), ncol=2, dimnames=list(NULL, State))
cptR <- matrix(c(0.7, 0.3), ncol=2, dimnames=list(NULL, State))
cptNBLW <- matrix(c(0.95, 0.05, 0.05, 0.95), ncol=2, dimnames=list(NULL, "r"= State))
cptNBLG <- matrix(c(0.9, 0.099999999999999998, 0.2, 0.8), ncol=2, dimnames=list(NULL,
"nblw"=State))
cptMLG <- matrix(c(0.95, 0.05, 0.4, 0.6, 0.2, 0.8, 0.05, 0.95),ncol=2,nrow = 2,
dimnames=list("mlw"= State, "f"=State))
cptMLGR <- matrix(c(0.6,0.4,0.95,0.05,0.2,0.8,0.55,0.45),ncol=2,nrow = 2,
dimnames=list("mlg"= State, "nblg"=State))
cptMLW <-matrix(c(0.95, 0.05, 0.1, 0.9, 0.2, 0.8, 0.01, 0.99), ncol=2,nrow = 2,byrow = TRUE,
dimnames=list("r"= State, "s"=State))
# Build 3-d matrices( becuase you have 3 nodes, you can't use the matrix function; you
have to build it from scratch)
cptF <- c(0.05, 0.95, 0.4, 0.6, 0.9, 0.1, 0.99, 0.01, 0.9, 0.1, 0.95, 0.05, 0.95, 0.05, 0.99,
0.01)
dim(cptF) <- c(2, 2, 2, 2)
dimnames(cptF) <- list("s"=State, "r"=State, "mlw"=State)
###Create CPT Table
cpt <- list(s = cptS, r = cptR, mlw = cptMLW,nblw= cptNBLW,
mlg= cptMLG, nblg= cptNBLG, mlgr= cptMLGR)
# Construct BN network with Conditional Probability Table
S.net <- custom.fit(dag,cpt)
Reference: https://rpubs.com/sarataheri/bnlearnCGM
You have several errors in your CPT definitions. Primarily, you need to make sure that:
the number of probabilities supplied are equal to the product of the number of states in the child and parent nodes,
that the number of dimensions of the matrix/array is equal to the number of parent nodes plus one, for the child node,
the child node should be given in the first dimension when the node dimension is greater than one.
the names given in the dimnames arguments (e.g. the names in dimnames=list(ThisName = ...)) should match the names that were defined in the DAG, in your case with modelstring and in my answer with model2network. (So my earlier suggestion of using dimnames=list(cptNBLW = ...) should be dimnames=list(nblw = ...) to match how node nblw was declared in the model string)
You also did not add node f into your cpt list.
Below is your code with comments where things have been changed. (I have commented out the offending lines and added ones straight after)
library(bnlearn)
dag <- model2network("[s][r][nblw|r][nblg|nblw][mlw|s:r][mlg|mlw:f][mlgr|mlg:nblg][f|s:r:mlw]")
State <- c("State0", "State1")
cptS <- matrix(c(0.6, 0.4), ncol=2, dimnames=list(NULL, State))
cptR <- matrix(c(0.7, 0.3), ncol=2, dimnames=list(NULL, State))
# add child node into first slot of dimnames
cptNBLW <- matrix(c(0.95, 0.05, 0.05, 0.95), ncol=2, dimnames=list(nblw=State, "r"= State))
cptNBLG <- matrix(c(0.9, 0.099999999999999998, 0.2, 0.8), ncol=2, dimnames=list(nblg=State,"nblw"=State))
# Use a 3d array and not matrix, and add child node into dimnames
# cptMLG <- matrix(c(0.95, 0.05, 0.4, 0.6, 0.2, 0.8, 0.05, 0.95),ncol=2,nrow = 2, dimnames=list("mlw"= State, "f"=State))
cptMLG <- array(c(0.95, 0.05, 0.4, 0.6, 0.2, 0.8, 0.05, 0.95),dim=c(2,2,2), dimnames=list(mlg = State, "mlw"= State, "f"=State))
# cptMLGR <- matrix(c(0.6,0.4,0.95,0.05,0.2,0.8,0.55,0.45),ncol=2,nrow = 2, dimnames=list("mlg"= State, "nblg"=State))
cptMLGR <- array(c(0.6,0.4,0.95,0.05,0.2,0.8,0.55,0.45), dim=c(2,2,2), dimnames=list(mlgr=State, "mlg"= State, "nblg"=State))
# cptMLW <-matrix(c(0.95, 0.05, 0.1, 0.9, 0.2, 0.8, 0.01, 0.99), ncol=2,nrow = 2,byrow = TRUE, dimnames=list("r"= State, "s"=State))
cptMLW <-array(c(0.95, 0.05, 0.1, 0.9, 0.2, 0.8, 0.01, 0.99), dim=c(2,2,2), dimnames=list(mlw=State, "r"= State, "s"=State))
# add child into first slot of dimnames
cptF <- c(0.05, 0.95, 0.4, 0.6, 0.9, 0.1, 0.99, 0.01, 0.9, 0.1, 0.95, 0.05, 0.95, 0.05, 0.99, 0.01)
dim(cptF) <- c(2, 2, 2, 2)
dimnames(cptF) <- list("f" = State, "s"=State, "r"=State, "mlw"=State)
# add missing node f into list
cpt <- list(s = cptS, r = cptR, mlw = cptMLW,nblw= cptNBLW, mlg= cptMLG, nblg= cptNBLG, mlgr= cptMLGR, f=cptF)
# Construct BN network with Conditional Probability Table
S.net <- custom.fit(dag, dist=cpt)

Counterintuitive results for dplyr::between() when using vectors

When using dplyr::between(), I assumed that it would compare each element. However it seems like that is not the case, as shown in the below example.
x <- c(0.2, 0.2, 0.2, 0.5, 0.5, 0.5)
y <- c(0.0, 0.0, 0.0, 0.1, 0.052, -0.3)
z <- c(0.43, 0.52, 0.0, 0.76, 0.85, 0.83)
dplyr::between(x=x, left=y, right=z)
# [1] TRUE TRUE TRUE FALSE FALSE FALSE
For example, in the 3rd element, 0.2 is not between 0.0 and 0.0, but TRUE is returned.
In the 4th element, 0.5 is between 0.052 and 0.85, but FALSE is returned.
Any ideas on what causes this behavior?
dplyr::between only accepts single value in left and right, it cannot work with vector of values.
The behaviour that you expect is present in data.table::between :
x <- c(0.2, 0.2, 0.2, 0.5, 0.5, 0.5)
y <- c(0.0, 0.0, 0.0, 0.1, 0.052, -0.3)
z <- c(0.43, 0.52, 0.0, 0.76, 0.85, 0.83)
data.table::between(x=x, lower=y, upper=z)
#[1] TRUE TRUE FALSE TRUE TRUE TRUE
In base R, this is easier with comparison operators and is flexible
x >= y & x <= z
#[1] TRUE TRUE FALSE TRUE TRUE TRUE
data
x <- c(0.2, 0.2, 0.2, 0.5, 0.5, 0.5)
y <- c(0.0, 0.0, 0.0, 0.1, 0.052, -0.3)
z <- c(0.43, 0.52, 0.0, 0.76, 0.85, 0.83)

qplot: Only graphing nodes below a threshold

I am trying to make a visual graph of a dissimilarity matrix. Using this site, I ran into the qgraph function from the package qgraph. Using the threshold flag, I am able to remove edges from my network above the supplied numerical value. This works beautifully, however, what if I only want to plot values below a certain threshold, not above?
For this, I came back to this site and read here: How to plot near-zero values with qgraph? to use the cut flag for this purpose. However, as the answer states, this flag will only "adjust the saturation so that everything above the cut point has the strongest color intensity, anything below the cut point, the saturation gets weaker."
What I would like to do is to plot only lines between the nodes that are below my cut value (or threshold), not anything else.
Here is some reproducible data:
Dist <- data.frame(Sample_1 = c(0.0, 0.245, 0.191, 0.78, 0.5),
Sample_2 = c(0.3, 0.0, 0.2, 0.99, 0.6),
Sample_3 = c(0.65, 0.45, 0.0, 0.05, 0.8),
Sample_4 = c(0.45, 0.06, 0.88, 0.0, 0.7),
Sample_5 = c(0.11, 0.79, 0.66, 0.37, 0.0),
row.names = c("Sample_1", "Sample_2", "Sample_3", "Sample_4", "Sample_5"))
Plotting the graph:
qgraph(Dist, layout = "circle", vsize = 5, color = c("cyan", "yellow", "pink", "green3", "gray"), labels = c("Sample_1", "Sample_2", "Sample_3", "Sample_4", "Sample_5"), label.cex = 3, cut = 0.2)
As you can see, anything above the cut = 0.2 is also plotted and darker.
I would like only values below the 0.2 threshold to be plotted. Is there any way to do this?
Thanks.
qgraph does not seems to have the ability to cut below a threshold, so we have to manipulate the input data.
Replacing values above the threshold to 0 or NA should do it. Using NA result in the same output but with a warning.
Dist <- data.frame(
Sample_1 = c(0.0, 0.245, 0.191, 0.78, 0.5),
Sample_2 = c(0.3, 0.0, 0.2, 0.99, 0.6),
Sample_3 = c(0.65, 0.45, 0.0, 0.05, 0.8),
Sample_4 = c(0.45, 0.06, 0.88, 0.0, 0.7),
Sample_5 = c(0.11, 0.79, 0.66, 0.37, 0.0),
row.names = c("Sample_1", "Sample_2", "Sample_3", "Sample_4", "Sample_5")
)
library(qgraph)
qgraph(
replace(Dist, Dist > 0.2, 0),
layout = "circle",
vsize = 5,
color = c("cyan", "yellow", "pink", "green3", "gray"),
labels = c("Sample_1", "Sample_2", "Sample_3", "Sample_4", "Sample_5"),
label.cex = 3
)
Created on 2020-04-06 by the reprex package (v0.3.0)

unable to scrape by traversing <br> in <td> using scrapy with css

html code is following:
<td class="column-3">
(price per 1,000 images)<br>
0-1M images -
<span class="price-data " data-amount="{"regional":{"asia-pacific-southeast":0.5,"australia-east":0.5,"brazil-south":0.5,"canada-central":0.5,"central-india":0.5,"europe-north":0.5,"europe-west":0.5,"united-kingdom-south":0.5,"us-east":0.5,"us-east-2":0.5,"us-south-central":0.5,"us-west-2":0.5,"us-west-central":0.5}}" data-decimals="3" data-decimals-force="0" data-region-unavailable="N/A" data-has-valid-price="true">$0.50</span> <br>
1M-5M images -
<span class="price-data " data-amount="{"regional":{"asia-pacific-southeast":0.4,"australia-east":0.4,"brazil-south":0.4,"canada-central":0.4,"central-india":0.4,"europe-north":0.4,"europe-west":0.4,"united-kingdom-south":0.4,"us-east":0.4,"us-east-2":0.4,"us-south-central":0.4,"us-west-2":0.4,"us-west-central":0.4}}" data-decimals="3" data-decimals-force="0" data-region-unavailable="N/A" data-has-valid-price="true">$0.40</span> <br>
5M+ images -
<span class="price-data " data-amount="{"regional":{"asia-pacific-southeast":0.325,"australia-east":0.325,"brazil-south":0.325,"canada-central":0.325,"central-india":0.325,"europe-north":0.325,"europe-west":0.325,"united-kingdom-south":0.325,"us-east":0.325,"us-east-2":0.325,"us-south-central":0.325,"us-west-2":0.325,"us-west-central":0.325}}" data-decimals="3" data-decimals-force="0" data-region-unavailable="N/A" data-has-valid-price="true">$0.325</span> <br>
</td>
url: https://azure.microsoft.com/en-in/pricing/details/search/
How can I traverse <br> and scrape the data? I want to split td tags into count(br) times and then scrape. I don't want to use xpath. I want to get the result through css.
dumb = 'Your response, or above text'
html_dumb = Selector(text=dumb)
td_vals = [x.strip().strip('- ') for x in
html_dumb.xpath("//td/text()").extract() if x.strip()] #got all td values
f_val = td_vals[0] # seperate the first one. here (price per 1,000 images)
td_vals = td_vals[1:]
span_vals = [x.strip() for x in html_dumb.xpath("//span/#data-amount").extract() if x.strip()] #got all span data, you can also get span text if you need
inner_json = {}
result = {}
for td_val, span_val in zip(td_vals, span_vals):
d[td_val] = json.loads(span_val) #building inner dictionary
result[f_val] = d #append in outer one
{u'(price per 1,000 images)': {u'5M+ images': {u'regional': {u'united-kingdom-south': 0.325, u'europe-north': 0.325, u'brazil-south': 0.325, u'us-west-2': 0.325, u'us-south-central': 0.325, u'central-india': 0.325, u'us-east': 0.325, u'canada-central': 0.325, u'europe-west': 0.325, u'us-east-2': 0.325, u'us-west-central': 0.325, u'asia-pacific-southeast': 0.325, u'australia-east': 0.325}}, u'0-1M images': {u'regional': {u'united-kingdom-south': 0.5, u'europe-north': 0.5, u'brazil-south': 0.5, u'us-west-2': 0.5, u'us-south-central': 0.5, u'central-india': 0.5, u'us-east': 0.5, u'canada-central': 0.5, u'europe-west': 0.5, u'us-east-2': 0.5, u'us-west-central': 0.5, u'asia-pacific-southeast': 0.5, u'australia-east': 0.5}}, u'1M-5M images': {u'regional': {u'united-kingdom-south': 0.4, u'europe-north': 0.4, u'brazil-south': 0.4, u'us-west-2': 0.4, u'us-south-central': 0.4, u'central-india': 0.4, u'us-east': 0.4, u'canada-central': 0.4, u'europe-west': 0.4, u'us-east-2': 0.4, u'us-west-central': 0.4, u'asia-pacific-southeast': 0.4, u'australia-east': 0.4}}}}

Resources