Skip to content

Commit

Permalink
Merge branch 'error-trace' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
yuvrajjain2003 committed Dec 1, 2023
2 parents c92cc0f + 26baad7 commit 8ad7693
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions clpipe/convert2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import click
import logging

from .error_handler import exception_handler
from .utils import get_logger
from .status import needs_processing, write_record

Expand Down
48 changes: 48 additions & 0 deletions clpipe/job_dependency_experiment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
import subprocess
from typing import List, Optional

class Job:
def __init__(self, job_name, job_string, parent_jobs = None):
self.job_name = job_name
self.job_string = job_string
self.parent_jobs: Optional[List[Job]] = parent_jobs
self.slurm_id = None

# Kinda need to know how jobs are dependent in the first place to know how to submit
# which imo defeats the purpose of using slurm dependencies? It just makes sure that
# one job finishes running before the next one starts.
j1 = Job("j1", "sbatch --wrap='echo j1'")
j2 = Job("j2", "sbatch --wrap='echo j2'", [j1])
j3 = Job("j3", "sbatch --wrap='echo j3'", [j1])
j4 = Job("j4", "sbatch --wrap='echo j4'", [j2, j3])
j5 = Job("j5", "sbatch --wrap='echo j5'")
j6 = Job("j6", "sbatch --wrap='echo j6'", [j4, j5])

def run_job(job: Job) -> int:
"""Take a job and returns the slurm id for the corresponding job"""
if job.parent_jobs:
dependency_string = " --dependency=afterany:"
for parent_job in job.parent_jobs:
dependency_string += str(parent_job.slurm_id)
dependency_string += ','
job.job_string += dependency_string[:-1]
result = subprocess.run(job.job_string, shell=True, capture_output=True, text=True)
job.slurm_id = int(result.stdout.split(" ")[-1])
return job.slurm_id

run_job(j1)
print(j1.slurm_id)
print(j1.job_string)

run_job(j2)
print(j2.slurm_id)
print(j2.job_string)

run_job(j3)
print(j3.slurm_id)
print(j3.job_string)

run_job(j4)
print(j4.slurm_id)
print(j4.job_string)
10 changes: 9 additions & 1 deletion clpipe/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import click
import os
import sys
import logging
from pathlib import Path

from nipype.utils.filemanip import split_filename


@click.command()
@click.argument("subjects", nargs=-1, required=False, default=None)
@click.option(
Expand Down Expand Up @@ -83,12 +83,16 @@ def resolve_fmriprep_dir(fmriprep_dir):

return fmriprep_root

def exception_handler(logger, exception_type, exception, traceback):
logger.error("%s: %s" % (exception_type.__name__, exception))

def get_logger(name, debug=False, log_dir=None, f_name="clpipe.log"):
logger = logging.getLogger("clpipe").getChild(name)

if debug:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)

if log_dir:
add_file_handler(log_dir, f_name, logger=logger)
Expand All @@ -108,6 +112,10 @@ def get_logger(name, debug=False, log_dir=None, f_name="clpipe.log"):
log_args = {"username": user_name}

logger = logging.LoggerAdapter(logger, log_args)

if not debug:
sys.excepthook = lambda *exc_info: exception_handler(logger, *exc_info)

return logger


Expand Down

0 comments on commit 8ad7693

Please sign in to comment.