healpy equivalent of alm2fits - healpy

I am wondering what is the equivalent of alm2fits in healpy.
Let's say I have 3 alms for T,E, and B in healpy, how do I export them so that they can be read by a fortran code trough fits2alm?
hp.fitsfunc.write_alm('alms.fits',[alm_T,alm_E,alm_B])
seems not to like a list and
hp.fitsfunc.mwrfits(filename= 'alm.fits',data=[alm_T,alm_E,alm_B],colnames=['T','E','B'],keys=None)
seems to save them in the wrong order, so that fits2alm raises a
'Inconsistent l^2+l+m+1 -> l,m mapping'
error
Thanks
A.

Related

gitpython: How to show diffs between blobs in a human readable format, as in gitk?

I must be missing something really basic. Given this script:
import git
repo = git.Repo(r'C:/leo.repo/leo-editor')
diff_index = repo.head.commit.diff('HEAD~1')
for d in diff_index:
print('%s %9s %9s %s' % (
d.change_type, id(d.a_blob), id(d.b_blob), d.a_path))
I get something like this:
M 173600704 173600080 leo/core/commit_timestamp.json
M 173600368 173599408 leo/core/leoTest.py
M 173600272 173598928 leo/test/unitTest.leo
So far, so good. This is compatible with what gitk shows, that is, modifications to the three files shown.
But now, having access to the a_blob and b_blob objects for each file, how do I get a human-readable diff of the differences between those two blobs? In other words, I want to recreate what gitk shows.
I don't see anything in the docs related to this.
Edward
My question is a variant of this stack-overflow question.
given a blob, blob.data_stream.read() returns its raw contents, that is a <str> object on Python 2, and a <bytes> object on Python 3.
Rather than reading the feeble api docs for Objects.Blob, one would be better off reading the source code. Indeed, Objects.Blob is a subclass of base.IndexObject, which in turn inherits the data_stream property from base.Object (not to be confused with object).

why does sorting a table in Lua doesn't work

I have a piece of Lua code that generate an error and I don't understand how to solve it.
.............................
local last_num = 0
local channelTable={}
for num in channels.each_number() do -- channels.each_number() returns 1.number in each call
channelTable[last_num] =num;
last_num = last_num +1;
end
table.sort(channelTable);
based on lua documentation I can use the function sort to sort the saved numbers in channelTable. the error that I get is:
attempt to index global 'table'
Any idea how can I solve this, or should implement bubble sort?
thanks for any hint!
Either you haven't loaded the table library or you have overwritten it by accident.
The error message seems truncated: it should say why indexing failed.
The error you are seeing indicates that the table library is not available. It's unlikely that this core library isn't part of your Lua environment, so it's likely you have assigned something to table elsewhere in your code.
I think the issue may be that you are expecting channels.each_number() to be called in each iteration of the loop. If I'm not mistaken, I think it only gets called the first time the program goes through the loop. Whatever you use in thefor..in loop needs to be a table, I believe. So I guess the problem is that your table isn't being generated as you want it to. Try doing this:
print('number of items in channelTable = ' .. #channelTable)
If it comes out to 0, then what I said is probably the problem.

groupingBy operation in Java-8

I'm trying to re-write famous example of Spark's text classification (http://chimpler.wordpress.com/2014/06/11/classifiying-documents-using-naive-bayes-on-apache-spark-mllib/) on Java 8.
I have a problem - in this code I'm making some data preparations for getting idfs of all words in all files:
termDocsRdd.collect().stream().flatMap(doc -> doc.getTerms().stream()
.map(term -> new ImmutableMap.Builder<String, String>()
.put(doc.getName(),term)
.build())).distinct()
And I'm stuck on the groupBy operation. (I need to group this by term, so each term must be a key and the value must be a sequence of documents).
In Scala this operation looks very simple - .groupBy(_._2).
But how can I do this in Java?
I tried to write something like:
.groupingBy(term -> term, mapping((Document) d -> d.getDocNameContainsTerm(term), toList()));
but it's incorrect...
Somebody knows how to write it in Java?
Thank You very much.
If I understand you correctly, you want to do something like this:
(import static java.util.stream.Collectors.*;)
Map<Term, Set<Document>> collect = termDocsRdd.collect().stream().flatMap(
doc -> doc.getTerms().stream().map(term -> new AbstractMap.SimpleEntry<>(doc, term)))
.collect(groupingBy(Map.Entry::getValue, mapping(Map.Entry::getKey, toSet())));
The use of Map.Entry/ AbstractMap.SimpleEntry is due to the absence of a standard Pair<K,V> class in Java-8. Map.Entry implementations can fulfill this role but at the cost of having unintuitive and verbose type and method names (regarding the task of serving as Pair implementation).
If you are using the current Eclipse version (I tested with LunaSR1 20140925) with its limited type inference, you have to help the compiler a little bit:
Map<Term, Set<Document>> collect = termDocsRdd.collect().stream().flatMap(
doc -> doc.getTerms().stream().<Map.Entry<Document,Term>>map(term -> new AbstractMap.SimpleEntry<>(doc, term)))
.collect(groupingBy(Map.Entry::getValue, mapping(Map.Entry::getKey, toSet())));

Julia doing something strange with assignments

I am trying to learn Julia by repeating some of the easy ProjectEuler problems in Julia. Everything has been really smooth so far, up until I encountered this frustrating problem. I spent some time debugging my code, and here's what I found:
(Hopefully I'm not missing something really stupid here)
function is_abundant(n::Int) #just a function
return prod(map(x->int((x[1]^(x[2]+1)-1)/(x[1]-1)),factor(n))) > 2 * n
end
abundants=[12] #there should be a better way to initialize an Array
for i=13:28120
if is_abundant(i)
push!(abundants,i)
end
end
le=abundants; #The following lines are the problems
ri=abundants;
d=length(abundants)
println(d)
pop!(le)
shift!(ri)
println(le==ri, " ", endof(ri), " ", endof(abundants))
The output I get is:
6964
true 6962 6962
which means that Julia has changed all three sets of le , ri and abundants with each of pop! and shift! commands. I was able to work around this bug/problem by using a dumb extra identity mapping:
le=map(x->x,abundants)
ri=map(x->x,abundants)
Now the output would change to what I initially expected:
6964
false 6963 6964
My question is, if this is not a bug, why is Julia keeping an equivalence relation between le , ri and abundants sets in the first place? Also, can anyone reproduce this behaviour? I am using Julia "Version 0.3.0-rc3+14 (2014-08-13 16:01 UTC)" on Ubuntu 14.04.
le and ri both point to the same list that abundants points to, so this is expected behavior - they are all operating on the same memory. This part of the manual might help you understand. Or possibly the MATLAB differences section, as it is different in MATLAB (but most other languages are like Julia).
For
abundants=[12] #there should be a better way to initialize an Array
how about
abundants = {} # Vector of anything
or
abundants = Int[] # Vector of ints
and instead of your map(x->x,...), you can just use copy.

Erlang on Windows List Comprehension multiply gives "\f"

Hi i have a weird problem with Erlang on Windows i am running 16B and WinXP.
I have the following code
-module(test).
-export([cost/1,total/1]).
cost(orange) ->
5;
cost(apple) ->
6.
total(L) ->
[cost(I) * Q || {I,Q} <- L].
I run it with
test:total([{orange,2}]).
and it gives me "\f"
changing cost(I) * Q to use -,+ or divide gives me a number.
I have no idea why multiply dosen't work in list comprehension. Running
[test:cost(I) * Q || {I,Q} <- [{orange,2}]]
in an erlang console and emacs mode also dosen't work but
test:cost(orange) * 2
does give me a number.
Any ideas why?
Note your cost/1 function returns a number. But total/1 returns a list (of numbers).
The results on that list are ok, this is just how erlang happens to display lists of small integers. See http://www.erlang.org/faq/problems.html 9.3
to see what I mean, try with larger numbers
test:total([{orange,2000}]).
Again, this is just a display issue, the value in the lists are what you expect. Try it:
[Value] = test:total([{orange,2}]).
Value.
A string is a list of integers. The value you're returning is a list of integers.
Erlang uses a simple heuristic for when to show something as a string, or as a list of integers: is it a flat list containing only numbers in the range {55,250}. (I made those numbers up, but it's something like that. If there are control characters or low characters, it bails.)
Since Erlang doesn't do this to tuples, tuples make it easy to see.
1> {72,101,108,108,111,44,32,83,116,101,112,104,101,110,46}.
{72,101,108,108,111,44,32,83,116,101,112,104,101,110,46}
2> [72,101,108,108,111,44,32,83,116,101,112,104,101,110,46].
"Hello, Stephen."
Erlang is just guessing wrongly what's inside the list.
HTH.

Resources