Friday, April 10, 2015

Dropping pages from a pdf

When I got a journal article through Illiad, there was an annoying first page about copyright law. After futzing around with Preview for a while, I downloaded a command-line tool:

sudo port install pdftk

My foo.pdf was 10 pages, so to get pages 2-10 I just did

pdftk foo.pdf cat 2-10 output out.pdf

Bonus: I wrote a script that reads the output from the dump_data command. One of those lines is NumberOfPages, so I parsed that number and put that into the cat command so I wouldn't have to look it up myself.

Tuesday, March 31, 2015

Using git with Word

I like git, and I like to use it with most of my projects. I use LaTeX for documents, but sometimes I need to use Word. (Apparently ISME J does not accept submissions in TeX. It only accepts Word, rtf, and, unbelievably, plain txt.)

Word uses a binary format, so git diff normally displays nothing useful. Martin Fenner posted an incredibly useful blog post that solves this whole problem for me.

I'm on OSX. First, I got pandoc, a tool that turns Word files into simpler text formats that can be more easily piped into diff.

Second, I needed to add some lines to the .gitconfig file in my home directory:

[diff "pandoc"]
  textconv=pandoc --to=markdown
  prompt = false
[alias]
  wdiff = diff --word-diff=color --unified=1

Third, in the git repo, I needed to add a new file .gitattributes with contents

*.docx diff=pandoc

Et voila! git diff works just like I would hope.

Tuesday, November 25, 2014

installing ruby without sudo

First off, one should should a version control for ruby. I only know rvm.

The first step is the hair-raising curl | bash combination they list on their website. This should set up rvm. It’s nice and made for non-sudoers; it installs in a local directory by default. If you get any warnings, probably check up on those.

I wanted to use ruby 2.0.0 because that’s what I have on my personal machine. Running the recommended rvm install 2.0 led to requests for a password, which are due to rvm’s desire to install some dependencies as necessary. I crossed my fingers and tried to do without these libraries: rvm install 2.0.0 --autolibs=0.

rvm automatically gives you gem, so I was happy to be able to install my favorite gem: gem install arginine. Worked like a charm.

Wednesday, July 2, 2014

What queues exist on my PBS server?

You can get information on what queues exist by using qstat -Q, but that doesn't give you information about the walltime and total possible number of jobs. You can get all that information from the mysterious qmgr -c 'p s'.

Thursday, May 22, 2014

why i stopped using perl

I wanted to have a double loop over key-value pairs in a hash. I was doing a sensitivity analysis, so I had a hash that linked parameter names to their values. I wanted to loop over the values in that hash, modifying the values one by one. In each loop, I would copy the original hash, change one value, then run the simulation.

So if I had parameters a, b, and c, each with initial value 1.0, I would want to run simulations where a was changed but b and c left alone, then b changed but a and c left alone, and then c changed.

If you do this the naive way in python, you would get screwed:

d = {'a': 1, 'b': 1, 'c': 1}
for key, value in d.items():
    new_d = d
    new_d[key] = value*1.01
    print new_d

yields

{'a': 1.01, 'c': 1, 'b': 1}
{'a': 1.01, 'c': 1.01, 'b': 1}
{'a': 1.01, 'c': 1.01, 'b': 1.01}

because new_d is never a new object, it just points to the original dictionary d. I verified this by printing id(d) and id(new_d):

140433372636000
140433372636000

Changing that line to new_d = dict(d) suitably rescues the result:

{'a': 1.01, 'c': 1, 'b': 1}
{'a': 1, 'c': 1.01, 'b': 1}
{'a': 1, 'c': 1, 'b': 1.01}

Doing the (I thought) sensible thing in perl leads to bizarre results:

use 5.10.1;
my %hash = ("a" => 1, "b" => 1, "c" => 1);

while (my ($key, $value) = each %hash) {
    my %new_hash = %hash;
    $new_hash{$key} = value*1.01;
    print "$_: $new_hash{$_}, " for (keys %new_hash);
    print "\n";
}

causes in infinite loop:

c: 1.01, a: 1, b: 1, 
c: 1.01, a: 1, b: 1, 
c: 1.01, a: 1, b: 1, 
c: 1.01, a: 1, b: 1, 
...

I thought I had made the python mistake, so I printed \%hash and \%hash_new, but I saw different memory addresses:

HASH(0x7fc56c032418)
HASH(0x7fc56c037d80)

It turns out that in perl, every hash has exactly one internal iterator. In the first step of the while-loop, that iterator points to c. When I copy the hash, the iterator goes back to the beginning and ends up pointing to c again in the next iteration of the loop.

You can rescue this in a bunch of ways, one of which is to just loop over keys:

for my $key (keys %hash) {

but I was so disgusted by this bizarre and crazy behavior that I just stopped using perl.

Tuesday, May 20, 2014

Pushing to a github repo from a firewalled cluster

I cloned my repo from github, but then when I tried to push, I got this complaint:

$ git push
error: The requested URL returned error: 403 while accessing https://github.com/user/repo.git/info/refs

I fixed this by issuing

$ git remote set-url origin https://user@github.com/user/repo.git

Wednesday, April 23, 2014

Cisco AnyConnect reconnection on Mac

My Cisco AnyConnect thingy would complain whenever I lost an internet connection. I enabled reconnection by looking in /opt/cisco/anyconnect.

First I made a backup of AnyConnectLocalPolicy.xsd.

Then, inside that file, I added the following block inside the <xs: all minOccurs="0"> tag (line 5 for me, above the tag name="FipsMode"):

<AutoReconnect UserControllable="true">true
  <AutoReconnectBehavior UserControllable="true">ReconnectAfterResume</AutoReconnectBehavior>
</AutoReconnect>

C'est tout.