How to map transform in r-drake - mlr3

May i ask how to use function GraphLearner$new to transform in r-drake.
The codes following:
learner_plan = drake_plan(
## 1. Choose learner--------------------------------------------------------
polrn_classif = target(fnc_po_learner(list_learners),
transform = map(
list_learners = c(
"classif.ranger",
"classif.lightgbm",
"classif.xgboost",
"classif.log_reg",
"classif.svm"
)
)),
graph_classif = target(
imputer_classif %>>% filter_classif %>>% polrn_classif,
transform = map(polrn_classif)
),
glrn_classif = target({
GraphLearner$new(graph = graph_classif) # get error ---------- it's not transform
transform = map(graph_classif)
})
)
learner_plan

transform = map(graph_classif) should go outside the curly braces.
library(drake)
learner_plan = drake_plan(
polrn_classif = target(
fnc_po_learner(list_learners),
transform = map(
list_learners = c(
"classif.ranger",
"classif.lightgbm",
"classif.xgboost",
"classif.log_reg",
"classif.svm"
)
)
),
graph_classif = target(
imputer_classif %>>% filter_classif %>>% polrn_classif,
transform = map(polrn_classif)
),
glrn_classif = target({
GraphLearner$new(graph = graph_classif)
}, transform = map(graph_classif)) # outside the curly braces
)
plot(learner_plan)
Created on 2021-07-07 by the reprex package (v2.0.0)

Related

'Not callable' error when calculating integrated gradient interpretability with captum

I’m trying to perform model interpretability with captum but running into an error. Specifically, it says:
/usr/lib/python3.7/inspect.py in _signature_from_callable(obj, follow_wrapper_chains, skip_bound_arg, sigcls)
2206
2207 if not callable(obj):
-> 2208 raise TypeError('{!r} is not a callable object'.format(obj))
2209
2210 if isinstance(obj, types.MethodType):
I’m not certain how to resolve this. Here’s the definition of my model, for reference:
class dvib(nn.Module):
def __init__(self,k,out_channels, hidden_size):
super(dvib, self).__init__()
self.conv = torch.nn.Conv2d(in_channels=1,
out_channels = out_channels,
kernel_size = (1,20),
stride=(1,1),
padding=(0,0),
)
self.rnn = torch.nn.GRU(input_size = out_channels,
hidden_size = hidden_size,
num_layers = 2,
bidirectional = True,
batch_first = True,
dropout = 0.2
)
self.fc1 = nn.Linear(hidden_size*4, hidden_size*4)
self.enc_mean = nn.Linear(hidden_size*4+578,k)
self.enc_std = nn.Linear(hidden_size*4+578,k)
self.dec = nn.Linear(k, 2)
nn.init.xavier_uniform_(self.fc1.weight)
nn.init.constant_(self.fc1.bias, 0.0)
nn.init.xavier_uniform_(self.enc_mean.weight)
nn.init.constant_(self.enc_mean.bias, 0.0)
nn.init.xavier_uniform_(self.enc_std.weight)
nn.init.constant_(self.enc_std.bias, 0.0)
nn.init.xavier_uniform_(self.dec.weight)
nn.init.constant_(self.dec.bias, 0.0)
def cnn_gru(self,x,lens):
print(x.shape)
x = x.unsqueeze(1)
print('after first unsqueeze: ', x.shape)
x = self.conv(x)
print('after conv: ', x.shape)
x = torch.nn.ReLU()(x)
print('shape after relu: ', x.shape,type(x))
x = x.squeeze(3)
print('shape after squeeze: ', x.shape)
x = x.view(x.size(0),-1)
x = x.permute(0,2,1)
print('shape after permute: ', x.shape)
print(type(lens))
gru_input = pack_padded_sequence(x,lens,batch_first=True, enforce_sorted=False)
output, hidden = self.rnn(gru_input)
print('hidden layer: ', hidden.shape)
output_all = torch.cat([hidden[-1],hidden[-2],hidden[-3],hidden[-4]],dim=1)
print("output_all.shape:",output_all.shape)
return output_all
def forward(self, pssm, lengths, FEGS):
cnn_vectors = self.cnn_gru(pssm, lengths)
feature_vec = torch.cat([cnn_vectors, FEGS], dim = 1)
enc_mean, enc_std = self.enc_mean(feature_vec), f.softplus(self.enc_std(feature_vec)-5)
eps = torch.randn_like(enc_std)
latent = enc_mean + enc_std*eps
outputs = f.sigmoid(self.dec(latent))
print(outputs.shape)
return outputs, enc_mean, enc_std, latent
I load pretrained weights into the model as well, prior to passing it to captum with the relevant arguments:
ig = IntegratedGradients(model(test_pssm_small, test_len_small, test_FEGS_small))
attr = ig.attribute(test_FEGS_small, n_steps=5)

Using Manim, can I draw a new object in the background of existing ones?

I have illustrated the parallelogram spanned by two vectors, and would like to shade in the area of that parallelogram, which I tried to do like so:
from manim import *
import numpy as np
class DrawParallelogram( Scene ):
def construct( self ):
o = np.array( [ 0, -2, 0 ] )
p1 = np.array( [ 3, 1, 0 ] ) # u
p2 = np.array( [ 1, 3, 0 ] ) # v
op1 = o + p1
op2 = o + p2
op3 = o + p1 + p2
v1 = Arrow( start = o, end = op1, buff = 0, color = RED ) # u
v2 = Arrow( start = o, end = op2, buff = 0, color = YELLOW ) # v
v1p = Arrow( start = op2, end = op3, buff = 0, color = RED ) # u'
v2p = Arrow( start = op1, end = op3, buff = 0, color = YELLOW ) # v'
parallelogram = [ o, op1, op3, op2 ]
poly = Polygon( *parallelogram, color = PURPLE, fill_opacity = 0.5 )
self.play( AnimationGroup( Write( v1 ), Write( v2 ), Write( v1p ), Write( v2p ) ) )
self.wait( )
self.play( Write( poly ) )
However, this parallelogram colors over the arrows that I have already drawn, like so:
and I'd like it to be in the background. Is there a way to introduce a new object into the scene so that it is logically behind any of the existing ones, as if I had drawn it first, so that it would look like:
You can use the set_z_index method to set the z_index property of the parallelogram to a value less than that of the arrows.
Here I have set it to a lower value than that of v1:
poly.set_z_index(v1.z_index - 1)
Alternatively you can manipulate the z_index property directly:
poly.z_index = v1.z_index - 1
Using the set_z_index method would be the cleaner solution.
Complete code:
from manim import *
import numpy as np
class DrawParallelogram( Scene ):
def construct( self ):
o = np.array( [ 0, -2, 0 ] )
p1 = np.array( [ 3, 1, 0 ] ) # u
p2 = np.array( [ 1, 3, 0 ] ) # v
op1 = o + p1
op2 = o + p2
op3 = o + p1 + p2
v1 = Arrow( start = o, end = op1, buff = 0, color = RED ) # u
v2 = Arrow( start = o, end = op2, buff = 0, color = YELLOW ) # v
v1p = Arrow( start = op2, end = op3, buff = 0, color = RED ) # u'
v2p = Arrow( start = op1, end = op3, buff = 0, color = YELLOW ) # v'
parallelogram = [ o, op1, op3, op2 ]
poly = Polygon( *parallelogram, color = PURPLE, fill_opacity = 0.5 )
# Set the z-index
poly.set_z_index(v1.z_index - 1)
self.play( AnimationGroup( Write( v1 ), Write( v2 ), Write( v1p ), Write( v2p ) ) )
self.wait( )
self.play( Write( poly ) )

bind_plans and map transformation

Is it possible to use a map transformation with a grouping variable that is described in an external plan?
In other words, this works for me:
plan_a = drake_plan(
foo = target(x + 1, transform = map(x = c(4, 5, 6))),
bar = target(y + 5, transform = map(foo))
)
but this doesn't:
plan_a = drake_plan(
foo = target(x + 1, transform = map(x = c(4, 5, 6))),
)
plan_b = drake_plan(
bar = target(y + 5, transform = map(foo))
)
bind_plans(plan_a, plan_b)
Thanks!
I found a solution using the transform_plan function.

Creating flowchart with DiagrammeR nodes and edges instead of graphviz

I would like to create a flowchart using the DiagrammeR nodes and edges functionality with R instead of using the graphviz wrapper function.
However, I can't figure out how to make the edges straight to make it nice.
This is the graphviz solution that looks like what I want:
# Packages needed for the test
library(DiagrammeR)
# grViz solution
grViz("digraph flowchart {
# node definitions with substituted label text
node [fontname = Helvetica, shape = rectangle]
tab1 [label = '##1', group=gr1]
tab2 [label = '##2', group=gr2]
tab3 [label = '##3', group=gr3]
invis1 [style = invis, shape=point, width = 0, group=gr1]
invis1a [style = invis, shape=point, width = 0, group=gr2]
invis1b [style = invis, shape=point, width = 0, group=gr3]
# edge definitions with the node IDs
edge [arrowhead='none']
tab1 -> invis1;
invis1a -> invis1 -> invis1b; {rank=same invis1a invis1 invis1b}
edge [arrowhead='normal']
invis1a -> tab2;
invis1b -> tab3; {rank=same tab2 tab3}}
[1]: 'A'
[2]: 'B'
[3]: 'C'
")
This is my attempt to recreate the same graph with the nodes and edges solution:
# Packages needed for the test
library(DiagrammeR)
library(magrittr)
# Node and edge df solution
create_graph() %>%
add_node( # id 1
label = "A",
type = "group_1",
node_aes = node_aes(
style = "filled",
shape = "rectangle",
fixedsize = FALSE)
) %>%
add_node( # id 2
type = "group_1",
node_aes = node_aes(
style = "invisible",
height = 0,
width = 0)
) %>%
add_edge(
from = 1,
to = 2,
edge_aes = edge_aes(
arrowhead = "none")
) %>%
add_node( # id 3
type = "group_2",
node_aes = node_aes(
style = "invisible",
height = 0,
width = 0)
) %>%
add_edge(
from = 2,
to = 3,
edge_aes = edge_aes(
arrowhead = "none")
) %>%
add_node( # id 4
type = "group_3",
node_aes = node_aes(
style = "invisible",
height = 0,
width = 0)
) %>%
add_edge(
from = 2,
to = 4,
edge_aes = edge_aes(
arrowhead = "none")
) %>%
add_node( # id 5
label = "B",
type = "group_2",
node_aes = node_aes(
style = "filled",
shape = "rectangle",
fixedsize = FALSE)
) %>%
add_edge(
from = 3,
to = 5,
edge_aes = edge_aes(
arrowhead = "normal")
) %>%
add_node( # id 6
label = "C",
type = "group_3",
node_aes = node_aes(
style = "filled",
shape = "rectangle",
fixedsize = FALSE)
) %>%
add_edge(
from = 4,
to = 6,
edge_aes = edge_aes(
arrowhead = "normal")
) %>%
render_graph()

Mixing solidguage and column in a graph with highcharter

I have this two dataframes
1) t1 (Var1(numeric), Var2(string), Freq (numeric))
2) pref.media (pref.media (numeric)
I have this separated graphs, but I want to print like two series in the same graph
1) Column chart, ok
column1 <- highchart() %>%
hc_add_series(t1$Freq, type = "column") %>%
hc_xAxis(categories = t1$Var1) %>%
hc_plotOptions(series = list(showInLegend = FALSE,dataLabels = list(enabled = TRUE, color = t1$Var1)))
2) Solid gauge chart (ok)
gauge1 <- highchart(width = 800, height = 600) %>%
hc_chart(type = "solidgauge",backgroundColor = "#F0F0F0",marginTop = 50) %>%
hc_title(text = "Preferencia",style = list(fontSize = "24px")) %>%
hc_tooltip(borderWidth = 0, backgroundColor = 'none',shadow = FALSE,style = list(fontSize = '16px')) %>%
hc_pane(startAngle = -90,endAngle = 90) %>%
hc_yAxis(min = 0,max = 11,lineWidth = 0,tickPositions = list(0,1,2,3,4,5,6,7,8,9,10,11)) %>%
hc_plotOptions(solidgauge = list(borderWidth = '34px',dataLabels = list(enabled = TRUE, style = list(borderWidth = 3,backgroundColor = 'none',shadow = FALSE, fontSize = '16px',color="#888800")),linecap = 'round',stickyTracking = FALSE)) %>%
hc_add_series(name = "Preferencia",borderColor = "#000000",data = list(list(color = "#888800"),radius = "100%",innerRadius = "100%",y = round(pref.media[[1,1]],2)))
3) Mixing the charts (not ok, with errors)
highchart() %>%
hc_add_series(t1, "column", hcaes(x = Var1, y = Freq), name = "Preferencia") %>%
hc_add_series(pref.media, "solidgauge", hcaes(name = "Gauge", y = round(pref.media[[1,1]],2)), name = "Indicador") %>%
hc_plotOptions(
series = list(showInLegend = FALSE,pointFormat = "{point.y}%"),
column = list(colorByPoint = TRUE),
solidgauge = list(borderWidth = '34px',dataLabels = list(enabled = TRUE),linecap = 'round',stickyTracking = FALSE)) %>%
hc_pane(startAngle = -90,endAngle = 90) %>%
hc_yAxis(title = list(text = "Porcentaje de preferencia"),labels = list(format = "{value}%"), max = 100) %>%
hc_xAxis(categories = t1$Var1) %>%
hc_title(text = "Título") %>%
hc_subtitle(text = "Subtítulo")
I don't understand the sequence for mixing two series. I tried it with a J.Kunst's example in http://jkunst.com/highcharter/highcharts.html
Thanks

Resources