def test_cached_call(self): with temporaryworkingdirectory(): callvalue = cachedcall("key", self.createpurepython) readvalue = cacheget("key") self.assertEqual(callvalue, readvalue) self.assertEqual(callvalue, self.createpurepython())
def test_cached_get(self): with temporaryworkingdirectory(): writtenvalue = self.createpurepython() with Cache() as c: c["key"] = writtenvalue readvalue = cacheget("key") self.assertEqual(readvalue, writtenvalue)
def _runbonsai(g4file: str, config: GenerateMCConfig) -> str: bonsai_name = f"{g4file.replace('root_files', 'bonsai_root_files')}" with temporaryworkingdirectory(): os.symlink(expandpath(config.bonsailikelihood), f"{os.getcwd()}{os.sep}like.bin") if (not os.path.exists(bonsai_name)) and (g4file != bonsai_name): try: _log.info(f"Running bonsai to generate {bonsai_name}") subprocess.check_call( [expandpath(config.bonsaiexecutable), g4file, bonsai_name]) except subprocess.CalledProcessError: _log.error(f"bonsai failed to generate {bonsai_name}") return bonsai_name
def test_cached_callable(self): with temporaryworkingdirectory(): callcount = defaultdict(int) @cachedcallable(lambda x: f"x_is_{x}") def triple(x, callcount=callcount): callcount[x] += 1 return 3 * x result = (triple(1), triple(1), triple(2), triple(2)) self.assertEqual(result, (3, 3, 6, 6)) self.assertEqual(callcount, {1: 1, 2: 1})
def test_runall(): with temporaryworkingdirectory() as d: run( [ "python", "-m", "watchopticalmc", "--signal-only", "--num-events-per-job=10", "--num-jobs=1", "--client=single", ], check=True, ) path = Path(d) / "analysisdataset.pickle" assert AnalysisDataset.load(path) return
def test_zeroatt_gives_no_light(): with temporaryworkingdirectory() as dir: run([ "python", "-m", "watchopticalmc", "--num-jobs=1", "--num-events-per-job=10", "--attenuation=0.0", "--client=single", "--signal-only", f"--directory={dir}", ]) total_charge = np.sum( np.concatenate( uproot.lazyarray("watchopticalanalysis*.root", "watchopticalanalysis", "total_charge"))) assert total_charge < 10.0
def _convert_ratpacbonsai_to_analysis(g4file: str, bonsaifile: str, outname: str): with temporaryworkingdirectory() as tempdir: tempfilename = os.sep.join((tempdir, os.path.basename(outname))) convert_ratpacbonsai_to_analysis(g4file, bonsaifile, tempfilename) move(tempfilename, outname)