Git hangs at clone? It could be a Type of Service issue.

Today I was trying to download some code via git, and I got into a strange problem. When I ran git clone, it simply reported cloning into ‘repo’ and was stuck there, until timeout. Same thing for pull. I could not understand what was happening, and it took me a while to figure it out but here is the solution, at least to my problem. Add this to your .ssh/config

Host *
  IPQoS 0x00

What was the problem? Here are the symptoms:

  • git worked on my NAS (Linux, connected to the router via cable), but not from my laptop (OSX, connected via WiFi)
  • ssh worked from the laptop. I could ssh to the NAS and to other external ssh machines.
  • However, I observed that the interactive ssh behaved strange when I ssh’d to an external machine. For example, if I asked for an ls, it kind of got stuck, and I had to press enter every time to get further data. It was like acting with a constant “more”.
  • using GIT_TRACE, I found out that ssh negotiation was completely successful and it got stuck at invoking git-upload-pack via ssh.

Google didn’t help. I changed versions of ssh and git, checked my router for strange setups, but nothing could solve. Until I smelled something about the way packets were exchanged. Don’t ask me how I did it. I don’t know. I just had the feeling that strange “enter to get more stuff” behavior had something to do with it. The fact that the laptop had the problem, while the NAS didn’t, made me think it was either a OS issue or a NAT configuration issue.

So I started checking something about NAT, adding NAT to the google query, and here I found a post of a Dane who had similar troubles, apparently caused by the router messing up due to Quality of Service. This sounded just about right. Quality of Service influences how packets are considered for priority routing. I did more searching until I found this other post, where I found the solution given above.

It was a fun hour.

Posted in Linux, MacOSX. Tags: , . Comments Off »

A raytracer in python – part 6: cameras

In the latest commit for the raytracer, I added cameras. The design changed so that now the responsible for rendering is the camera object. Actual cameras are specializations of an abstract class BaseCamera, which holds common information about positioning. The BaseCamera is then specialized into two concrete classes:

  1. PinholeCamera is a camera where rays are shot as diverging from a single point, called the eye_point. This allows perspective, which was not present previously as the rays were emerging from the ViewPlane pixels.
  2. LensCamera is a camera that simulates depth of field, that is, focus/out of focus. Contrary to the PinholeCamera, where everything is in focus, LensCamera allows different focusing. Objects that happen to be on the “focal plane” are in focus, while objects that are outside (either closer or farther from the camera) present less defined details proper of an out-of-focus object. To perform this effect, we need the random sampling on a disk implemented in the previous post.

The following picture shows how LensCamera performs. A set of hemispheres are deployed along a line. The camera is above them, slightly angled and with a roll angle appreciable from the horizon. In all three cases, the orange central sphere is focused, as the focus plane has been set to fall on the sphere’s position. Note how other objects are in focus for a Pinhole camera (left picture) which has no depth of field by construction, and become more out of focus as the lens size increases (1.0 in the center picture, 5.0 in the right one)


From left to right, PinholeCamera, LensCamera with lens size 1.0, LensCamera with lens size 5.0

Other cameras may technically be possible: the book goes further in deploying fisheye and stereoscopic cameras, but I am not interested in them. I think the pinhole and lens camera are flexible enough for quality renderings and my desire to learn.

One important feature of the Camera system is that it requires the definition of local coordinates on the camera itself. The three vectors defining this set of coordinates, called u, v, w in the book, are obtained by building an orthonormal basis using the cross product between the observation vector (the vector between the “eye” of the camera and the look_at point) and an “up” vector, our default being in the same direction as the y axis. Doing the cross product of these two vectors (observation and up) produces the third remaining vector of the orthogonal basis centered on the camera. However, if the camera looks straight up, or straight down, the cross product is zero and we obtain a singularity, losing one degree of freedom (a condition also known as gimbal lock). The book proposes to detect this condition and treat it accordingly, by either overriding the specification and setting the vectors to an arbitrary, well defined alternative, or by “juggling” the up vector out of alignment so that the third vector is still defined. I decided for the third option, ignore the problem, as I am not going to use gimbal locked configurations for now, but it’s definitely a problem to add to the todo list.

With this post, I take a temporary break from the raytracing business. I may add optical effects such as reflections, refractions, materials, lights, but the point is that the amount of rays that must be propagated for these effects to show tends to be very high. I want to venture into CUDA, and therefore I will switch my attention to CUDA programming from now on, integrate it with the raytracing later on, then go back to light effects at a later stage. I will implement light effects first in python, then use CUDA to achieve the same results. My aim is to have fun, test CUDA/C/Python integration, compare performances, and provide a fully python raytracer with optional C/CUDA high-performance code to achieve the same task. For CUDA tinkering, I will switch back to my old friend, the mandelbrot set.

Posted in Python, Raytracing. Comments Off »

How to convert a QString to unicode object in python 2?

I had this problem to solve, and I tried to find the safest way. This program illustrates the solution

from PyQt4 import QtCore, QtGui                                                                                                                       
import sys                                                                                                                                            

app = QtGui.QApplication(sys.argv)                                                                                                                    
ed = QtGui.QLineEdit()                                                                                                                                

def editingFinished():                                                                                                                                
    # The text() returns a QString, which is unicode-aware                                                                                            
    print type(ed.text())                                                                                                                             
    # This returns a QByteArray, which is the encoded unicode string in the utf-8 encoding.                                                           
    print type(ed.text().toUtf8())                                                                                                                    
    # Since unicode() accepts a sequence of bytes, the safest and fully controlled way of performing                                                  
    # the transformation is to pass the encoded sequence of bytes, and let unicode know it is utf-8 encoded                                           
    print unicode(ed.text().toUtf8(), encoding="UTF-8")                                                                                               

QtCore.QObject.connect(ed, QtCore.SIGNAL("editingFinished()"), editingFinished)                                                                                                                                                                                                           


So the solution is

unicode(qstring_object.toUtf8(), encoding="UTF-8")

Maybe there’s another, simpler way that it’s also safe, but for now this solution is good enough.

Posted in Python. Comments Off »

Fair throw from an unfair coin

Imagine you are given an unfair coin. You don’t know how unfair it is, nor which side (head or tails) is produced more frequently than the other. How do you perform a fair throw with it ?

Enter the von Neumann method. It’s really simple: throw the coin twice. If the results are the same, discard the result; if the results are different, choose the first one.

How does it work ? It’s rather easy. Suppose the coin returns head 90 % of the times, and the remaining 10 % returns tail. We give actual probabilities here for the sake of discussion, but you don’t need to know how unfair is the coin in order to apply this method. If you throw the coin two times

  • the probability of throwing two times head is 0.9 * 0.9 * 100 = 81 %
  • of getting two tails is, of course 0.1 * 0.1 * 100 = 1 %.
  • the probability of throwing tail followed by head is 0.1 * 0.9 * 100 = 9 %
  • and finally, the probability of throwing head followed by tail is 0.9 * 0.1 * 100 = 9 %.

You can see how the probabilities of the mixed events (Tail-Head and Head-Tail) is the same. If you discard homogeneous cases Head-Head and Tail-Tail, accept only the mixed results and pick one of the throws as the right one, you will obtain the same probability of obtaining a head and tail, that is 50/50, a fair coin. The only problem with this method is that you may have to discard a very high number of throws. The method will be slower to yield a result, but the result is guaranteed to be fair. In the limit of a completely unfair coin that returns the same face every time, you will never get a result. I wonder how this can be generalized to an unfair n-faces die.

Posted in Probability. Comments Off »

Random number generator hardware

Suppose you want to generate a large number of random dice rolls for a computer program. How do you do it ? With a robotic dice rolling machine, of course

Why would anyone need such device, I hear you ask? Well, software random number generators are technically not perfectly random, and if you have to reassure a crowd of people complaining about your rolls’ randomness, this solution is straightforward, creative, and a real pleasure to read in its gory and unexpected technical issues.

Posted in Hardware, Probability, Statistics. Comments Off »

Why Gaia?

Choosing the name of my new blog was a problem I worked on for some time. I wanted to stay within my domain,, and I wanted the blog to be mostly about simple living, outdoor activities, beekeeping, and so on, while maintaining a scientific approach to the presentation of these topics. I therefore needed a central theme embracing their broad spectrum.

As humans, we are just the latest gimmick in a big show started billions of years ago. Many species have come and gone during this time, and many more will in the future. We humans have apparently little control on the complexity of our environment, exactly because this complexity emerges from an extremely large set of interactions and feedbacks we have little knowledge or control on. Additionally, these interactions can be extremely non-linear, meaning that they may either resist to changes, or run off wildly for even small perturbations of the dependent factors.

I first heard about the Gaia hypothesis when I was 13, in a PC game called SimEarth. The central idea is that the planet Earth is what can be considered as a self-regulating “superorganism” given by the dependencies among biota (plants, animals, bacteria, fungi, algae) and the inorganic environment in which they live (atmosphere composition, temperature, water level and acidity). This hypothesis, developed mainly by James Lovelock in the 70s, found more and more supporting evidence, and it triggered a better understanding of the complexity of the Earth system. Experiments such as Biosphere 2 made rather clear how a stable, self-sustaining system is hard to establish, and how many subtle interactions are hidden and unexpected. This network of interactions, on a global scale, is what Lovelock calls “Gaia”.

I must admit I read very little of Lovelock, and of what I read, I don’t fully agree with some of his personal opinions, in particular about the importance of computational modeling and the actions that one can take, yet the point of Gaia as a complex system of interactions is well established, factual and verified by experiments both on the field and in computational models.

That said, I decided to explore a very small subsystem of these interactions at a personal level: bees are important pollinators, producers of useful substances such as honey, wax, and propolis, but also allowing plants to reproduce; propolis and wax are used in woodworking, another dreamy interest of mine (dreamy because I lack the tools and the space); growing an extremely tiny but pleasant part of my diet is a rewarding and relaxing activity, where I learned the importance of soil characteristics, the different chemical and physical requirements for different species of plants, techniques such as hydroponics and aquaponics, mushroom growing, and integrated cultivars to take advantage of dependencies among species and improve production. I took great interest in Jared Diamond’s Collapse, and its reporting of Tikopia as a long established self-sustained islet in the pacific.

Keeping bees, growing plants, building gardens, working wood are difficult tasks that require knowledge of biology, mathematics, chemistry and physics, in addition to artistic and practical skills. This blog aims at exploring some of these topics, disentangling a tiny section of the interactions’ snarl for fun and cultural profit.

Posted in Opinion, Personal. Comments Off »

The insane clauses of a university’s contract for a researcher position

Despite having left academia for good, and for all the best reasons, all my circle of friends and professional contacts is mostly in academia. Some time ago a friend forwarded me the following contract for a researcher position in a university of a non-European country.

It should not come as a surprise that, in some countries, what we consider slavery is still a thing, but you would not expect to be a thing in written form. I report here only the paragraphs that are important, removing the boilerplate stuff. I also render the amounts in euros for ease of comparison. I leave the introductory opening, because it makes a nice (read: creepy) contrast. I will put my comments directly following the relevant fragments.

The two parties, in a spirit of valuable consideration and friendly cooperation, agree to sign this Contract and pledge to fulfill conscientiously all the duties and obligations stipulated in it.

Yeah, sure.

Employee’s annual salary will be 44.000 EUR (3683 EUR/mo). Social insurances are included and all kinds of taxes and fees have to be paid by Employee. 90% of the annual salary will be paid as follows: Employee will be paid 1900 EUR monthly and the rest, which is set as the annual research reward, will not be paid until Employee has passed the annual evaluation. And the other 10% of the annual salary will be paid by the end of the next year when Employee has passed the annual evaluation.

Meaning that, depending on the actual cost of living, one might be forced to work at a loss until the annual research award is given (if it’s given). Note, additionally, that these amounts are before taxes, that there’s no pension contribute, nor any entitled vacation.

Employee shall observe the laws, decrees and relevant regulations enacted by the country’s government and shall not interfere in its internal affairs.

Good morning fascism, it’s been a while. The definition of “interfere” is up to free interpretation, of course, especially when scientific research interferes with political decisions. We’ve seen this already, and it didn’t turn out well. I will also add that the research topic is potentially source of political distress.

Employee shall be evaluated and supervised by Employer according to the duties and obligations stipulated in the Contract

The obligations (which I am not showing here) are extremely detailed requirements for number of publications, quality of journals, teaching duties, all with undefined collaborators at discretion of the Employer.

In the annual evaluation of Employee, if Employee publishes one research paper fewer than the required number, EUR 5700 shall be deducted from his/her annual salary during the 1st and 2nd years of the term of this contract and EUR 4300 shall be deducted from his/her annual salary during the 3rd and 5th years of the term of this contract. But all the deducted money will be returned to Employee if he manages to complete all the duties and obligations by the end of the employment period.

Yeah, I am sure anybody would be glad to work like that, under the whims of referees. And not let’s even consider what happens if you get sick, or have any problem preventing you to publish due to either a collaborator’s resignation, a broken instrument, or any other unforeseen circumstance like it always happens in scientific research, in particular of the experimental on-the-field kind (which is the one we have here).

We talked about resignation. Well, that’s another “funny” point. Read this:

VI. Revision, Cancellation and Termination of the Contract
1. Both parties should abide by the Contract and should refrain from revising, canceling, or terminating the Contract without mutual consent.

Meaning you can’t quit for the duration of the contract (5 years).

4. Under the following circumstances, Employer may terminate this Contract in the form of written notice to Employee:
(1) Where Employee does not perform his/her duty or is incompetent to perform his/her duty
and remains to be incompetent after Employer’s warning.
(2) Where Employee has been proven to use fraud and deception information in the applying materials such as health, education, political performance, academic position and other documents.
(3) Employee has serious discipline breach behaviors or involvement in criminal activities.
(4) Employee unauthorized departure from office prior to the end of his term without consent.

Except for some reasonable points, point 4 restates that you can’t quit. Unless there’s mutual consent, you are not authorized to depart from your office before the end. It’s debatable if, in this case, the word “office” is meant as “work room” or “duty”, but considering what comes next…

VII. Breach Penalty
1. Employee shall pay liquidated damages to Employer if the Contract is terminated due to the occurrence of circumstances described above. The liquidated damages amount for 3 times of the monthly wages Employee has received.
2. Employee shall notify Employer three months in advance if Employee asks to cancel the Contract before the term of this Contract expires. After Employer’s consent, Employee shall pay liquidated damages to Employer and the liquidated damages amount for 3 times of the monthly wages Employer has received.

So if you are fired, or if you quit (assuming they let you go through mutual consent) you have to repay three times what you got until then.

What. The. Hell!

Needless to say, the offered position was refused. I can’t imagine anybody sane in his mind accepting it.

Posted in Academia. Comments Off »

What happened to ForTheScience?

There have been quite some changes in the past 24 hours, and a few more will follow:

  • I changed the theme to Garland. I kept the green theme because I like it and it’s the traditional color of the chemistry degree in my native city. The theme is not perfect, and I am tweaking it as I write. I will also have to check past posts to see if they behave correctly.
  • As a passionate (but inexperienced) beekeeper and gardener, I decided to start a new blog at It will mainly focus on my personal adventures in outdoor and non-computer-related activities, such as gardening, beekeeping, and cooking (Italian cooking, specifically), with a scientific eye and strong accent on discovery and experimentation that can be useful to others. I decided for the split because I expect the audience to be different: I already have plenty of movies and pictures to post, but I felt this blog as not appropriate for this content.
  • I removed the failed experiment of, which I started during a relatively well-received, but not financed application for a Marie Curie grant I applied for. After the grant was declined, I kept the domain trying to reinvent its purpose, but I found myself not particularly driven to do any more work on it. For a while, I helped a friend and hosted the p4vasp code, and received good traffic, but after the code was moved to a dedicated site, the traffic declined to spambots and I decided to terminate it for good. The URL now redirects to this blog.
  • I will soon remove WPML translations. The plugin has gone commercial, and I am not posting a lot in Italian to justify the transition. I don’t know how the current Italian posts will be handled after the removal, but my guess is that they will collapse into the current blog timeline. I might still post in Italian and provide translation posts, but they will appear directly in the current progression. I doubt this will be excessively annoying for current readers, as I normally post translations when the English post is no longer on the main page or the RSS scope.
  • I removed the redundant twitter lateral pane, leaving more screen for code and scripts that occasionally ended up being hard to read or to copy/paste. I will keep posting updates on twitter every time there’s a new post (it’s automatic)
Posted in Administrative, Personal. Comments Off »

Git stash size in the command line prompt

Too often I get confused with git when it comes to stashes. I tend to stash often, as I jump from a task to another or from a branch to another, but it already happened  I forgot I stashed something. The stash grows and I don’t remember what each patch contains. Fortunately, I never really end up doing duplicate work, but this is doomed to happen if I don’t take appropriate measures.

I concocted this function and bash prompt to present (in proper color no less) the current amount of stashed items. This way I always know if I have stashes around

function git_stash_size {
 lines=$(git stash list -n 100 2> /dev/null) || return
 if [ "${#lines}" -gt 0 ]
   count=$(echo "$lines" | wc -l | sed 's/^[ \t]*//') # strip tabs
   echo " ["${count#} "stash] "
# Comment in the above and uncomment this below for a color prompt
$(__git_ps1 " (%s)")\[\033[00m\]\[\033[01;31m\]$(git_stash_size)\[\033[00m\]\$ '

Reformat properly as a single line.

Posted in git. Comments Off »

Can’t add widgets in Qt Designer? Don’t use Unity

I got around a problem in Qt designer that didn’t allow me to drop any widget. When I tried, I obtained the barred circle mouse pointer, instead of the plus. When dropped, the drop operation did not complete, and the receiving widget remained empty. Apparently, it’s due to some form of collision between designer and Unity (the graphic environment on Ubuntu). I switched to Gnome and now designer works. Note that I got it to work occasionally on Unity as well, but I was never able to reproduce the exact conditions that allowed it.


Posted in Qt, Ubuntu. Comments Off »