def test_of_fa_location_in_different_units(self): """Testing location of value in different units """ ta = TimeAxis(0.0, 1000, 0.1) ta.atype='complete' wa = ta.get_FrequencyAxis() with energy_units("1/cm"): val = 10000.0 nsni = numpy.int(numpy.floor((val-wa.start)/wa.step)) i1 = wa.locate(10000.0) self.assertEqual(nsni, i1[0]) #with h5py.File("test_file_ValueAxes",driver="core", # backing_store=False) as f: with tempfile.TemporaryFile() as f: wa.save(f, test=True) tb = FrequencyAxis() tb = tb.load(f, test=True) numpy.testing.assert_array_equal(wa.data,tb.data)
def test_of_fa_location_in_different_units(self): """Testing location of value in different units """ ta = TimeAxis(0.0, 1000, 0.1) ta.atype = 'complete' wa = ta.get_FrequencyAxis() with energy_units("1/cm"): val = 10000.0 nsni = numpy.int(numpy.floor((val - wa.start) / wa.step)) i1 = wa.locate(10000.0) self.assertEqual(nsni, i1[0]) #with h5py.File("test_file_ValueAxes",driver="core", # backing_store=False) as f: with tempfile.TemporaryFile() as f: wa.save(f, test=True) tb = FrequencyAxis() tb = tb.load(f, test=True) numpy.testing.assert_array_equal(wa.data, tb.data)
def test_of_frequency_axis_creation(self): """Testing FrequencyAxis creation """ wa = FrequencyAxis(0.0, 1000, 0.1) self.assertEqual(wa.min, wa.data[0]) self.assertEqual(wa.max, wa.data[len(wa.data) - 1])
def setUp(self, verbose=False): abss = AbsSpectrumBase() with energy_units("1/cm"): f = FrequencyAxis(10000.0, 2000, 1.0) a = self._spectral_shape(f, [1.0, 11000.0, 100.0]) abss.axis = f abss.data = a self.abss = abss self.axis = abss.axis time = TimeAxis(0.0, 1000, 1.0) with energy_units("1/cm"): mol1 = Molecule(elenergies=[0.0, 12000.0]) params = dict(ftype="OverdampedBrownian", reorg=20, cortime=100, T=300) mol1.set_dipole(0, 1, [0.0, 1.0, 0.0]) cf = CorrelationFunction(time, params) mol1.set_transition_environment((0, 1), cf) abs_calc = AbsSpectrumCalculator(time, system=mol1) abs_calc.bootstrap(rwa=12000) abs1 = abs_calc.calculate() self.abs1 = abs1
def setUp(self, verbose=False): abss = AbsSpectrumBase() with energy_units("1/cm"): f = FrequencyAxis(10000.0, 2000, 1.0) a = self._spectral_shape(f, [1.0, 11000.0, 100.0]) abss.axis = f abss.data = a self.abss = abss self.axis = abss.axis
def upper_half_frequency_axis(self): for row in self.hashes: start = float(row['start']) Ns = int(row['number_of_steps']) dw = float(row['step']) print("FrequencyAxis", start, Ns, dw, "atype=upper-half") world.wa = FrequencyAxis(start, Ns, dw, atype="upper-half") tc = TestCase() tc.assertEqual(world.wa.atype, "upper-half")
def complete_frequency_axis(self): for row in self.hashes: start = float(row['start']) Ns = int(row['number_of_steps']) dw = float(row['step']) print("FrequencyAxis", start, Ns, dw, "atype='complete'") world.wa = FrequencyAxis(start, Ns, dw) tc = TestCase() tc.assertEqual(world.wa.atype, "complete")
def setUp(self, verbose=False): lindichs = LinDichSpectrumBase() with energy_units("1/cm"): f = FrequencyAxis(10000.0, 2000, 1.0) a = self._spectral_shape(f, [1.0, 11000.0, 100.0]) lindichs.axis = f lindichs.data = a self.lindichs = lindichs self.axis = lindichs.axis #make a single-molecule system time = TimeAxis(0.0, 1000, 1.0) self.ta = time with energy_units("1/cm"): mol1 = Molecule(elenergies=[0.0, 12000.0]) mol2 = Molecule(elenergies=[0.0, 12000.0]) params = dict(ftype="OverdampedBrownian", reorg=20, cortime=100, T=300) mol1.set_dipole(0, 1, [0.0, 1.0, 0.0]) mol2.set_dipole(0, 1, [0.0, 1.0, 0.0]) cf = CorrelationFunction(time, params) mol1.set_transition_environment((0, 1), cf) mol2.set_transition_environment((0, 1), cf) mol1.position = [0, 0, 0] mol2.position = [10, 10, 10] agg = Aggregate(name="tester_dim", molecules=[mol1, mol2]) agg.build() lindich_calc = LinDichSpectrumCalculator(time, system=agg, \ vector_perp_to_membrane=numpy.array((0,1,0))) lindich_calc.bootstrap(rwa=12000) lindich1 = lindich_calc.calculate() self.lindich1 = lindich1
def test_dfunction_saveable(self): """Testing if DFunction is saveable """ wa = FrequencyAxis(0.0, 1000, 0.1) fw = numpy.exp(-wa.data) fce = DFunction(wa, fw) #fce.plot() with h5py.File("test_file_1", driver="core", backing_store=False) as f: fce.save(f, test=True) fce2 = DFunction() fce2.load(f, test=True) #fce2.plot() numpy.testing.assert_array_equal(fce.data, fce2.data)
def test_dfunction_saveable_2(self): """Testing if DFunction can save spline initiated object correctly """ wa = FrequencyAxis(0.0, 100, 1.0) fw = numpy.exp(-wa.data/30.0) fce = DFunction(wa,fw) val_mez_linear = fce.at(20.5) val_mez_spline = fce.at(20.5, approx="spline") # print("init", fce._splines_initialized) # print(val_mez_linear) # print(val_mez_spline) #with h5py.File("test_file_1",driver="core", # backing_store=False) as f: with tempfile.TemporaryFile() as f: fce.save(f, test=True) fce2 = DFunction() fce2 = fce2.load(f, test=True) self.assertEqual(fce._splines_initialized, True) self.assertEqual(fce2._splines_initialized, True) new_mez_linear = fce2.at(20.5) new_mez_spline = fce2.at(20.5, approx="spline") # print(new_mez_linear) # print(new_mez_spline) self.assertEqual(val_mez_spline, new_mez_linear) self.assertEqual(val_mez_spline, new_mez_spline) self.assertEqual(fce._splines_initialized, fce2._splines_initialized) numpy.testing.assert_array_equal(fce.data, fce2.data)
def main(): ############################################################################### # # Parsing command line arguments # ############################################################################### parser = argparse.ArgumentParser( description='Loads and manupilates spectra.') # # # parser.add_argument("files", metavar='file', type=str, nargs='+', help='files to be processed') parser.add_argument("-o", "--outfile", metavar="outfile", help="output file", default="tot.dat") #parser.add_argument('--sum', dest='accumulate', action='store_const', # const=sum, default=max, # help='sum the integers (default: find the max)') parser.add_argument("-op", "--operation", default="sum", choices=["sum", "norm", "max", "convert"], help="operation to be performed on the spectra") parser.add_argument("-t", "--type", default="abs", help="type of spectra", choices=["abs", "2d"]) parser.add_argument("-n", "--nosplash", action="store_true", help="no script info (a 'spash screen')") # # # parser.add_argument("-e", "--exe", help="prints out the location of python" + " executable", action="store_true") # # # args = parser.parse_args() ############################################################################### # # # Results of command line parsing # # ############################################################################### files = args.files oper = args.operation stype = args.type show_exe = args.exe splash = not args.nosplash ############################################################################### # # # "Splash screen" # # ############################################################################### term = Terminal() t_height = term.height t_width = term.width class StarLine: def __init__(self, term): self.term = term def full(self): print('*' * self.term.width) def empty(self): print('*' + (' ' * (self.term.width - 2)) + '*') def center_text(self, text, bold=False): tlen = len(text) spaces = self.term.width - tlen - 2 lspaces = spaces // 2 last = self.term.width - (tlen + 2 + 2 * lspaces) rspaces = lspaces + last if bold: tx = '*' + (' ' * lspaces) + self.term.bold(text) + ( ' ' * rspaces) + '*' else: tx = '*' + (' ' * lspaces) + text + (' ' * rspaces) + '*' print(tx) def left_text(self, text): tlen = len(text) spaces = self.term.width - tlen - 2 print('*' + text + (' ' * spaces) + '*') indent = 4 if splash: star_line = StarLine(term) print("") star_line.full() star_line.empty() star_line.center_text("lams (Loading and manipulating spectra)" + " command line tool", bold=True) star_line.center_text("Based on Quantarhei package " + "(github.com/tmancal74/quantarhei)") star_line.empty() star_line.left_text(" Author: Tomas Mancal") star_line.left_text(" Charles University") star_line.left_text(" Email : [email protected] ") star_line.empty() star_line.left_text(" try 'lams -h' for basic usage information") star_line.empty() star_line.full() print("") if show_exe: with term.location(indent, term.height - 1): print("Location of Python executable: ", sys.executable, "\n") quit() with term.location(indent, term.height - 1): print("Lams task summary:") with term.location(indent, term.height - 1): print(oper, stype, ": outfile =", args.outfile) print("") fname, ext = os.path.splitext(args.outfile) ext = ext[1:] ############################################################################### # # Identification of the files to work on # ############################################################################### files_abs = [] for aaa in files: faaa = glob.glob(aaa) for f in faaa: files_abs.append(f) abs_sum = (stype == "abs") and (oper == "sum") abs_norm = (stype == "abs") and (oper == "norm") abs_max = (stype == "abs") and (oper == "max") abs_conv = (stype == "abs") and (oper == "convert") if abs_sum: with term.location(indent, term.height - 1): print("Summing absorption spectra") k = 0 spect = None for fl in files_abs: a = AbsSpectrumBase() a.load(fl) # # sum them up # if spect is None: spect = a else: spect.add_to_data(a) k += 1 spect.save(fname, ext=ext) with term.location(indent, term.height - 1): print("... finished with ", k, " files\n") if abs_norm: with term.location(indent, term.height - 1): print("Normalizing absorption spectra") k = 0 fl = files_abs[0] spect = AbsSpectrumBase() spect.load(fl) # # Normalize spectra # mx = numpy.max(spect.data) spect.data = spect.data / mx k += 1 spect.save(fname, ext=ext) with term.location(indent, term.height - 1): print("... finished with ", k, " files\n") if abs_max: with term.location(indent, term.height - 1): print("Looking for absorption maxima") k = 0 fl = files_abs[0] spect = AbsSpectrumBase() spect.load(fl) # # Find location of maximum # mxi = numpy.argmax(spect.data) #with energy_units("1/cm"): mx = spect.axis.data[mxi] k += 1 with term.location(indent, term.height - 1): print("{t.bold}Maximum at ".format(t=term), mx, "1/cm") with term.location(indent, term.height - 1): print("... finished with ", k, " files\n") if abs_conv: with term.location(indent, term.height - 1): print("Converting from nm to 1/cm") fl = files_abs[0] data = numpy.genfromtxt(fl, converters={0: lambda s: 1.0e7 / float(s)}) x = data[:, 0] ab = data[:, 1] _spline_r = scipy.interpolate.UnivariateSpline(x, ab, s=0) wa = FrequencyAxis(10000.0, 5000, 1.0) data = numpy.zeros(wa.data.shape) i = 0 for w in wa.data: data[i] = _spline_r(w) i += 1 spect = AbsSpectrumBase(axis=wa, data=data) spect.save(fname, ext=ext)