Difficulty piping with qhull through python

334 views Asked by At

I'm having trouble piping command through QHull in python. I'm currently trying to do so like this:

input_command = "rbox c " + str(qpoints) + " | qconvex FQ FV n"
command = subprocess.Popen(input_command.split(" "), stdout=subprocess.PIPE)
print command.communicate()[0]

Here, qpoints is formatted so that input_command winds up as:

rbox c P0,0,0 P0,0,2 P0,2,0 P0,2,2 P2,0,0 P2,0,2 P2,2,0 P2,2,2 | qconvex FQ FV n

Unfortunately though, this just prints out the usage of qconvex:

qconvex- compute the convex hull.  Qhull 2012.1 2012/02/18
    input (stdin): dimension, number of points, point coordinates
    comments start with a non-numeric character

options (qconvex.htm):
    Qt   - triangulated output
    QJ   - joggled input instead of merged facets
    Tv   - verify result: structure, convexity, and point inclusion
    .    - concise list of all options
    -    - one-line description of all options

output options (subset):
    s    - summary of results (default)
    i    - vertices incident to each facet
    n    - normals with offsets
    p    - vertex coordinates (includes coplanar points if 'Qc')
    Fx   - extreme points (convex hull vertices)
    FA   - report total area and volume
    FS   - compute total area and volume
    o    - OFF format (dim, n, points, facets)
    G    - Geomview output (2-d, 3-d, and 4-d)
    m    - Mathematica output (2-d and 3-d)
    QVn  - print facets that include point n, -n if not
    TO file- output results to file, may be enclosed in single quotes

examples:
    rbox c D2 | qconvex s n                    rbox c D2 | qconvex i
    rbox c D2 | qconvex o                      rbox 1000 s | qconvex s Tv FA
    rbox c d D2 | qconvex s Qc Fx              rbox y 1000 W0 | qconvex s n
    rbox y 1000 W0 | qconvex s QJ              rbox d G1 D12 | qconvex QR0 FA Pp
    rbox c D7 | qconvex FA TF1000

I have read online some examples of extra steps that have to be taken when including piping in python calls. But I can't get any examples of them to work, and there's been almost no explanation as to what's going on. Can someone explain to me a code snippet here that will work and why it works?

I have also tried reading in the result of one function from file. For instance, I have tried reading the result of rbox from file:

python code:

input_command =  "qconvex FQ FV n < rbox.txt"
    command = subprocess.Popen(input_command.split(" "), shell=True)
    result = command.communicate()
    return result

data:

3 rbox c P1,1,1 P1,1,3 P1,3,1 P1,3,3 P3,1,1 P3,1,3 P3,3,1 P3,3,3
16
     1      1      1 
     1      1      3 
     1      3      1 
     1      3      3 
     3      1      1 
     3      1      3 
     3      3      1 
     3      3      3 
  -0.5   -0.5   -0.5 
  -0.5   -0.5    0.5 
  -0.5    0.5   -0.5 
  -0.5    0.5    0.5 
   0.5   -0.5   -0.5 
   0.5   -0.5    0.5 
   0.5    0.5   -0.5 
   0.5    0.5    0.5 

This still just prints out the QConvex description though. The weird thing is that this works perfectly fine from the command line, just not through python. Even if I can't get piping to work, I absolutely need the reading in from file to work. Does anyone know what the trick is to making this function call?

1

There are 1 answers

1
jfs On BEST ANSWER
  • use shell=True if you use shell features such as | or rewrite the command using pure Python, see Replacing shell pipeline
  • if you use shell=True then pass the command as a string as specified in the docs
from subprocess import check_output as qx

output = qx("rbox c {qpoints} | qconvex FQ FV n".format(qpoints=qpoints),
            shell=True)
print output