Skip to content

Commit

Permalink
🚧 🎉 Somewhat working!
Browse files Browse the repository at this point in the history
1. Clone repo
2. `cd` into local copy
3. `pip install .`
4. `cpac run --help` to see options

Still fairly crash-prone.
Still needs to add support for utils.

on-behalf-of: @FCP-INDI <http://fcp-indi.github.com>
  • Loading branch information
shnizzedy committed Jan 29, 2020
1 parent a02faf6 commit ae9c419
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 33 deletions.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ classifiers =
Operating System :: OS Independent
Programming Language :: Python :: 3
Topic :: Scientific/Engineering :: Bio-Informatics
version = 0.0.1

[options]
zip_safe = False
Expand Down
76 changes: 51 additions & 25 deletions src/cpac/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sys

from cpac import __version__
from cpac.backends.docker import Docker, DockerRun

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -68,14 +69,25 @@ def parse_args(args):
run_parser.add_argument('--address', action='store', type=address)
run_parser.add_argument('--bids_dir', help="input dataset directory", metavar="PATH")
run_parser.add_argument('--output_dir', default='/outputs', help="directory where output files should be stored", metavar="PATH")
run_parser.add_argument(
'--data_config_file',
help="Yaml file containing the location of the data that is to be "
"processed.",
metavar="PATH"
)
run_parser.add_argument('--tag', help="tag of the container image to use (eg, latest or nightly)")
# run_parser.add_argument('--backend', choices=['docker']) # TODO: Add Singularity
run_parser.add_argument('level_of_analysis', choices=['participant', 'group', 'test_config'])
run_parser.add_argument('args', nargs=argparse.REMAINDER, help="any C-PAC optional arguments <http://fcp-indi.github.io/docs/user/quick>")
run_parser.add_argument('extra_args', nargs=argparse.REMAINDER, help="any C-PAC optional arguments <http://fcp-indi.github.io/docs/user/running>")

parsed = parser.parse_args(args)
parsed, extras = parser.parse_known_args(args)

# Set backend automatically while there's only one supported backend
parsed.backend = 'docker'
parsed.extra_args = [
*(parsed.extra_args if hasattr(parsed, 'extra_args') else []),
*extras
]

return parsed

Expand All @@ -89,35 +101,49 @@ def setup_logging(loglevel):
def main(args):
command = args[0]
args = parse_args(args[1:])
setup_logging(args.loglevel)

if args.command == 'scheduler':
from cpac.scheduler.process import start_scheduler
start_scheduler(args.address, args.backend)
args.data_config_file = args.data_config_file if hasattr(
args,
'data_config_file'
) else None

elif args.command == 'run':
args.bids_dir = args.bids_dir if hasattr(
args,
'bids_dir'
) else None

if not args.address:
from cpac.scheduler.process import spawn_scheduler
spawn_scheduler(args.address, args.backend)

from cpac.scheduler.client import schedule, wait
from cpac.scheduler import SCHEDULER_ADDRESS
if ((args.bids_dir is None) and (args.data_config_file is None)):
raise AttributeError(
"cpac requires at least one of `bids_dir` or `data_config_file` to "
"run. See http://fcp-indi.github.io/docs/user/running"
)

scheduler = args.address or SCHEDULER_ADDRESS
setup_logging(args.loglevel)

schedule(
scheduler,
args.backend,
args.data_config_file if hasattr(
args,
'data_config_file'
) else None,
args.pipeline_file if hasattr(
args,
'pipeline_file'
) else None
)
if args.command == 'run':

Docker().run(flags=" ".join(args.extra_args), **vars(args))
# DockerRun()
#
# if not args.address:
# from cpac.scheduler.process import spawn_scheduler
# spawn_scheduler(args.address, args.backend)
#
# from cpac.scheduler.client import schedule, wait
# from cpac.scheduler import SCHEDULER_ADDRESS
#
# scheduler = args.address or SCHEDULER_ADDRESS
#
# schedule(
# scheduler,
# args.backend,
# args.data_config_file,
# args.pipeline_file if hasattr(
# args,
# 'pipeline_file'
# ) else None
# )


def run():
Expand Down
55 changes: 48 additions & 7 deletions src/cpac/backends/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,60 @@

class Docker(Backend):

tag = 'nightly'

def __init__(self, scheduler):
def __init__(self):
self.client = docker.from_env()
print('a')
try:
self.client.ping()
except docker.errors.APIError:
raise "Could not connect to Docker"
self.scheduler = scheduler

def schedule(self, pipeline, data_config):
self.scheduler.schedule(DockerSchedule(self, pipeline, data_config))
def run(self, tag='nightly', flags="", **kwargs):

volumes = {
kwargs['temp_dir']: {'bind': '/scratch', 'mode': 'rw'},
kwargs['output_dir']: {'bind': '/outputs', 'mode':'rw'},
kwargs['working_dir']: {'bind': '/wd', 'mode': 'rw'}
}

if isinstance(
kwargs['bids_dir'], str
) and not kwargs['bids_dir'].startswith('s3://'):
bids_dir = '/bids_dir'
volumes[kwargs['bids_dir']] = {'bind': bids_dir, 'mode': 'ro'}
else:
bids_dir = str(kwargs['bids_dir'])

command = [i for i in [
bids_dir,
'/outputs',
kwargs['level_of_analysis'],
*flags.split(' ')
] if (i is not None and len(i))]

print(command)
print(volumes)
print(tag)
print(':'.join([
'fcpindi/c-pac',
tag if isinstance(tag, str) else 'nightly'
]))

_run = DockerRun(self.client.containers.run(
':'.join([
'fcpindi/c-pac',
tag if isinstance(tag, str) else 'nightly'
]),
command=command,
detach=False,
user=os.getuid(),
volumes=volumes,
working_dir='/wd'
))

print('a')
_run.container.wait()
print('b')


class DockerRun(object):
Expand Down Expand Up @@ -118,7 +160,6 @@ def run(self):
)
)


class DockerSubjectSchedule(DockerSchedule):

def __init__(self, backend, pipeline, subject, parent=None):
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[tox]
minversion = 2.4
envlist = py34,py35,py36,flake8
envlist = py35,py36,flake8
skip_missing_interpreters = True

[testenv]
Expand Down

4 comments on commit ae9c419

@shnizzedy
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still needs to print logs to stdout

@shnizzedy
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still needs to print logs to stdout

Resolved in 72ebe07

@sgiavasis
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very excited!!! 🔥

@shnizzedy
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.