Skip to content

Commit 71e8176

Browse files
committed
Various improvements
Minor changes
1 parent 0a543a7 commit 71e8176

22 files changed

+146
-48
lines changed

I3ToSQLite/I3ToSQLite.egg-info/PKG-INFO

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Metadata-Version: 1.1
22
Name: I3ToSQLite
3-
Version: 0.1.53
3+
Version: 0.1.63
44
Summary: An unoffical I3-file to SQLite database converter.
55
Home-page: https://github.com/RasmusOrsoe/I3ToSQLite
66
Author: Rasmus F. Ørsøe

I3ToSQLite/I3ToSQLite/ __init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
from .tools import *
33
from .CreateTemporaryDatabases import *
44
from .MergeTemporaryDatabases import *
5-
__version__ = "0.1.53"
5+
__version__ = "0.1.62"
66
__author__ = 'Rasmus F. Ørsøe'
77
__credits__ = 'Niels Bohr Instutite, IceCube Collaboration'

I3ToSQLite/I3ToSQLite/CreateTemporaryDatabases.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
import time
1212
from multiprocessing import Pool
1313
import pickle
14-
#icetray.I3Logger.global_logger = icetray.I3NullLogger()
14+
import argparse
15+
import sys
16+
17+
parser = argparse.ArgumentParser()
18+
parser.add_argument("-config", "--config", type=str, required=True)
19+
args = parser.parse_args()
20+
21+
1522

1623
def Contains_RetroReco(frame):
1724
try:
@@ -248,6 +255,7 @@ def WriteDicts(settings):
248255
if verbose > 0:
249256
print('Worker %s Reading %s'%(id,input_file.split('/')[-1]))
250257
print(input_file)
258+
sys.stdout.flush()
251259
gcd_count +=1
252260

253261
while i3_file.more() :
@@ -281,6 +289,7 @@ def WriteDicts(settings):
281289
if len(retros)>0 :
282290
retro_big = retro_big.append(retro, ignore_index = True, sort = True)
283291
if len(truth_big) >= max_dict_size:
292+
print('saving')
284293
engine = sqlalchemy.create_engine('sqlite:///'+outdir + '/%s/tmp/worker-%s-%s.db'%(db_name,id,output_count))
285294
truth_big.to_sql('truth',engine,index= False, if_exists = 'append')
286295
if len(retro_big)> 0:
@@ -296,6 +305,7 @@ def WriteDicts(settings):
296305
if verbose > 0:
297306
print('Worker %s has finished %s/%s I3 files.'%(id, file_counter, len(input_files)))
298307
if (len(feature_big) > 0):
308+
print('saving eof')
299309
engine = sqlalchemy.create_engine('sqlite:///'+outdir + '/%s/tmp/worker-%s-%s.db'%(db_name,id,output_count))
300310
truth_big.to_sql('truth',engine,index= False, if_exists = 'append')
301311
if len(retro_big)> 0:
@@ -363,8 +373,18 @@ def WalkDirectory(dir, extensions):
363373
gcds_folder.append(gcd_folder)
364374
files_list.extend(i3files_folder)
365375
GCD_list.extend(gcds_folder)
376+
377+
files_list, GCD_list = PairWiseShuffle(files_list, GCD_list)
366378
return files_list, GCD_list
367379

380+
def PairWiseShuffle(files_list, gcd_list):
381+
df = pd.DataFrame({'i3': files_list, 'gcd': gcd_list})
382+
df_shuffled = df.sample(frac = 1)
383+
i3_shuffled = df_shuffled['i3'].tolist()
384+
gcd_shuffled = df_shuffled['gcd'].tolist()
385+
return i3_shuffled, gcd_shuffled
386+
387+
368388
def FindFiles(paths,outdir,db_name,gcd_rescue, extensions = None):
369389
print('Counting files in: \n%s\n This might take a few minutes...'%paths)
370390
if extensions == None:
@@ -379,10 +399,10 @@ def FindFiles(paths,outdir,db_name,gcd_rescue, extensions = None):
379399
input_files.extend(input_files_mid)
380400
gcd_files.extend(gcd_files_mid)
381401

382-
402+
input_files, gcd_files = PairWiseShuffle(input_files, gcd_files)
383403
Save_Filenames(input_files, outdir, db_name)
384-
385404
return input_files, gcd_files
405+
386406
def Save_Filenames(input_files,outdir, db_name):
387407
input_files = pd.DataFrame(input_files)
388408
input_files.columns = ['filename']
@@ -405,8 +425,8 @@ def PickleCleaner(List):
405425
clean_list.append(str(element))
406426
return clean_list
407427

408-
def Extract_Config():
409-
with open('tmp/config/config.pkl', 'rb') as handle:
428+
def Extract_Config(config_path):
429+
with open('%s/config.pkl'%config_path, 'rb') as handle:
410430
config = pickle.load(handle)
411431
paths = PickleCleaner(config['paths'])
412432

@@ -429,10 +449,10 @@ def Extract_Config():
429449
custom_truth = None
430450
return paths, outdir, workers, pulse_keys, db_name, max_dictionary_size, custom_truth, gcd_rescue, verbose
431451
def Transmit_Start_Time(start_time,config_path):
432-
with open(config_path, 'rb') as handle:
452+
with open('%s/config.pkl'%config_path, 'rb') as handle:
433453
config = pickle.load(handle)
434454
config['start_time'] = start_time
435-
with open(config_path , 'wb') as handle:
455+
with open('%s/config.pkl'%config_path, 'wb') as handle:
436456
pickle.dump(config, handle, protocol=2)
437457
return
438458

@@ -443,7 +463,9 @@ def PrintMessage(workers, input_files):
443463
print('----------------------')
444464
def CreateTemporaryDatabases(paths, outdir, workers, pulse_keys,config_path, start_time,db_name,gcd_rescue,verbose,max_dictionary_size = 10000, custom_truth = None):
445465
if __name__ == "__main__" :
446-
start_time = time.time()
466+
start_time = time.time()
467+
if verbose == 0:
468+
icetray.I3Logger.global_logger = icetray.I3NullLogger()
447469
directory_exists = CreateOutDirectory(outdir + '/%s/tmp'%db_name)
448470
input_files, gcd_files = FindFiles(paths, outdir,db_name,gcd_rescue)
449471
if workers > len(input_files):
@@ -463,13 +485,15 @@ def CreateTemporaryDatabases(paths, outdir, workers, pulse_keys,config_path, sta
463485
p.map_async(WriteDicts, settings)
464486
p.close()
465487
p.join()
488+
print('transmitting..')
466489
Transmit_Start_Time(start_time, config_path)
467490

468491

469492
start_time = time.time()
470493

471-
paths, outdir, workers, pulse_keys, db_name, max_dictionary_size, custom_truth, gcd_rescue, verbose = Extract_Config()
472-
CreateTemporaryDatabases(paths, outdir, workers, pulse_keys,'tmp/config/config.pkl', start_time,db_name,gcd_rescue,verbose, max_dictionary_size, custom_truth)
494+
paths, outdir, workers, pulse_keys, db_name, max_dictionary_size, custom_truth, gcd_rescue, verbose = Extract_Config(args.config)
495+
print(args.config)
496+
CreateTemporaryDatabases(paths, outdir, workers, pulse_keys,args.config, start_time,db_name,gcd_rescue,verbose, max_dictionary_size, custom_truth)
473497

474498

475499

I3ToSQLite/I3ToSQLite/MergeTemporaryDatabases.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
import time
77
import pickle
88
from tqdm import tqdm
9+
import argparse
10+
11+
parser = argparse.ArgumentParser()
12+
parser.add_argument("-config", "--config", type=str, required=True)
13+
args = parser.parse_args()
14+
15+
916
def fetch_temps(path):
1017
out = []
1118
files = os.listdir(path)
@@ -67,7 +74,7 @@ def Run_SQL_Code(database, CODE):
6774
return
6875

6976
def Attach_Index(database, table_name):
70-
CODE = "PRAGMA foreign_keys=off;\nBEGIN TRANSACTION;\nCREATE INDEX event_no ON {} (event_no);\nCOMMIT TRANSACTION;\nPRAGMA foreign_keys=on;".format(table_name)
77+
CODE = "PRAGMA foreign_keys=off;\nBEGIN TRANSACTION;\nCREATE INDEX event_no_{} ON {} (event_no);\nCOMMIT TRANSACTION;\nPRAGMA foreign_keys=on;".format(table_name,table_name)
7178
Run_SQL_Code(database,CODE)
7279
return
7380

@@ -95,21 +102,25 @@ def CreateTable(database,table_name, columns, is_pulse_map = False):
95102
CODE = "PRAGMA foreign_keys=off;\nCREATE TABLE {} ({});\nPRAGMA foreign_keys=on;".format(table_name,query_columns)
96103
Run_SQL_Code(database, CODE)
97104
if is_pulse_map:
98-
try:
99-
Attach_Index(database,table_name)
100-
except:
101-
notimportant = 0
105+
#try:
106+
print(table_name)
107+
print('attaching indexs')
108+
Attach_Index(database,table_name)
109+
#except:
110+
# notimportant = 0
102111
return
103112

104113
def Create_Empty_Tables(database,pulse_map_keys,truth_columns, pulse_map_columns, retro_columns):
105-
for pulse_map_key in pulse_map_keys:
106-
# Creates the pulse map tables
107-
print('Creating Empty %s Table'%pulse_map_key)
108-
CreateTable(database, pulse_map_key,pulse_map_columns[pulse_map_key], is_pulse_map = True)
114+
109115
print('Creating Empty Truth Table')
110116
CreateTable(database, 'truth', truth_columns, is_pulse_map = False) # Creates the truth table containing primary particle attributes and RetroReco reconstructions
111117
print('Creating Empty RetroReco Table')
112118
CreateTable(database, 'RetroReco',retro_columns, is_pulse_map = False) # Creates the RetroReco Table with reconstuctions and associated values.
119+
120+
for pulse_map_key in pulse_map_keys:
121+
# Creates the pulse map tables
122+
print('Creating Empty %s Table'%pulse_map_key)
123+
CreateTable(database, pulse_map_key,pulse_map_columns[pulse_map_key], is_pulse_map = True)
113124
return
114125

115126
def Submit_Truth(database, truth):
@@ -150,8 +161,8 @@ def PickleCleaner(List):
150161
clean_list.append(str(element))
151162
return clean_list
152163

153-
def Extract_Config():
154-
with open('tmp/config/config.pkl', 'rb') as handle:
164+
def Extract_Config(config_path):
165+
with open('%s/config.pkl'%config_path, 'rb') as handle:
155166
config = pickle.load(handle)
156167
paths = PickleCleaner(config['paths'])
157168

@@ -198,7 +209,7 @@ def CreateDatabase(database_name,outdir, pulse_map_keys):
198209
return
199210

200211

201-
paths, outdir, workers, pulse_keys, db_name, max_dictionary_size, custom_truth, start_time = Extract_Config()
212+
paths, outdir, workers, pulse_keys, db_name, max_dictionary_size, custom_truth, start_time = Extract_Config(args.config)
202213
CreateDatabase(db_name, outdir, pulse_keys)
203214

204215
print('Database Creation Successful!')

I3ToSQLite/I3ToSQLite/Tools.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,40 @@ def Build_Configuration(paths, outdir, workers, pulse_keys, db_name, gcd_rescue,
1616
os.makedirs(dictionary_path)
1717
except:
1818
notimportant = 0
19-
try:
20-
os.makedirs('tmp/config')
21-
except:
22-
notimportant = 0
2319

2420
with open(dictionary_path + '/config.pkl', 'wb') as handle:
2521
pickle.dump(dictionary, handle, protocol=2)
26-
with open('tmp/config/config.pkl', 'wb') as handle:
27-
pickle.dump(dictionary, handle, protocol=2)
28-
return dictionary_path + '/config.pkl'
29-
def MakeDir():
22+
23+
print(dictionary_path)
24+
return dictionary_path
25+
def MakeDir(coms_path):
3026
try:
31-
os.makedirs('tmp/coms')
27+
os.makedirs(coms_path)
3228
except:
3329
notimportant = 0
34-
def Write_Handler(cvmfs_setup_path, cvmfs_shell_path):
35-
CODE = "eval `%s` \n%s ./tmp/coms/run_extraction.sh"%(cvmfs_setup_path,cvmfs_shell_path)
36-
text_file = open("tmp/coms/handler.sh", "w")
30+
return coms_path
31+
32+
def Write_Handler(cvmfs_setup_path, cvmfs_shell_path, coms_path):
33+
CODE = "eval `%s` \n%s %s/run_extraction.sh"%(cvmfs_setup_path,cvmfs_shell_path, coms_path)
34+
text_file = open("%s/handler.sh"%coms_path, "w")
3735
text_file.write(CODE)
3836
text_file.close()
39-
os.system("chmod 755 tmp/coms/handler.sh")
37+
os.system("chmod 755 %s/handler.sh"%coms_path)
4038

41-
def Write_Executer():
39+
def Write_Executer(config_path, coms_path):
4240
path = (anchor.__file__).split('anchor.py')[0]
43-
CODE = "python %sCreateTemporaryDatabases.py && python %sMergeTemporaryDatabases.py && exit"%(path,path)
44-
text_file = open("tmp/coms/run_extraction.sh", "w")
41+
CODE = "python %sCreateTemporaryDatabases.py --config %s && python %sMergeTemporaryDatabases.py --config %s && exit"%(path,config_path,path,config_path)
42+
text_file = open("%s/run_extraction.sh"%coms_path, "w")
4543
text_file.write(CODE)
4644
text_file.close()
47-
os.system("chmod 755 tmp/coms/run_extraction.sh")
45+
os.system("chmod 755 %s/run_extraction.sh"%coms_path)
4846

4947
def CreateDatabase(paths, outdir, workers, cvmfs_setup_path, cvmfs_shell_path, db_name, pulse_keys, gcd_rescue, verbose = 1):
50-
configuration_path = Build_Configuration(paths, outdir, workers, pulse_keys,db_name, gcd_rescue, verbose)
51-
MakeDir()
52-
Write_Executer()
53-
Write_Handler(cvmfs_setup_path, cvmfs_shell_path)
54-
os.system('./tmp/coms/handler.sh')
48+
config_path = Build_Configuration(paths, outdir, workers, pulse_keys,db_name, gcd_rescue, verbose)
49+
coms_path = MakeDir(outdir + '/%s/config'%db_name)
50+
Write_Executer(config_path, coms_path)
51+
Write_Handler(cvmfs_setup_path, cvmfs_shell_path, coms_path)
52+
os.system('%s/handler.sh'%coms_path)
5553

5654

5755

dist/I3ToSQLite-0.1.54.tar.gz

7.8 KB
Binary file not shown.

dist/I3ToSQLite-0.1.55.tar.gz

7.79 KB
Binary file not shown.

dist/I3ToSQLite-0.1.56.tar.gz

7.79 KB
Binary file not shown.

dist/I3ToSQLite-0.1.57.tar.gz

7.79 KB
Binary file not shown.

dist/I3ToSQLite-0.1.58.tar.gz

7.79 KB
Binary file not shown.

0 commit comments

Comments
 (0)