How do I determine human-readable file size in Julia? - julia

I would like to display to the user a human-readable file size in Julia.
Python, for instance, has humanize.naturalsize():
>>> humanize.naturalsize(1000000)
'1.0 MB'
>>> humanize.naturalsize(1000000, binary=True)
'976.6 KiB'
How can I achieve this in Julia?

I don't believe there is a package to do this, but the Python code is very short and should translate nicely to Julia.
EDIT: In fact, I decided to make a package out of it: Humanize.jl, which you can install with Pkg.add("Humanize")

Related

Jupyter/IPython: how to get results in traditional mathematical notation?

I have been using WxMaxima for my symbolic calculations for a while now. The good thing about WxMaxima is that you can get formatted outputs right in the program and then export them to LaTeX format with a click of the mouse.
Now I want to try the Jupyter/Ipython plus sympy for multiple reasons. I know how to use display(Math(r' some LaTeX math here ')) but what I want is to have the result/output of a cell in a nice mathematical form; something like the TraditionalForm[] command in Mathematica.
I would appreciate if you could help me know if/how I can get that right in a Jupyter notebook?
I think I found the proper solution and it is a sympy feature rather than Jupyter/IPython one. As explained here:
If all you want is the best pretty printing, use the init_printing() function. This will automatically enable the best printer available in your environment.
and
In the [Jupyter/]IPython notebook, it will use MathJax to render LATEX.
Then one can right click on the output and select Show Math As > Tex commands:
to get the LaTeX output.
P.S. A more proper formatting can be achieved via galgebra library. I will look into that and add it here later.

How to load a pickle file within Julia

I have a pickle file that is written by using python (By using cPickle). I need to use it inside julia. I guess I can use PyCall for that purpose.Here is what I've done so far:
julia> using PyCall
julia> #pyimport cPickle as pickle
julia> f=open("mypicklefile.picle","r")
julia> PyTextIO(f)[:seek](0) #if I don't do this, I got error.
julia> trn,tst= pickle.load(PyTextIO(f))
Here,I have two questions. As far as I know, unlike Julia, python(numpy) is rowmajor. So, what should I do to read the content of file in a correct way? I guess somehow I have to tell the PyCall it is written in rowmajor order.
My second question is about performance. The pickle file is about 4.5GB. When I read it inside python, it is completed in a minute whereas in julia it takes at least 15 minutes. Am I doing something wrong ? Since the file is not created by me, I cannot use another format such as hdf5 or jld.

Jupyter: how to make simple illustrations

I am learning to use Jupyter/IPython Notebook as an electronic notebook. Sometimes I need simple illustrations to go along with my calculations, e.g. arrows to represent vector quantities. That's the kind of illustration for which TikZ would be used if we were in Latex. Having tried the TikZ magic extension and failed, I wonder if there's a more native (Python) way to do this. I don't see Matplotlib as the right tool for this sort of thing (correct me if I'm wrong).
If you think TikZ magic is indeed the way to go and I should try to get it to work, then do say so. Thanks.
TikZ (prefered solution)
If you're already familiar with TikZ the respective magic is probably the best option. To use it, simply follow the installation instruction in this repo (pip install git+git://github.com/mkrphys/ipython-tikzmagic.git) and load the extension as shown in on the githib page with %load_ext tikzmagic.
I just tried with IPython 3.1 and it works fine. Of course you have to have pdflatex available.
Matplotlib
If you want to draw simple arrows matplotlib can be used as well and is, of course, more pythonic than TikZ. A really simple example based on this example could look like
import matplotlib.pyplot as plt
%matplotlib inline
plt.axis('off')
plt.arrow(0, 0, 0.5, 0.5, head_width=0.05, head_length=0.1, fc='k', ec='k');
For more technical plots with lots of arrows and dimensions, I totally agree with you that matplotlib is not be preferred.
Other alternatives
There is also an asymptote magic found here. I haven't tried this yet, though.
Finally, you could use svgs either written in the notebook (hints see this question, or using Inkscape or similar and embed the resulting svg-file via the from IPython.display import SVG.

Speeding up matlab file import

I am trying to load a matlab file with the R.matlab package. The problem is that it keeps loading indefinitely (e.g. table <- readMat("~/desktop/hg18_with_miR_20080407.mat"). I have a genome file from the Broad Institute (hg18_with_miR_20080407.mat).
You can find it at:
http://genepattern.broadinstitute.org/ftp/distribution/genepattern/dev_archive/GISTIC/broad.mit.edu:cancer.software.genepattern.module.analysis/00125/1.1/
I was wondering: has anyone tried the package and have similar issues?
(hopefully helpful, but not really an answer, though there was too much formatting for a comment)
I think you may need to get a friend with matlab access to save the file into a more reasonable format or use python for data processing. It "hangs" for me as well (OS X 10.9, R 3.1.1). The following works in python:
import scipy.io
mat = scipy.io.loadmat("hg18_with_miR_20080407.mat")
(you can see and work with the rg and cyto' crufty numpy arrays, but they can't be converted to JSON withjson.dumpsand evenjsonpickle.encodecoughs up a lung-full of errors (i.e. you won't be able to userPython` to get access to the object which was the original workaround I was looking for), so no serialization to a file either (and, I have to believe the resultant JSON would have been ugly to deal with).
Your options are to:
get a friend to convert it (as suggested previous)
make CSV files out of the numpy arrays in python
use matlab

plotting data in R from matlab

I was wondering if it is possible to work between matlab and R to plot some data. I have a script in matlab which generates a text file. From this I was wondering if it was possible to open R from within matlab and plot the data from this text file and then return to matlab.
For example, if I save a text file called test.txt, in the path 'E:\', and then define the path of R which in my case will be:
pathR = 'C:\Program Files\R\R-2.14.1\bin\R';
Is it possible to run a script already written in R saved under test1.R (saved in the same directory as test.txt) in R from matlab?
If you're working with Windows (from the path it looks like you are), you can use the MATLAB R-link from the File Exchange to pass data from Matlab to R, execute commands there, and retrieve output.
I don't use R so this is not something I have done but I see no reason why you shouldn't be able to use the system function to call R from a Matlab session. Look in the product documentation under the section Run External Commands, Scripts, and Programs for this and related approaches.
There are some platform-specific peculiarities to be aware of and you may have to wrestle a little with what is returned (though since you are planning to have R create a plot which is likely to be a side-effect rather than what is returned you may not). As ever this is covered quite well in the product's documentation
After using R(D)COM and Matlab R-link for a while, I do not recommend it. The COM interface has trouble parsing many commands and it is difficult to debug the code. I recommend using a system command from Matlab as described in the R Wiki. This also avoids having to install RAndFriends.

Resources