RTModel 2.3__cp38-cp38-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
RTModel/RTModel.py ADDED
@@ -0,0 +1,480 @@
1
+ import site
2
+ import subprocess
3
+ import os
4
+ import sys
5
+ import glob
6
+ import time
7
+ from pathlib import Path
8
+ from tqdm import tqdm
9
+ import shutil
10
+
11
+
12
+ class RTModel:
13
+ def __init__(self, event = None):
14
+ # Directory preliminaries
15
+ print('*********************')
16
+ print('**** RTModel ****')
17
+ print('*********************')
18
+ self.bindir = self.find_bin_directory()
19
+ if(os.path.exists(self.bindir + 'Reader.exe')):
20
+ self.readerexe = 'Reader.exe'
21
+ self.initcondexe = 'InitCond.exe'
22
+ self.levmarexe = 'LevMar.exe'
23
+ self.modelselectorexe = 'ModelSelector.exe'
24
+ self.finalizerexe = 'Finalizer.exe'
25
+ else:
26
+ self.readerexe = 'Reader'
27
+ self.initcondexe = 'InitCond'
28
+ self.levmarexe = 'LevMar'
29
+ self.modelselectorexe = 'ModelSelector'
30
+ self.finalizerexe = 'Finalizer'
31
+ if(event == None):
32
+ self.eventname = os.getcwd()
33
+ else:
34
+ self.eventname = os.path.realpath(event)
35
+ print("Event name: " + self.eventname)
36
+ self.inidir = "ini"
37
+ self.modelcodes = ['PS', 'PX', 'BS', 'BO', 'LS', 'LX', 'LO', 'LK', 'TS', 'TX']
38
+ self.endphase = len(self.modelcodes)*2+3
39
+ self.eventinifile = 'event.ini'
40
+ self.nprocessors = os.cpu_count()
41
+ print('Number of processors: {}'.format(self.nprocessors))
42
+ self.config_Reader()
43
+ self.config_InitCond()
44
+ self.config_LevMar()
45
+ self.config_ModelSelector()
46
+ self.satellitedir = '.'
47
+
48
+ def set_processors(self, nprocessors):
49
+ self.nprocessors = nprocessors
50
+
51
+ def set_event(self, event):
52
+ self.eventname = os.path.realpath(event)
53
+
54
+ def set_satellite_dir(self, satellitedir):
55
+ self.satellitedir = satellitedir
56
+
57
+ def set_constraints(self, constraints = None):
58
+ self.constraints = constraints
59
+ if(not os.path.exists(self.eventname + '/' + self.inidir)):
60
+ os.makedirs(self.eventname + '/' + self.inidir)
61
+ with open(self.eventname + '/' + self.inidir + '/Constraints.ini','w') as f:
62
+ for cons in constraints:
63
+ f.write(cons[0] + ' = '+ str(cons[1]) + ' '+ str(cons[2]) + ' '+ str(cons[3]) + ' ' + '\n')
64
+
65
+ def config_Reader(self, tau = 0.1, binning = 4000, otherseasons = 1, renormalize = 1, thresholdoutliers = 10):
66
+ self.Reader_tau= tau # conventional correlation time for consecutive points
67
+ self.Reader_binning = binning # maximum number of points left after re-binning
68
+ self.Reader_otherseasons = otherseasons # How to use other seasons (0 = Yes, 1 = decrease significance, 2 = remove)
69
+ self.Reader_renormalize = renormalize # Re-normalize error bars if non-zero
70
+ self.Reader_thresholdoutliers = thresholdoutliers # Threshold in sigmas for removing outliers
71
+
72
+ def Reader(self):
73
+ if(not os.path.exists(self.eventname + '/' + self.inidir)):
74
+ os.makedirs(self.eventname + '/' + self.inidir)
75
+ with open(self.eventname + '/' + self.inidir + '/Reader.ini','w') as f:
76
+ f.write('tau = ' + str(self.Reader_tau) + '\n')
77
+ f.write('binning = ' + str(self.Reader_binning) + '\n')
78
+ f.write('otherseasons = ' + str(self.Reader_otherseasons) + '\n')
79
+ f.write('renormalize = ' + str(self.Reader_renormalize) + '\n')
80
+ f.write('thresholdoutliers = ' + str(self.Reader_thresholdoutliers) + '\n')
81
+ print('- Launching: Reader')
82
+ print(' Pre-processing data...')
83
+ completedprocess = subprocess.run([self.bindir+self.readerexe,self.eventname], cwd = self.bindir, shell = False, stdout=subprocess.DEVNULL)
84
+ if(completedprocess.returncode != 0):
85
+ print('! Error in pre-processing. Please check your data!')
86
+ self.done = True
87
+ else:
88
+ print(' OK')
89
+
90
+ def config_InitCond(self, npeaks = 2, peakthreshold = 10.0, oldmodels = 4, override = None, nostatic = False, onlyorbital = False, usesatellite = 0
91
+ , templatelibrary = None, modelcategories = ['PS','PX','BS','BO','LS','LX','LO'], onlyupdate =False):
92
+ self.InitCond_npeaks = npeaks # Number of peaks in the observed light curve to be considered for setting initial conditions.
93
+ self.InitCond_peakthreshold = peakthreshold # Number of sigmas necessary for a deviation to be identified as a maximum or a minimum.
94
+ self.InitCond_oldmodels = oldmodels # Maximum number of old models to include in new run as initial conditions
95
+ self.InitCond_override = override # Override peak identification and manually set peak times
96
+ self.InitCond_nostatic = nostatic or onlyorbital # No static models will be calculated.
97
+ self.InitCond_onlyorbital = onlyorbital # Only orbital motion models will be calculated.
98
+ self.InitCond_usesatellite = usesatellite # Satellite to be used for initial conditions. Ground telescopes by default.
99
+ self.InitCond_templatelibrary = templatelibrary # Template library to be used in place of the default one.
100
+ self.InitCond_modelcategories = modelcategories # Model categories to be fit
101
+ self.InitCond_onlyupdate = onlyupdate # No search but only update of previously found best models
102
+
103
+ def InitCond(self):
104
+ ''' Establishes initial conditions for fitting by executing the InitCond external module.
105
+ Options can be assigned through the config_InitCond() method. '''
106
+ if(not os.path.exists(self.eventname + '/' + self.inidir)):
107
+ os.makedirs(self.eventname + '/' + self.inidir)
108
+ with open(self.eventname + '/' + self.inidir + '/InitCond.ini','w') as f:
109
+ f.write('npeaks = ' + str(self.InitCond_npeaks) + '\n')
110
+ f.write('peakthreshold = ' + str(self.InitCond_peakthreshold) + '\n')
111
+ f.write('oldmodels = ' + str(self.InitCond_oldmodels) + '\n')
112
+ f.write('usesatellite = ' + str(self.InitCond_usesatellite) + '\n')
113
+ if(self.InitCond_nostatic):
114
+ f.write('nostatic = 1\n')
115
+ if(self.InitCond_onlyorbital):
116
+ f.write('onlyorbital = 1\n')
117
+ if(self.InitCond_override != None):
118
+ f.write('override = ' + str(self.InitCond_override[0])+ ' ' + str(self.InitCond_override[1]) + '\n')
119
+ if(self.InitCond_templatelibrary != None):
120
+ f.write('templatelibrary = ' + self.InitCond_templatelibrary + '\n')
121
+ if(self.InitCond_modelcategories != None):
122
+ f.write('modelcategories = '+ ''.join(self.InitCond_modelcategories) + '\n')
123
+ if(self.InitCond_onlyupdate):
124
+ f.write('onlyupdate = 1\n')
125
+ print('- Launching: InitCond')
126
+ print(' Setting initial conditions...')
127
+ completedprocess = subprocess.run([self.bindir+self.initcondexe,self.eventname], cwd = self.bindir, shell = False, stdout=subprocess.DEVNULL)
128
+ if(completedprocess.returncode != 0):
129
+ print('! Error in setting initial conditions!')
130
+ self.done = True
131
+ else:
132
+ peaksearch = True
133
+ i=0
134
+ while(peaksearch):
135
+ initfils=glob.glob(self.eventname + '/InitCond/InitCond'+ self.modelcodes[i] + '*')
136
+ if(len(initfils)!=0):
137
+ peaksearch = False
138
+ with open(initfils[0], 'r') as f:
139
+ npeaks = int(f.readline().split()[0])
140
+ print('Peaks: ',end ='')
141
+ for i in range(0,npeaks):
142
+ print(f'{float(f.readline().split()[0]):.4f}',end = ' ')
143
+ print('\n OK')
144
+
145
+ def config_LevMar(self, nfits = 6, offsetdegeneracy = 3, timelimit = 600.0, maxsteps = 50, bumperpower = 2.0):
146
+ self.LevMar_nfits = nfits # Number of models to be calculated from the same initial condition using the bumper method
147
+ self.LevMar_offsetdegeneracy = offsetdegeneracy # Number of models to be fit after applying offset degeneracy to best model found so far
148
+ self.LevMar_maxsteps = maxsteps # Maximum number of steps in each fit
149
+ self.LevMar_timelimit = timelimit # Maximum time in seconds for total execution
150
+ self.LevMar_bumperpower = bumperpower # Repulsion factor of bumpers
151
+
152
+ def LevMar(self,strmodel, parameters_file = None, parameters = None):
153
+ if(not os.path.exists(self.eventname + '/' + self.inidir)):
154
+ os.makedirs(self.eventname + '/' + self.inidir)
155
+ if(parameters != None):
156
+ parameters_file = self.eventname + '/' + self.inidir + '/parameters.ini'
157
+ with open(parameters_file,'w') as f:
158
+ line =''
159
+ for fl in parameters:
160
+ line = line + str(fl) + ' '
161
+ f.write(line)
162
+ with open(self.eventname + '/' + self.inidir + '/LevMar.ini','w') as f:
163
+ f.write('nfits = ' + str(self.LevMar_nfits) + '\n')
164
+ f.write('offsetdegeneracy = ' + str(self.LevMar_offsetdegeneracy) + '\n')
165
+ f.write('maxsteps = ' + str(self.LevMar_maxsteps) + '\n')
166
+ f.write('timelimit = ' + str(self.LevMar_timelimit) + '\n')
167
+ f.write('bumperpower = ' + str(self.LevMar_bumperpower) + '\n')
168
+ if(parameters_file != None):
169
+ f.write('parametersfile = ' + parameters_file)
170
+ print('- Launching: LevMar')
171
+ print(' Fitting ' + strmodel + ' ...')
172
+ completedprocess = subprocess.run([self.bindir+self.levmarexe,self.eventname, strmodel,self.satellitedir], cwd = self.bindir, shell = False, stdout=subprocess.DEVNULL)
173
+ if(completedprocess.returncode != 0):
174
+ print('! Error in fit!')
175
+ self.done = True
176
+ else:
177
+ print(' OK')
178
+
179
+ def launch_fits(self,modelcode):
180
+ if(not os.path.exists(self.eventname + '/' + self.inidir)):
181
+ os.makedirs(self.eventname + '/' + self.inidir)
182
+ with open(self.eventname + '/' + self.inidir + '/LevMar.ini','w') as f:
183
+ f.write('nfits = ' + str(self.LevMar_nfits) + '\n')
184
+ f.write('offsetdegeneracy = ' + str(self.LevMar_offsetdegeneracy) + '\n')
185
+ f.write('maxsteps = ' + str(self.LevMar_maxsteps) + '\n')
186
+ f.write('timelimit = ' + str(self.LevMar_timelimit) + '\n')
187
+ f.write('bumperpower = ' + str(self.LevMar_bumperpower) + '\n')
188
+ stringfits = {'PS' : '- Single-lens-Single-source fits',
189
+ 'PX' : '- Single-lens-Single-source fits with parallax',
190
+ 'BS' : '- Single-lens-Binary-source fits',
191
+ 'BO' : '- Single-lens-Binary-source fits with xallarap',
192
+ 'LS' : '- Binary-lens-Single-source fits',
193
+ 'LX' : '- Binary-lens-Single-source fits with parallax',
194
+ 'LO' : '- Binary-lens-Single-source fits with orbital motion',
195
+ 'LK' : '- Binary-lens-Single-source fits with eccentric orbital motion',
196
+ 'TS' : '- Triple-lens-Single-source fits',
197
+ 'TX' : '- Triple-lens-Single-source fits with parallax'}
198
+ print(stringfits[modelcode])
199
+ initcondfile = self.eventname + '/InitCond/' + 'InitCond'+ modelcode + '.txt'
200
+ if(os.path.exists(initcondfile)):
201
+ with open(self.eventname + '/InitCond/' + 'InitCond'+ modelcode + '.txt') as f:
202
+ line = f.readline().split()
203
+ npeaks = int(line[0])
204
+ ninitconds = int(line[1])
205
+ processes = []
206
+ procnumbers = []
207
+ procepochs = []
208
+ iinitcond = 0
209
+ finitcond = 0
210
+ finitcondold = -1
211
+ pbar = tqdm(total = ninitconds,desc = 'Fits completed',file=sys.stdout, colour='GREEN', smoothing = 0)
212
+ while(finitcond < ninitconds):
213
+ i=0
214
+ while i < len(processes):
215
+ if(time.time() - procepochs[i] > self.LevMar_timelimit):
216
+ processes[i].kill()
217
+ premodfiles = glob.glob(self.eventname +'/PreModels/*.txt')
218
+ strmodel = modelcode + '{:0>4}'.format(str(procnumbers[i]))
219
+ with open(self.eventname +'/PreModels/' + strmodel + '/t' + strmodel + '.dat','w') as f:
220
+ f.write(f'{len(premodfiles)} {self.LevMar_nfits}')
221
+ if(processes[i].poll() != None):
222
+ processes.pop(i)
223
+ procnumbers.pop(i)
224
+ procepochs.pop(i)
225
+ finitcond += 1
226
+ else:
227
+ i += 1
228
+ while(iinitcond < ninitconds and len(processes) < self.nprocessors):
229
+ strmodel = modelcode + '{:0>4}'.format(str(iinitcond))
230
+ if(glob.glob(self.eventname +'/PreModels/' + strmodel + '/t' + strmodel + '.dat')==[]):
231
+ processes.append(subprocess.Popen([self.bindir+self.levmarexe,self.eventname, strmodel,self.satellitedir], cwd = self.bindir, shell = False, stdout=subprocess.DEVNULL))
232
+ procnumbers.append(iinitcond)
233
+ procepochs.append(time.time())
234
+ else:
235
+ finitcond += 1
236
+ iinitcond += 1
237
+ if(finitcond != finitcondold):
238
+ #print(' Fits launched: {}; completed: {}/{}'.format(iinitcond, finitcond, ninitconds))
239
+ pbar.update(finitcond - max(finitcondold,0))
240
+ finitcondold =finitcond
241
+ time.sleep(0.1)
242
+ pbar.close()
243
+ else:
244
+ print('- No initial conditions for this category')
245
+
246
+ def config_ModelSelector(self, sigmasoverlap = 3.0, sigmachisquare = 1.0, maxmodels = 10):
247
+ self.ModelSelector_sigmasoverlap = sigmasoverlap # factor multiplying the inverse covariance in search for superpositions (models are incompatible if farther than sigmasoverlap*sigma)
248
+ self.ModelSelector_sigmachisquare = sigmachisquare # number of sigmas in the chi square distribution for accepting alternative models after the best one
249
+ self.ModelSelector_maxmodels = maxmodels # maximum number of models returned
250
+
251
+ def ModelSelector(self, modelcode):
252
+ if(not os.path.exists(self.eventname + '/' + self.inidir)):
253
+ os.makedirs(self.eventname + '/' + self.inidir)
254
+ with open(self.eventname + '/' + self.inidir + '/ModelSelector.ini','w') as f:
255
+ f.write('sigmasoverlap = ' + str(self.ModelSelector_sigmasoverlap) + '\n')
256
+ f.write('sigmachisquare = ' + str(self.ModelSelector_sigmachisquare) + '\n')
257
+ f.write('maxmodels = ' + str(self.ModelSelector_maxmodels) + '\n')
258
+ stringmodels = {'PS' : '- Selecting models for Single-lens-Single-source fits',
259
+ 'PX' : '- Selecting models for Single-lens-Single-source fits with parallax',
260
+ 'BS' : '- Selecting models for Single-lens-Binary-source fits',
261
+ 'BO' : '- Selecting models for Single-lens-Binary-source fits with xallarap',
262
+ 'LS' : '- Selecting models for Binary-lens-Single-source fits',
263
+ 'LX' : '- Selecting models for Binary-lens-Single-source fits with parallax',
264
+ 'LO' : '- Selecting models for Binary-lens-Single-source fits with orbital motion',
265
+ 'LK' : '- Selecting models for Binary-lens-Single-source fits with eccentric orbital motion',
266
+ 'TS' : '- Selecting models for Triple-lens-Single-source fits',
267
+ 'TX' : '- Selecting models for Triple-lens-Single-source fits with parallax'}
268
+ print(stringmodels[modelcode])
269
+ completedprocess = subprocess.run([self.bindir+self.modelselectorexe,self.eventname, modelcode], cwd = self.bindir, shell = False, stdout=subprocess.DEVNULL)
270
+ if(completedprocess.returncode != 0):
271
+ print('! Error in model selection!')
272
+ self.done = True
273
+ else:
274
+ print(' OK')
275
+
276
+ def Finalizer(self):
277
+ print('- Launching: Finalizer')
278
+ print(' Making final assessment for this event')
279
+ completedprocess = subprocess.run([self.bindir+self.finalizerexe,self.eventname], cwd = self.bindir, shell = False, stdout=subprocess.DEVNULL)
280
+ if(completedprocess.returncode != 0):
281
+ print('! Error in finalization. Maybe there are problems with models')
282
+ self.done = True
283
+ else:
284
+ with open(self.eventname + '/Nature.txt') as f:
285
+ for line in f.readlines():
286
+ print(" " + line,end='')
287
+ print(" OK")
288
+
289
+ def run(self, event = None, cleanup = False):
290
+ phase =0
291
+ if(event!= None):
292
+ self.eventname = os.path.realpath(event)
293
+ self.done = False
294
+ print("o " + time.asctime())
295
+ while not(self.done):
296
+ # Check that event directory exists
297
+ if phase == 0:
298
+ if(os.path.exists(self.eventname + '/Data')):
299
+ print('- Analyzing event: ',self.eventname)
300
+ phase = 1
301
+ else:
302
+ print('! Event data for ' + self.eventname + ' not found !')
303
+ self.done = True
304
+ # Launch Reader
305
+ elif phase == 1:
306
+ self.Reader()
307
+ print("o " + time.asctime())
308
+ phase = 2
309
+ # Launch InitCond
310
+ elif phase == 2:
311
+ self.InitCond()
312
+ print("o " + time.asctime())
313
+ phase = 3
314
+ # Launch Finalizer
315
+ elif phase == self.endphase:
316
+ self.Finalizer()
317
+ print("o " + time.asctime())
318
+ phase += 1
319
+ # Conclude analysis
320
+ elif phase > self.endphase:
321
+ if(cleanup):
322
+ print('- Cleaning up preliminary models')
323
+ cleanup_preliminary_models()
324
+ print("- Analysis of " + self.eventname + " successfully completed!")
325
+ print("o " + time.asctime())
326
+ self.done = True
327
+ # Launch LevMar for next class
328
+ elif phase%2 == 1:
329
+ if(self.InitCond_modelcategories == None or self.modelcodes[phase//2-1] in self.InitCond_modelcategories):
330
+ self.launch_fits(self.modelcodes[phase//2-1])
331
+ print("o " + time.asctime())
332
+ phase += 1
333
+ # Launch ModelSelector for this class
334
+ else:
335
+ if(self.InitCond_modelcategories == None or self.modelcodes[phase//2-2] in self.InitCond_modelcategories):
336
+ self.ModelSelector(self.modelcodes[phase//2-2])
337
+ print("o " + time.asctime())
338
+ phase += 1
339
+
340
+ def cleanup_preliminary_models(self):
341
+ os.chdir(self.eventname)
342
+ if(os.path.exists('PreModels')):
343
+ shutil.rmtree('PreModels')
344
+
345
+ def archive_run(self, destination = None):
346
+ olddir = os.getcwd()
347
+ os.chdir(self.eventname)
348
+ if(os.path.exists('LCToFit.txt')):
349
+ previousrunslist = glob.glob('run-*')
350
+ previousrunslist.sort()
351
+ if(destination == None):
352
+ if(len(previousrunslist)>0):
353
+ lastrun = int(previousrunslist[-1].split('-')[-1])
354
+ else:
355
+ lastrun = 0
356
+ rundir = 'run-' + str(lastrun+1). zfill(4)
357
+ else:
358
+ rundir = destination
359
+ alllist = glob.glob('*')
360
+ alllist.remove('Data')
361
+ filelist = list(set(alllist) - set(previousrunslist))
362
+ os.mkdir(rundir)
363
+ shutil.copytree('Data',rundir+'/Data')
364
+ for nam in filelist:
365
+ shutil.move(nam,rundir)
366
+ os.chdir(olddir)
367
+
368
+ def recover_options(self,run = None):
369
+ if(self.eventname == None):
370
+ print('! No event chosen')
371
+ if(run!=None):
372
+ pathname = run
373
+ else:
374
+ pathname = self.eventname
375
+ if(not(os.path.exists(pathname))):
376
+ print("Invalid path!")
377
+ return
378
+ if(os.path.exists(pathname + '/' + self.inidir + '/Constraints.ini')):
379
+ with open(pathname + '/' + self.inidir + '/Constraints.ini','r') as f:
380
+ lines = f.read().splitlines()
381
+ print('Constraints --- ',lines)
382
+ self.constraints =[]
383
+ for line in lines:
384
+ chunks =line.split()
385
+ self.constraints.append([chunks[0], float(chunks[2]), float(chunks[3]), float(chunks[4])])
386
+ self.set_constraints(self.constraints)
387
+ if(os.path.exists(pathname + '/' + self.inidir + '/Reader.ini')):
388
+ with open(pathname + '/' + self.inidir + '/Reader.ini','r') as f:
389
+ lines = f.read().splitlines()
390
+ print('Reader --- ',lines)
391
+ for line in lines:
392
+ chunks = line.split()
393
+ if(chunks[0]=='tau'):
394
+ self.Reader_tau = float(chunks[2])
395
+ elif(chunks[0]=='binning'):
396
+ self.Reader_binning = int(chunks[2])
397
+ elif(chunks[0]=='otherseasons'):
398
+ self.Reader_otherseasons = int(chunks[2])
399
+ elif(chunks[0]=='renormalize'):
400
+ self.Reader_renormalize = int(chunks[2])
401
+ elif(chunks[0]=='thresholdoutliers'):
402
+ self.Reader_thresholdoutliers = float(chunks[2])
403
+ if(os.path.exists(pathname + '/' + self.inidir + '/InitCond.ini')):
404
+ with open(pathname + '/' + self.inidir + '/InitCond.ini','r') as f:
405
+ lines = f.read().splitlines()
406
+ print('InitCond --- ',lines)
407
+ self.InitCond_nostatic = False
408
+ self.InitCond_onlyorbital = False
409
+ self.InitCond_override = None
410
+ self.InitCond_onlyupdate = False
411
+ for line in lines:
412
+ chunks = line.split()
413
+ if(chunks[0]=='npeaks'):
414
+ self.InitCond_npeaks = int(chunks[2])
415
+ elif(chunks[0]=='peakthreshold'):
416
+ self.InitCond_peakthreshold = float(chunks[2])
417
+ elif(chunks[0]=='oldmodels'):
418
+ self.InitCond_oldmodels = int(chunks[2])
419
+ elif(chunks[0]=='usesatellite'):
420
+ self.InitCond_usesatellite = int(chunks[2])
421
+ elif(chunks[0]=='nostatic'):
422
+ self.InitCond_nostatic = (int(chunks[2])!=0)
423
+ elif(chunks[0]=='onlyorbital'):
424
+ self.InitCond_onlyorbital = (int(chunks[2])!=0)
425
+ elif(chunks[0]=='onlyupdate'):
426
+ self.InitCond_onlyupdate = (int(chunks[2])!=0)
427
+ elif(chunks[0]=='override'):
428
+ self.InitCond_override = (float(chunks[2]),float(chunks[3]))
429
+ elif(chunks[0]=='templatelibrary'):
430
+ self.InitCond_templatelibrary = chunks[2]
431
+ elif(chunks[0]=='modelcategories'):
432
+ self.InitCond_modelcategories = [chunks[2][i:i+2] for i in range(0, len(chunks[2]), 2)]
433
+ if(os.path.exists(pathname + '/' + self.inidir + '/LevMar.ini')):
434
+ with open(pathname + '/' + self.inidir + '/LevMar.ini','r') as f:
435
+ lines = f.read().splitlines()
436
+ print('LevMar --- ',lines)
437
+ for line in lines:
438
+ chunks = line.split()
439
+ if(chunks[0]=='nfits'):
440
+ self.LevMar_nfits = int(chunks[2])
441
+ if(chunks[0]=='offsetdegeneracy'):
442
+ self.LevMar_offsetdegeneracy = int(chunks[2])
443
+ elif(chunks[0]=='maxsteps'):
444
+ self.LevMar_maxsteps = int(chunks[2])
445
+ elif(chunks[0]=='timelimit'):
446
+ self.LevMar_timelimit = float(chunks[2])
447
+ elif(chunks[0]=='bumperpower'):
448
+ self.LevMar_bumperpower = float(chunks[2])
449
+ if(os.path.exists(pathname + '/' + self.inidir + '/ModelSelector.ini')):
450
+ with open(pathname + '/' + self.inidir + '/ModelSelector.ini','r') as f:
451
+ lines = f.read().splitlines()
452
+ print('ModelSelector --- ',lines)
453
+ for line in lines:
454
+ chunks = line.split()
455
+ if(chunks[0]=='sigmasoverlap'):
456
+ self.ModelSelector_sigmasoverlap = float(chunks[2])
457
+ elif(chunks[0]=='sigmachisquare'):
458
+ self.ModelSelector_sigmachisquare = float(chunks[2])
459
+ elif(chunks[0]=='maxmodels'):
460
+ self.ModelSelector_maxmodels = int(chunks[2])
461
+
462
+ @staticmethod
463
+ def find_bin_directory() -> str:
464
+ """
465
+ Searches for the RTModel/bin directory in the available site-packages directories.
466
+
467
+ :return: The string of the parth to the bin directory.
468
+ """
469
+ bin_directory_string = None
470
+ site_packages_directories = site.getsitepackages()
471
+ site_packages_directories.append(site.getusersitepackages())
472
+ for site_packages_directory in site_packages_directories:
473
+ bin_directory_path = Path(site_packages_directory).joinpath('RTModel/bin')
474
+ if bin_directory_path.exists():
475
+ bin_directory_string = str(bin_directory_path) + '/'
476
+ break
477
+ if bin_directory_string is None:
478
+ raise FileNotFoundError(f'RTModel binary directory not found. Searched {site_packages_directories} '
479
+ f'site-packages directories.')
480
+ return bin_directory_string
RTModel/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ __version__ = "2.2"
2
+ __author__ = 'Valerio Bozza'
3
+ __credits__ = 'University of Salerno, Italy'
4
+
5
+ from .RTModel import RTModel
Binary file
Binary file
RTModel/bin/LevMar.exe ADDED
Binary file
Binary file
RTModel/bin/Reader.exe ADDED
Binary file
RTModel/data/ESPL.tbl ADDED
Binary file
@@ -0,0 +1,59 @@
1
+ 113
2
+ 0.7 0.5 0.1 1.35 0.01 0.07511 1.12127
3
+ 0.7 0.1 0.1 4.6 0.01 -0.00676 0.1493
4
+ 0.7 0.1 0.75 4.6 0.01 -0.74331 0.53977
5
+ 0.9 0.03 -0.15 2.0 0.01 0.06576 0.09019
6
+ 0.9 0.03 -0.2 2.3 0.01 -0.47211 0.01011
7
+ 1.5 0.5 0.1 3.2 0.01 -0.32531 0.63255
8
+ 1.5 0.5 0.3 2.8 0.01 0.58817 0.78162
9
+ 0.9 0.03 -0.3 1.8 0.01 -0.28464 0.14202
10
+ 1.5 0.5 0.3 2.9 0.01 -0.48885 0.56195
11
+ 1.5 0.5 0.3 2.9 0.01 0.39245 0.56195
12
+ 1.5 0.5 0.25 2.75 0.01 0.66775 0.73845
13
+ 1.5 0.5 0.35 2.2 0.01 0.06007 0.6458
14
+ 1.5 0.5 0.35 2.2 0.01 0.5378 0.6458
15
+ 1.5 0.5 0.25 2.5 0.01 -0.45776 0.09626
16
+ 1.5 0.5 0.25 2.5 0.01 -0.45776 0.58665
17
+ 1.5 0.5 0.25 2.5 0.01 0.09626 0.58665
18
+ 1.5 0.5 0.25 2.0 0.01 -0.04058 0.2958
19
+ 1.5 0.5 0.25 2.0 0.01 -0.04058 0.47182
20
+ 1.5 0.5 0.25 2.0 0.01 0.2958 0.47182
21
+ 1.5 0.5 0.7 1.5 0.01 -0.07965 -0.01574
22
+ 1.5 0.5 0.7 1.5 0.01 -0.07965 0.34594
23
+ 1.5 0.5 0.7 1.5 0.01 -0.01574 0.34594
24
+ 1.5 0.5 0.45 4.6 0.01 -0.40928 0.27708
25
+ 1.5 0.5 0.45 4.6 0.01 -0.04946 0.27708
26
+ 1.5 0.5 0.33 3.2 0.01 -0.36736 0.65181
27
+ 0.9 0.1 -0.45 1.65 0.01 -0.4663 -0.00103
28
+ 0.9 0.1 -0.45 1.65 0.01 -0.4663 0.37168
29
+ 0.9 0.1 -0.45 1.65 0.01 -0.00103 0.37168
30
+ 2.2 0.5 0.1 3.0 0.01 0.74611 1.18948
31
+ 2.2 0.5 0.1 3.0 0.01 1.06918 1.18948
32
+ 2.2 0.5 0.35 2.9 0.01 1.07297 1.14242
33
+ 2.2 0.5 0.35 2.9 0.01 1.07297 1.28772
34
+ 2.2 0.5 0.35 2.9 0.01 1.14242 1.28772
35
+ 2.2 0.5 0.4 2.8 0.01 0.98474 1.16883
36
+ 2.2 0.5 0.4 2.8 0.01 0.98474 1.21577
37
+ 2.2 0.5 0.4 2.8 0.01 1.16883 1.21577
38
+ 2.2 0.5 1.05 2.0 0.01 0.39398 0.56261
39
+ 2.2 0.5 1.05 2.0 0.01 0.39398 0.6566
40
+ 2.2 0.5 1.05 2.0 0.01 0.56261 0.6566
41
+ 2.2 0.5 0.45 2.7 0.01 -0.67542 0.89586
42
+ 2.2 0.5 0.45 2.7 0.01 -0.67542 1.10689
43
+ 2.2 0.5 0.45 2.7 0.01 0.89586 1.10689
44
+ 2.2 0.5 1.0 1.9 0.01 0.30045 0.39828
45
+ 2.2 0.5 1.0 1.9 0.01 0.30045 0.52621
46
+ 2.2 0.5 1.0 1.9 0.01 0.39828 0.52621
47
+ 2.2 0.5 1.23 1.6 0.01 -0.17028 0.01821
48
+ 2.2 0.5 1.23 1.6 0.01 -0.17028 0.05286
49
+ 2.2 0.5 1.23 1.6 0.01 0.01821 0.05286
50
+ 2.2 0.5 0.35 2.7 0.01 -0.66716 0.7449
51
+ 2.2 0.5 0.35 2.7 0.01 -0.66716 1.14629
52
+ 2.2 0.5 0.35 2.7 0.01 0.7449 1.14629
53
+ 2.2 0.5 0.05 2.95 0.01 -0.64485 0.23071
54
+ 2.2 0.5 0.05 2.95 0.01 -0.64485 1.20146
55
+ 2.2 0.5 0.05 2.95 0.01 0.23071 1.20146
56
+ 2.2 0.5 0.35 3.0 0.01 -0.68509 1.15393
57
+ 1.8 0.03 1.0 2.3 0.01 -0.08517 0.72053
58
+ 1.8 0.03 1.0 2.3 0.01 -0.08517 0.88924
59
+ 1.8 0.03 1.0 2.3 0.01 0.72053 0.88924
@@ -0,0 +1,91 @@
1
+ // LevMarFit.h
2
+ // Definition of the LevMar and bumper classes for Levenberg-Marquardt fitting
3
+
4
+ #include <cstdio>
5
+ #include "bumper.h"
6
+ #include <regex>
7
+ #include <filesystem>
8
+
9
+ using namespace std;
10
+ using namespace std::filesystem;
11
+
12
+ #ifndef _LevMarFit
13
+ #define _LevMarFit
14
+ #define __unmanaged
15
+
16
+ #include <VBMicrolensingLibrary.h>
17
+
18
+ class LevMar {
19
+
20
+ VBMicrolensing* VBM;
21
+
22
+ char eventname[512], filename[30], filnum[10], outdir[30], satellitedir[256];
23
+ char modelcode[16];
24
+ int error;
25
+ int flagblending;
26
+ path exedir;
27
+
28
+ double tim0, tm;
29
+
30
+ double (VBMicrolensing::* model)(double*, double);
31
+ int nps;
32
+ double* sigmapr, * leftlim, * rightlim;
33
+
34
+ int* filter, * satel, nfil, np, OGLE;
35
+ double* t, * y, * w, * delta, * maxdelta, * Curv, * A, * B, * B0, * Cov, * fb, ** Gr, * dFdp, * errs;
36
+ double* pr, * prn, * sumy, * sumy2, * sumsigma, * sumfy, * sumf, * sumf2, * limbdarks;
37
+
38
+ int consnumber, *consindex;
39
+ double* constraints, * consleft, * consright, *consvars;
40
+ int modnumber;
41
+
42
+ double Tol;
43
+
44
+ void (LevMar::* PrintOut)(double*);
45
+ void (LevMar::* PrintFile)(FILE*, double, bool);
46
+
47
+ bumper* stepchain, * bumperlist, * laststep;
48
+
49
+
50
+ public:
51
+ LevMar(int, char**);
52
+ ~LevMar();
53
+
54
+ void ReadFiles(int, char**);
55
+ int InitCond(double* presigmapr, double* preleftlim, double* prerightlim);
56
+ void ReadCurve();
57
+ void ReadOptions();
58
+ void Run();
59
+ double ChiSquared(double*);
60
+ void Grad();
61
+ void Covariance();
62
+ double ComputeConstraint(double *pr, int i);
63
+
64
+ void PrintOutPS(double*);
65
+ void PrintOutPX(double*);
66
+ void PrintOutBS(double*);
67
+ void PrintOutBO(double*);
68
+ void PrintOutLS(double*);
69
+ void PrintOutLX(double*);
70
+ void PrintOutLO(double*);
71
+ void PrintOutLK(double*);
72
+ void PrintOutTS(double*);
73
+ void PrintOutTX(double*);
74
+
75
+ void PrintFilePS(FILE*, double, bool);
76
+ void PrintFilePX(FILE*, double, bool);
77
+ void PrintFileBS(FILE*, double, bool);
78
+ void PrintFileBO(FILE*, double, bool);
79
+ void PrintFileLS(FILE*, double, bool);
80
+ void PrintFileLX(FILE*, double, bool);
81
+ void PrintFileLO(FILE*, double, bool);
82
+ void PrintFileLK(FILE*, double, bool);
83
+ void PrintFileTS(FILE*, double, bool);
84
+ void PrintFileTX(FILE*, double, bool);
85
+
86
+ };
87
+
88
+ double Determinant(double*, int);
89
+ void Inverse(double*, double*, int);
90
+
91
+ #endif