Search code examples
pythondockersys

dockerize python script with sys.path


Hello i'm trying to run image docker, which i created from Dockerfile:

FROM python:3

ADD feature.py /

RUN pip install pandas
RUN pip install numpy

CMD ["python", "/feature.py"]

Here is the python script:

import os # I am not sure we are going to do import right here???
import pandas as pd
import numpy as np
import sys


def freesurfer_statistics(address):


df_asseg = []

data = pd.read_csv(address+'/'+'/stats/aseg.stats', sep = '#').loc[79:] 
['Unnamed: 0']

patient = pd.DataFrame()
for i in range(79,124):
    patient = patient.append(data[i].split())

data = pd.DataFrame(np.array(patient).reshape(45,10))
data.columns = ['Index','SegId','NVoxels','Volume_mm3',\

'StructName','normMean','normStdDev','normMin','normMax','normRange']

df_asseg.append(list(data.NVoxels)+list(data.Volume_mm3)+ list(data.normMean)+list(data.normStdDev)+list(data.normMin)+list(data.normMax)+list(data.normRange))


df_asseg = pd.DataFrame(np.array(df_asseg).reshape(1,315))

df_rh = pd.DataFrame(np.array(df_rh).reshape(1,306))

df_rh.columns = names
csv_file=pd.concat([df_asseg, df_rh, df_lh], axis = 1)


csv_file.to_csv(address+'/morphometry.csv')

freesurfer_statistics(sys.argv[1])

It finds the file and write data to morphometry.csv

After docker build i running container:

docker run --rm -v /mnt/data/service/fmriprep/run_forrest/freesurfer/sub-01:/data features:latest

i have the error:

Traceback (most recent call last):
File "/feature.py", line 95, in <module>
freesurfer_statistics(sys.argv[1])
IndexError: list index out of range

I think i make a mistake in Dockerfile, i m newby in it. But can't get an info.


Solution

  • Your freesurfer_statistics(sys.argv[1]) need a command line argument, which you are not providing when invoking the python script using CMD in your Dockerfile. So, your CMD line in Dockerfile should be modified as follows.

    CMD ["python", "/feature.py", "argument-for-freeuser_statitics"]
    

    To clarify, sys.arg[0] is the name of the script itself, and sys.arg[1] is the first argument you provide when invoking the script on the command line.

    Edit based on comment, What you did with docker run was just map a directory to the docker containers. Since you have mapped your host directory to /data that should be the "argument-for-the-freeuser_statistics".

    CMD ["python", "/feature.py", "/data"]