@@ -176,19 +176,25 @@ def get_groups(obs_id, configs, context):
176
176
groups : list of list of int
177
177
The list of groups of detectors.
178
178
"""
179
- group_by = np .atleast_1d (configs ['subobs' ].get ('use' , 'detset' ))
180
- for i , gb in enumerate (group_by ):
181
- if gb .startswith ('dets:' ):
182
- group_by [i ] = gb .split (':' ,1 )[1 ]
183
-
184
- if (gb == 'detset' ) and (len (group_by ) == 1 ):
185
- groups = context .obsfiledb .get_detsets (obs_id )
186
- return group_by , [[g ] for g in groups ]
187
-
188
- det_info = context .get_det_info (obs_id )
189
- rs = det_info .subset (keys = group_by ).distinct ()
190
- groups = [[b for a ,b in r .items ()] for r in rs ]
191
- return group_by , groups
179
+ try :
180
+ group_by = np .atleast_1d (configs ['subobs' ].get ('use' , 'detset' ))
181
+ for i , gb in enumerate (group_by ):
182
+ if gb .startswith ('dets:' ):
183
+ group_by [i ] = gb .split (':' ,1 )[1 ]
184
+
185
+ if (gb == 'detset' ) and (len (group_by ) == 1 ):
186
+ groups = context .obsfiledb .get_detsets (obs_id )
187
+ return group_by , [[g ] for g in groups ], None
188
+
189
+ det_info = context .get_det_info (obs_id )
190
+ rs = det_info .subset (keys = group_by ).distinct ()
191
+ groups = [[b for a ,b in r .items ()] for r in rs ]
192
+ return group_by , groups , None
193
+ except Exception as e :
194
+ error = f'Failed get groups for: { obs_id } '
195
+ errmsg = f'{ type (e )} : { e } '
196
+ tb = '' .join (traceback .format_tb (e .__traceback__ ))
197
+ return [], [], [error , errmsg , tb ]
192
198
193
199
194
200
def get_preprocess_db (configs , group_by , logger = None ):
@@ -388,8 +394,14 @@ def multilayer_load_and_preprocess(obs_id, configs_init, configs_proc,
388
394
configs_proc , context_proc = get_preprocess_context (configs_proc , context_proc )
389
395
meta_proc = context_proc .get_meta (obs_id , dets = dets , meta = meta )
390
396
391
- group_by_init , groups_init = get_groups (obs_id , configs_init , context_init )
392
- group_by_proc , groups_proc = get_groups (obs_id , configs_proc , context_proc )
397
+ group_by_init , groups_init , error_init = get_groups (obs_id , configs_init , context_init )
398
+ group_by_proc , groups_proc , error_proc = get_groups (obs_id , configs_proc , context_proc )
399
+
400
+ if error_init is not None :
401
+ raise ValueError (f"{ error_init [0 ]} \n { error_init [1 ]} \n { error_init [2 ]} " )
402
+
403
+ if error_proc is not None :
404
+ raise ValueError (f"{ error_proc [0 ]} \n { error_proc [1 ]} \n { error_proc [2 ]} " )
393
405
394
406
if (group_by_init != group_by_proc ).any ():
395
407
raise ValueError ('init and proc groups do not match' )
@@ -451,7 +463,7 @@ def find_db(obs_id, configs, dets, context=None, logger=None):
451
463
configs = yaml .safe_load (open (configs , "r" ))
452
464
if context is None :
453
465
context = core .Context (configs ["context_file" ])
454
- group_by , _ = get_groups (obs_id , configs , context )
466
+ group_by , _ , _ = get_groups (obs_id , configs , context )
455
467
cur_groups = [list (np .fromiter (dets .values (), dtype = '<U32' ))]
456
468
dbexist = True
457
469
if os .path .exists (configs ['archive' ]['index' ]):
@@ -560,7 +572,7 @@ def save_group_and_cleanup(obs_id, configs, context=None, subdir='temp',
560
572
if context is None :
561
573
context = core .Context (configs ["context_file" ])
562
574
563
- group_by , groups = get_groups (obs_id , configs , context )
575
+ group_by , groups , error = get_groups (obs_id , configs , context )
564
576
565
577
all_groups = groups .copy ()
566
578
for g in all_groups :
@@ -583,6 +595,49 @@ def save_group_and_cleanup(obs_id, configs, context=None, subdir='temp',
583
595
except OSError as e :
584
596
# remove if it can't be opened
585
597
os .remove (outputs_grp ['temp_file' ])
598
+ return error
599
+
600
+
601
+ def cleanup_obs (obs_id , policy_dir , errlog , configs , context = None ,
602
+ subdir = 'temp' , remove = False ):
603
+ """
604
+ For a given obs id, this function will search the policy_dir directory
605
+ if it exists for any files with that obsnum in their filename. If any are
606
+ found, it will run save_group_and_cleanup for that obs id.
607
+
608
+ Arguments
609
+ ---------
610
+ obs_id: str
611
+ Obs id to check and clean up
612
+ policy_dir: str
613
+ Directory to temp per-group output files
614
+ errlog: fpath
615
+ Filepath to error logging file.
616
+ configs: fpath or dict
617
+ Filepath or dictionary containing the preprocess configuration file.
618
+ context: core.Context
619
+ Optional. Context object used for data loading/querying.
620
+ subdir: str
621
+ Optional. Subdirectory to save the output files into.
622
+ remove: bool
623
+ Optional. Default is False. Whether to remove a file if found.
624
+ Used when ``overwrite`` is True in driving functions.
625
+ """
626
+
627
+ if os .path .exists (policy_dir ):
628
+ found = False
629
+ for f in os .listdir (policy_dir ):
630
+ if obs_id in f :
631
+ found = True
632
+ break
633
+
634
+ if found :
635
+ error = save_group_and_cleanup (obs_id , configs , context ,
636
+ subdir = subdir , remove = remove )
637
+ if error is not None :
638
+ f = open (errlog , 'a' )
639
+ f .write (f'\n { time .time ()} , cleanup error\n { error [0 ]} \n { error [2 ]} \n ' )
640
+ f .close ()
586
641
587
642
588
643
def preproc_or_load_group (obs_id , configs_init , dets , configs_proc = None , logger = None ,
@@ -657,9 +712,12 @@ def preproc_or_load_group(obs_id, configs_init, dets, configs_proc=None, logger=
657
712
if context_proc is None :
658
713
context_proc = core .Context (configs_proc ["context_file" ])
659
714
660
- group_by , groups = get_groups (obs_id , configs_proc , context_proc )
715
+ group_by , groups , error = get_groups (obs_id , configs_proc , context_proc )
661
716
else :
662
- group_by , groups = get_groups (obs_id , configs_init , context_init )
717
+ group_by , groups , error = get_groups (obs_id , configs_init , context_init )
718
+
719
+ if error is not None :
720
+ return error [0 ], [error [1 ], error [2 ]], [error [1 ], error [2 ]], None
663
721
664
722
all_groups = groups .copy ()
665
723
cur_groups = [list (np .fromiter (dets .values (), dtype = '<U32' ))]
@@ -674,11 +732,13 @@ def preproc_or_load_group(obs_id, configs_init, dets, configs_proc=None, logger=
674
732
error = 'no_group_overlap'
675
733
return error , [obs_id , dets ], [obs_id , dets ], None
676
734
677
- db_init_exist = find_db (obs_id , configs_init , dets , context_init )
735
+ db_init_exist = find_db (obs_id , configs_init , dets , context_init ,
736
+ logger = logger )
678
737
679
738
db_proc_exist = False
680
739
if configs_proc is not None :
681
- db_proc_exist = find_db (obs_id , configs_proc , dets , context_proc )
740
+ db_proc_exist = find_db (obs_id , configs_proc , dets , context_proc ,
741
+ logger = logger )
682
742
683
743
if (not db_init_exist ) and db_proc_exist and (not overwrite ):
684
744
logger .info ('dependent db requires initial db if not overwriting' )
@@ -882,7 +942,8 @@ def cleanup_mandb(error, outputs, configs, logger=None, overwrite=False):
882
942
errlog = os .path .join (folder , 'errlog.txt' )
883
943
f = open (errlog , 'a' )
884
944
f .write (f'{ time .time ()} , { error } \n ' )
885
- f .write (f'\t { outputs [0 ]} \n \t { outputs [1 ]} \n ' )
945
+ if outputs is not None :
946
+ f .write (f'\t { outputs [0 ]} \n \t { outputs [1 ]} \n ' )
886
947
f .close ()
887
948
888
949
0 commit comments