How can I extract a verb definition as data? [duplicate] - j

In the console, typing a single verb without parameters will print its content:
tolower
3 : 0
x=. I. 26 > n=. ((65+i.26){a.) i. t=. ,y
($y) $ ((x{n) { (97+i.26){a.) x}t
)
That's nice for development, but unexploitable during execution. Is there a way to do that dynamically? Is there a verb that can return the contents of another verb?
For example:
showverb 'tolower'
or
showverb tolower

You can use its representation. For example the boxed representation (5!:2) of tolower is:
(5!:2) <'tolower'
┌─┬─┬────────────────────────────────────────┐
│3│:│x=. I. 26 > n=. ((65+i.26){a.) i. t=. ,y│
│ │ │($y) $ ((x{n) { (97+i.26){a.) x}t │
└─┴─┴────────────────────────────────────────┘
its linear (5!:5) is:
(5!:5) <'tolower'
3 : 0
x=. I. 26 > n=. ((65+i.26){a.) i. t=. ,y
($y) $ ((x{n) { (97+i.26){a.) x}t
)

Related

Is there a way to handle a variable number of inputs/outputs in Nextflow?

Is there a way to handle a varying number of inputs/outputs in Nextflow? Sometimes in the example below process 'foo' will have three inputs (and therefore create three pngs that need stitched together by 'bar') but other times there will be two or four. I'd like process 'bar' to be able to combine all existing files in 'foo.out.files' regardless of number. As it stands this would be able to properly handle everything only if there were exactly three inputs in params.input, but not if there were two or four.
Thanks!
#!/usr/bin/env nextflow
nextflow.enable.dsl=2
process foo {
input:
path input_file
output:
path '*.png', emit files
"""
script that creates variable number of png files
"""
}
process bar {
input:
tuple path(file_1), path(file_2), path(file_3)
"""
script that combines png files ${file_1} ${file_2} ${file_3}
"""
}
workflow {
foo(params.input)
bar(foo.out.files.collect())
}
UPDATE:
I'm getting 'Input tuple does not match input set cardinality' errors for this, for example:
params.num_files = 3
process foo {
input:
val num_files
output:
path '*.png', emit: files
"""
touch \$(seq -f "%g.png" 1 ${num_files})
"""
}
process bar {
debug true
input:
tuple val(word), path(png_files)
"""
echo "${word} ${png_files}"
"""
}
workflow {
foo( params.num_files )
words = Channel.from('a','b','c')
words
.combine(foo.out.files)
.set { combined }
bar(combined)
}
You don't acutally need the tuple qualifier here: the path input qualifier can also handle a collection of files. If you use a variable or the * wildcard, the original filenames will be preserved. In the example below, the variable refers to all files in the list. But if you need to, you can also access specific entries; for example:
params.num_files = 3
process foo {
input:
val num_files
output:
path '*.png', emit: files
"""
touch \$(seq -f "%g.png" 1 ${num_files})
"""
}
process bar {
debug true
input:
path png_files
script:
def first_png = png_files.first()
def last_png = png_files.last()
"""
echo ${png_files}
echo "${png_files[0]}"
echo "${first_png}, ${last_png}"
"""
}
workflow {
bar( foo( params.num_files ) )
}
Results:
$ nextflow run main.nf
N E X T F L O W ~ version 22.04.0
Launching `main.nf` [mighty_solvay] DSL2 - revision: 662b108e42
executor > local (2)
[e0/a619c4] process > foo [100%] 1 of 1 ✔
[ba/5f8032] process > bar [100%] 1 of 1 ✔
1.png 2.png 3.png
1.png
1.png, 3.png
If you need to avoid potential filename collisions, you can have Nextflow rewrite the input filenames using a name pattern. If the name pattern is a simple string and a collection of files is received, the filenames will be appended with a numerical suffix representing the ordinal position in the list. For example, if we change the 'bar' process definition to:
process bar {
debug true
input:
path 'png_file'
"""
echo png_file*
"""
}
We get:
$ nextflow run main.nf
N E X T F L O W ~ version 22.04.0
Launching `main.nf` [marvelous_bhaskara] DSL2 - revision: 980a2d067f
executor > local (2)
[f8/190e2c] process > foo [100%] 1 of 1 ✔
[71/e53b05] process > bar [100%] 1 of 1 ✔
png_file1 png_file2 png_file3
$ nextflow run main.nf --num_files 1
N E X T F L O W ~ version 22.04.0
Launching `main.nf` [nauseous_brattain] DSL2 - revision: 980a2d067f
executor > local (2)
[ce/2ba1b1] process > foo [100%] 1 of 1 ✔
[a2/7b867e] process > bar [100%] 1 of 1 ✔
png_file
Note that the * and ? wildcards can be used to control the names of the staged files. There is a table in the documentation that describes how the wildcards are to be replaced depending on the cardinality of the collection. For example, if we again change the 'bar' process definition to:
process bar {
debug true
input:
path 'file*.png'
"""
echo file*.png
"""
}
We get:
$ nextflow run main.nf
N E X T F L O W ~ version 22.04.0
Launching `main.nf` [small_poincare] DSL2 - revision: b106710bc6
executor > local (2)
[7c/cf38b8] process > foo [100%] 1 of 1 ✔
[a6/8cb817] process > bar [100%] 1 of 1 ✔
file1.png file2.png file3.png
$ nextflow run main.nf --num_files 1
N E X T F L O W ~ version 22.04.0
Launching `main.nf` [friendly_pasteur] DSL2 - revision: b106710bc6
executor > local (2)
[59/2b235b] process > foo [100%] 1 of 1 ✔
[2f/76e4e2] process > bar [100%] 1 of 1 ✔
file.png

mgcv::gam, Error in names(dat) <- object$term : attribut 'names' [2] as to be same length as vector [1]

I want to run a hieratchical GAM in the mgcv package using the gam function. I used the same form of model in brms without problem and I will eventually re-run the same model in brms, but the deadline for an abstract submission in Sunday so I want to try the model in mgcv to have quicker results.
My formula:
f = MDS1 ~ 1 + exposed + s(YEAR,bs = "tp")+ s(LEVEL, bs = "tp") +
t2(YEAR, SITE, bs = c("tp","re")) + s(INTERTIDAL_TRANSECT, bs = "re",
m = 1)
My data:
Classes ‘data.table’ and 'data.frame': 3992 obs. of 9 variables:
$ unique_id : chr "Babb's Cove-1-0-1988" "Babb's Cove-1-0-1989" "Babb's Cove-1-0-1990" "Babb's Cove-1-0-1992" ...
$ MDS1 : num -0.607 -0.607 -0.607 -0.607 -0.607 ...
$ MDS2 : num 0.19 0.19 0.19 0.19 0.19 ...
$ MDS3 : num 0.36 0.36 0.36 0.36 0.36 ...
$ SITE : chr "Babb's Cove" "Babb's Cove" "Babb's Cove" "Babb's Cove" ...
$ INTERTIDAL_TRANSECT: Factor w/ 21 levels "1","2","5","7",..: 1 1 1 1 1 1 1 1 1 1 ...
$ LEVEL : num 0 0 0 0 0 0 0 1 1 1 ...
$ YEAR : num 1988 1989 1990 1992 1994 ...
$ exposed : Factor w/ 2 levels "1","2": 2 2 2 2 2 2 2 2 2 2 ...
- attr(*, ".internal.selfref")=<externalptr>
- attr(*, "sorted")= chr "unique_id"
I have 2 questions:
a)
When I try to fit the model with fit_count <- gam(f, data = count_merge, method = "REML", family = gaussian()) I get :
Error in names(dat) <- object$term :
attribut 'names' [2] doit être de même longueur que le vecteur [1]
I think it's something with the t2() argument of the formula.
b)
I usually run GAM with brms and my formula for that model was :
MDS1 ~ 1 + exposed + s(YEAR,bs = "tp")+ s(LEVEL, bs = "tp") + t2(YEAR, SITE, bs = c("tp","re"), full = T) +(1|r|INTERTIDAL_TRANSECT),
family = gaussian()
Is my way to adapt the formula to mgcv::gam OK?
Q a)
Your SITE vector is a character vector and it is required to be a factor.
Q b)
That looks OK, but you don't need the m = 1 in the s(INTERTIDAL_TRANSECT, bs = "re") term.
You should also use the full = TRUE option on the t2() term if you want the parameterisation to be the same between your {brms} call the {mgcv} one.

Reverse Integer python

I am trying to solve the Leetcode "Reverse Integer" challenge in python.
I took look at the solution they supplied.
Their answer was written in Java.
I do not know why they use the following test-condition:
if (rev > Integer.MAX_VALUE/10 || (rev == Integer.MAX_VALUE / 10 && pop > 7)) return 0;
if (rev < Integer.MIN_VALUE/10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) return 0;
I wrote my python code without that same test condition.
It failed when I tested a negative number.
class Solution:
def reverse(self, x: int) -> int:
rev = 0
while( x != 0 ):
pop = x % 10
x //= 10
rev = rev * 10 +pop
print(rev)
return rev
I do not understand why that particular test-condition exists in their code.
The goal is to reverse the order of digits in an integer.
Some examples are shown below:
Example 1:
Input: x = 123
Output: 321
Example 2:
Input: x = -123
Output: -321
Example 3:
Input: x = 120
Output: 21
Example 4:
Input: x = 0
Output: 0
class Solution {
public int reverse(int x) {
int rev = 0;
while (x != 0) {
int pop = x % 10;
x /= 10;
if (rev > Integer.MAX_VALUE/10 || (rev == Integer.MAX_VALUE / 10 && pop > 7)) return 0;
if (rev < Integer.MIN_VALUE/10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) return 0;
rev = rev * 10 + pop;
}
return rev;
}
}
Branch conditions are sometimes easier to understand when they are drawn as tree-diagrams instead of one line formulas.
Also, we can write the tree like this:
├── OR
│ ├── (rev > Integer.MAX_VALUE/10)
│ └── AND
│ ├── rev == Integer.MAX_VALUE / 10
│ └── pop > 7
Imagine that rev is a container, like a cookie-jar, or a carboard box.
There is a maximum number of cookies which can be crammed into the cookie jar.
When rev == Integer.MAX_VALUE it means that the cookie jar is full.
The condition rev == Integer.MAX_VALUE/ 10 is represent the idea that the cookie-jar can only fit one more cookie.
Instead of stuffing cookies into cookie jars, we are stuffing digits (such as 5) into an integer.
The integer is the container.
Note that when you append zero to an integer in base 10, it is the same as multiplying by 10. For example, 25 becomes 250
Suppose that the largest integer allowed is 2,147,483,647
Note that 214,748,364 is equal to 2,147,483,647 / 10
When you divide by 10, the decimal part of the number is discarded.
When rev is 214,748,364, then you can append any integer the right-most end, as long as that digit is less than or equal to 7.
Old Element
New Element
New Container
Status
214748364
0
2147483640
VALID
214748364
1
2147483641
VALID
214748364
2
2147483642
VALID
214748364
3
2147483643
VALID
214748364
4
2147483644
VALID
214748364
5
2147483645
VALID
214748364
6
2147483646
VALID
214748364
7
2147483647
VALID
214748364
8
2147483648
TOO BIG
214748364
9
2147483649
TOO BIG
The "status" column in the table above
In their code, when they wrote (pop > 7) it probably should have been (pop > Integer.MAX_VALUE % 10)
Suppose that the largest integer allowed is 2,147,483,647
Then, 2,147,483,647 % 10 is equal to 7.
There are many, many different ways to reverse the digits of a number.
One solution, in python, is shown below:
x = int(reversed(str(x)))

"No instance found" when using seq

I'm puzzled by the fact that Alloy reports No instance found for this model using seq:
one sig Const {
T: seq (seq Int)
}
fact const_facts {
Const.T = {
0 -> {0->1 + 1->9} +
1 -> {0->3 + 1->15}
}
}
run {} for 20 but 6 Int, 8 seq
While the following model, where I simply replaced each seq with Int ->, has an instance as one would expect:
one sig Const {
T: Int -> (Int -> Int)
}
fact const_facts {
Const.T = {
0 -> {0->1 + 1->9} +
1 -> {0->3 + 1->15}
}
}
run {} for 20 but 6 Int
It's especially confusing to me since https://alloytools.org/quickguide/seq.html seems to imply that seq X and Int -> X are the same thing type-wise.
Any thoughts?
The Const.T you create has an arity of 3 while a seq must be an arity of 2.
┌──────────┬──────┐
│this/Const│T │
├──────────┼─┬─┬──┤
│Const⁰ │0│0│1 │
│ │ ├─┼──┤
│ │ │1│9 │
│ ├─┼─┼──┤
│ │1│0│3 │
│ │ ├─┼──┤
│ │ │1│15│
└──────────┴─┴─┴──┘
The predicates and functions for seq assume an arity of 2. I.e. a seq is not like an object, it is a convention for functions and predicates that take an arity 2 tupleset where the first column is an integer.

How to list the code of a verb in J

In the console, typing a single verb without parameters will print its content:
tolower
3 : 0
x=. I. 26 > n=. ((65+i.26){a.) i. t=. ,y
($y) $ ((x{n) { (97+i.26){a.) x}t
)
That's nice for development, but unexploitable during execution. Is there a way to do that dynamically? Is there a verb that can return the contents of another verb?
For example:
showverb 'tolower'
or
showverb tolower
You can use its representation. For example the boxed representation (5!:2) of tolower is:
(5!:2) <'tolower'
┌─┬─┬────────────────────────────────────────┐
│3│:│x=. I. 26 > n=. ((65+i.26){a.) i. t=. ,y│
│ │ │($y) $ ((x{n) { (97+i.26){a.) x}t │
└─┴─┴────────────────────────────────────────┘
its linear (5!:5) is:
(5!:5) <'tolower'
3 : 0
x=. I. 26 > n=. ((65+i.26){a.) i. t=. ,y
($y) $ ((x{n) { (97+i.26){a.) x}t
)

Resources