Python code for the inversion