Julia FITSio: FITS table with Float64 and ASCIIString - julia

I am new to Julia, I hope my question is not too trivial.
I try to create a FITS binary table that includes various columns of Float64 and one column of ASCIIString. As explained in the FITSIO.jl documentation, the input to the write() function should be "a dictionary with ASCIIString keys (giving the column names) and Array values (giving data to write to each column)".
but it seems that a Dictionary cannot hold mixed types, and I get the following error:
data=Dict{"col1"=>[1.0,2.0,3.0], "col2"=>[4.0,5.0,6.0],"col3"=>["toto","tata","titi"]}
LoadError: TypeError: Dict: in parameter, expected Type{T}, got Pair{ASCIIString,Array{Float64,1}} while loading In[408], in expression starting on line 1
Does anyone knows how to create a FITS table including columns of mixed types, and in particular Float64 and ASCIIString?
It should be possible, since I can read such a table with the same FITSIO.jl library without problem, but the limited examples in the documentation do not illsutrate such a case.
Thank you!

Change the braces to parentheses and you'll create the list you intend.
data=Dict("col1"=>[1.,2.,3.], "col2"=>[4.,5.,6.], "col3"=>["toto","tata","titi"])
You are essentially calling the constructor of the Dict type using a sequence of pairs.
Extra info:
Braces are something else entirely. It's for specifying that the dictionary keys and values should be of (or converted to, if possible) a specific type. e.g.
julia> Dict{String,Array{Float64,1}}("a"=>[1.,2.,3.], "b"=>[4.,5.,6.])
Dict{String,Array{Float64,1}} with 2 entries:
"b" => [4.0,5.0,6.0]
"a" => [1.0,2.0,3.0]
julia> Dict{String,Array{Float64,1}}("a"=>[1.,2.,3.], "b"=>['a','b','c'])
Dict{String,Array{Float64,1}} with 2 entries:
"b" => [97.0,98.0,99.0]
"a" => [1.0,2.0,3.0]
julia> Dict{String,Array{Float64,1}}("a"=>[1.,2.,3.], "b"=>["a","b","c"])
ERROR: MethodError: Cannot `convert` an object of type String to an object of type Float64

Related

Find key that matches value in zsh associative array?

In a regular array, I can use (i) or (I) to search for the index of entries matching a given value (first match from the start or end of the array, respectively):
list=(foo bar baz)
echo $list[(i)bar]
# => 2
This doesn't work for associative arrays, to get (one of) the key(s) where a value is found:
declare -A hash=([foo]=bar [baz]=zoo)
echo $hash[(i)bar]
# => no output
Is there another mechanism for doing this, other than manually looping through?
The (r) subscript flag combined with the (k) parameter flag should give you
what you want:
declare -A hash=([foo]=bar [baz]=zoo)
echo ${(k)hash[(r)bar]}
# => foo
The man page section on the (r) subscript flag only talks about returning
values and ignores this usage, so it's hard to find.
Here is something completely disgusting:
% declare -A hash=([foo]=bar [baz]=zoo)
% echo ${${(kA)hash}[${${(A)hash[#]}[(i)bar]}]}
foo
Basically, it consists of two parts:
${${(A)hash[#]}[(i)bar]}, which computes the index of bar in an anonymous array consisting of the values of the associative array.
${${(kA)hash}[...]}, which indexes the anonymous array consisting of the keys of the associative array using the numerical index computed by the previous expansion.
I'm not aware of a short equivalent to the I flag, and I too am surprised that the seemingly obvious extension to associative arrays doesn't exist.

Fill column in Julia

I am trying to fill a column in Julia with values from another matrix. In R, it would look like this:
for(id in 1:y){
countries[id,1] <- x[id, countries1[id]]
}
However, when I try to convert the left side of the equal sign to Julia like so:
countries[:1]
I get an error which says:
"ERROR: MethodError: Cannot `convert` an object of type Int64 to an
object of type Array{Int64,2}
This may have arisen from a call to the constructor Array{Int64,2} .
(...), since type constructors fall back to convert methods."
I don't think my Julia conversion in correct to start with since I am leaving off id. How can I convert the r code to Julia effectively?
It is not clear how the OP intends to convert the stated R code to Julia code. However, given that it involves countries[:1], we can make an educated guess about the error:
countries[:1] should be countries[:, 1].
countries[:, 1] returns the first column in the matrix countries.
countries[:1] resolves to countries[1] and returns the integer countries[1,1]. This is because a leading colon :<name> tells Julia to treat <name> as a symbol. At some point that symbol is parsed, returning 1.
The latter point explains the error message:
ERROR: MethodError: Cannot convert an object of type Int64 to an
object of type Array{Int64,2}
The OP expected countries[:1] to return an array (Array{Int64,2}) , when in fact, it returns an integer (Int64).

Julia: How to delte a column name starts with number in data frame

I have data frame which a column name starts with number, I want to delete the column with the following code, but there is error:
delete!(features, [:3SsnPorchH])
UndefVarError: SsnPorchH not defined
Your problem is that :3SsnPorchH is not correctly parsed as a symbol, but as follows:
julia> :(:3SsnPorchH)
:($(QuoteNode(3)) * SsnPorchH)
When a symbol cannot be correctly parsed, it most often works to put the "name" into parentheses:
julia> :(3SsnPorchH)
:(3SsnPorchH)
Another thing you could do is using the Symbol constructor directly:
julia> Symbol("3SsnPorchH")
Symbol("3SsnPorchH")
(But I'm not sure if that's a good idea -- maybe you lose interning then.)
That being said, it's probably a good idea to give columns a name which is a valid Julia identifier. This gives you construction using DataFrame with keyword arguments, and allows for certain macros to identify variables with columns. You'll just have an easier time.

How to convert any type into String in Julia

Using Julia, I'd like to reliably convert any type into type String. There seems to be two ways to do the conversion in v0.5, either the string function or String constructor. The problem is that you need to choose the right one depending upon the input type.
For example, typeof(string(1)) evaluates to String, but String(1) throws an error. On the other hand, typeof(string(SubString{String}("a"))) evaluates to Substring{String}, which is not a subtype of String. We instead need to do String(SubString{String}("a")).
So it seems the only reliable way to convert any input x to type String is via the construct:
String(string(x))
which feels a bit cumbersome.
Am I missing something here?
You should rarely need to explicitly convert to String. Note that even if your type definitions have String fields, or if your arrays have concrete element type String, you can still rely on implicit conversion.
For instance, here are examples of implicit conversion:
type TestType
field::String
end
obj = TestType(split("x y")[1]) # construct TestType with a SubString
obj.field # the String "x"
obj.field = SubString("Hello", 1, 3) # assign a SubString
obj.field # the String "Hel"

The arcane formals(function(x){})$x

What is the object formals(function(x){})$x?
It's found in the formals of a function, bound to arguments without default value.
Is there any other way to refer to this strange object? Does it have some role other than representing an empty function argument?
Here are some of its properties that can be checked in the console:
> is(formals(function(x){})$x)
[1] "name" "language" "refObject"
> formals(function(x){})$x
> as.character(formals(function(x){})$x)
[1] ""
EDIT: Here are some other ways to get this object:
alist(,)[[1]]
bquote()
quote(expr=)
Background: What is formals(function(x) {})?
Well, to start with (and as documented in ?formals) , formals(function(x) {}) returns a pairlist:
is(formals(function(x){}))
# [1] "pairlist"
Unlike list objects, pairlist objects can have named elements that contain no value -- a very nice thing when constructing a function that has a possibly optional formal argument. From ?pairlist:
tagged arguments with no value are allowed whereas ‘list’ simply ignores them.
To see the difference, compare alist(), which creates pairlists, with list() which constructs 'plain old' lists:
list(x=, y=2)
# Error in list(x = , y = 2) : argument 1 is empty
alist(x=, y=2)
# $x
#
# $y
# [1] 2
Your question: What is formals(function(x) {})$x?
Now to your question about what formals(function(x) {})$x is. My understanding is in some sense its real value is the "empty symbol". You can't, however, get at it from within R because the "empty symbol" is an object that R's developers -- very much by design -- try to entirely hide from R users. (For an interesting discussion of the empty symbol, and why it's kept hidden, see the thread starting here).
When one tries to get at it by indexing an empty-valued element of a pairlist, R's developers foil the attempt by having R return the name of the element instead of its verbotten-for-public-viewing value. (This is, of course, the name object shown in your question).
It's a name or symbol, see ?name, e.g.:
is(as.name('a'))
#[1] "name" "language" "refObject"
The only difference from your example is that you can't use as.name to create an empty one.

Resources