11"""This is the main module."""
22
33import numpy as np
4+ import pandas as pd
45
56from babs .base import BABS
67from babs .scheduler import (
@@ -30,20 +31,16 @@ def babs_submit(self, count=None, submit_df=None, skip_failed=False):
3031 default: None
3132 """
3233
33- # Check if there are still jobs running
34+ # Check if there are still jobs running/pending
3435 currently_running_df = self .get_currently_running_jobs_df ()
35- if currently_running_df .shape [0 ] > 0 :
36- non_cg_states = (
37- currently_running_df ['state' ].fillna ('' ).ne ('CG' )
38- if 'state' in currently_running_df
39- else np .array ([True ] * currently_running_df .shape [0 ])
40- )
41- if non_cg_states .any ():
42- raise Exception (
43- 'There are still jobs running. Please wait for them to finish or cancel them.'
44- f' Current running jobs:\n { currently_running_df } '
45- )
46- print ('All currently running jobs are in CG state; proceeding with submission.' )
36+ if currently_running_df .empty :
37+ running_pending_df = currently_running_df
38+ elif 'state' in currently_running_df :
39+ running_pending_df = currently_running_df [
40+ currently_running_df ['state' ].fillna ('' ).isin (['PD' , 'R' ])
41+ ]
42+ else :
43+ running_pending_df = currently_running_df
4744
4845 # Find the rows that don't have results yet
4946 status_df = self .get_job_status_df ()
@@ -54,6 +51,36 @@ def babs_submit(self, count=None, submit_df=None, skip_failed=False):
5451 if submit_df is not None :
5552 df_needs_submit = submit_df
5653
54+ # Remove any jobs that are still running/pending
55+ if not running_pending_df .empty :
56+ merge_cols = ['sub_id' ]
57+ if 'ses_id' in df_needs_submit and 'ses_id' in running_pending_df :
58+ merge_cols = ['sub_id' , 'ses_id' ]
59+ if all (col in running_pending_df for col in merge_cols ):
60+ skipped_df = pd .merge (
61+ df_needs_submit ,
62+ running_pending_df ,
63+ on = merge_cols ,
64+ how = 'inner' ,
65+ suffixes = ('' , '_running' ),
66+ )
67+ df_needs_submit = df_needs_submit .merge (
68+ running_pending_df [merge_cols ].drop_duplicates (),
69+ on = merge_cols ,
70+ how = 'left' ,
71+ indicator = True ,
72+ )
73+ df_needs_submit = df_needs_submit [df_needs_submit ['_merge' ] == 'left_only' ]
74+ df_needs_submit = df_needs_submit .drop (columns = ['_merge' ])
75+ if not skipped_df .empty :
76+ if 'job_id_running' in skipped_df :
77+ job_ids = sorted (
78+ pd .Series (skipped_df ['job_id_running' ]).dropna ().unique ().tolist ()
79+ )
80+ print (f'Skipping running/pending jobs. Job IDs: { job_ids } ' )
81+ else :
82+ print ('Skipping running/pending jobs without job IDs.' )
83+
5784 # only run `babs submit` when there are subjects/sessions not yet submitted
5885 if df_needs_submit .empty :
5986 print ('No jobs to submit' )
0 commit comments