def test_div(self): with self.subTest(0): p = Path('/tmp') p = p / 'sub' self.assertEqual(p.route, '/tmp/sub') with self.subTest(1): p = Path('/tmp') p = p / 'sub/' self.assertEqual(p.route, '/tmp/sub')
def lm2bin(config, target): path_file = config['input']['listmode']['path_file'] data = load_h5(path_file) coo_data = np.hstack((data['fst'], data['snd'])) bin_data = np.hstack( (coo_data, data['weight'].reshape(data['weight'].size, 1))) out = bin_data.astype(np.float32) out.tofile((Path(target) + 'input.txt').abs) f = open((Path(target) + 'input.txt.hdr').abs, 'w') f.write(str(out.shape[0]))
def setUp(self): dpath = tempfile.mkdtemp() dpath1 = tempfile.mkdtemp() pdr = Path(dpath) pf1 = Directory(pdr / 'sub1') mkdir(pf1) pf2 = Directory(pdr / 'sub2') mkdir(pf2) pd6 = pdr / 'sub6' mkdir(Directory(pd6)) pd7 = Directory(pd6 / 'sub7') mkdir(pd7) self.root = dpath self.root1 = Path(dpath1)
def save_script(path, data): path = Path(path) if path.suffix == ' ': path = path + 'hs' target = path.abs with open(target, 'w') as fin: fin.write(data)
def save_script(path, data, suffix): path = Path(path) if path.suffix == '': path = path + suffix target = path.abs with open(target, 'w') as fout: fout.write(data)
def save_osem_result(save_path, dataset_prefix, source, nb_iterations, nb_subsets): with h5py.File(save_path.abs) as fout: for i in range(0, nb_iterations): for j in range(0, nb_subsets): label = dataset_prefix + 'output_' + str(i + 1) + '_' + str(j + 1) source_path = Path(source) + str(i + 1) + '_' + str(j + 1) + '.v' fout[label] = np.fromfile(source_path.abs, dtype='float32')
def generate_recon_script(config, target, source): with open(config, 'r') as fin: c = json.load(fin) if source is None: source = '.' recon_spec = generatereconspec(c, source) recon_script = render(ReconstructionSpecScript(recon_spec)) path_script = Path(target).abs + '/OSMAPOSL.par' # if path_script.suffix == ' ': # path_script = path_script+'.par' save_script(path_script, recon_script)
def test_parts(self): with self.subTest(0): p = Path('/tmp/base') self.assertEqual(p.parts(), ('/', 'tmp', 'base')) with self.subTest(1): p = Path('a/b') self.assertEqual(p.parts(), ('a', 'b'))
def save_result(config, source): part_config = config['algorithm']['recon'] nb_subsets, nb_subiterations = get_iter_info(part_config) algorithm = get_algorithm(part_config) save_path = Path(config['output']['image']['path_file']) dataset_prefix = config['output']['image']['path_dataset_prefix'] if algorithm == ('FBP2D' or 'FBP3DRP'): save_fbp_result(save_path, dataset_prefix, source) elif algorithm == 'MLEM': save_mlem_result(save_path, dataset_prefix, source, nb_subiterations) else: save_osem_result(save_path, dataset_prefix, source, nb_subiterations, nb_subsets)
def generatereconspec(config, path_header): path_header = Path(path_header + '/sinogram.hs') part_config = config['algorithm']['recon'] if ("osem" in part_config): nb_subsets = part_config['osem']['nb_subsets'] nb_subiterations = part_config['osem']['nb_iterations'] else: nb_subsets = 1 nb_subiterations = part_config['mlem']['nb_iterations'] return ReconstructionSpec(path_header.abs, config['output']['image']['grid'][0], nb_subsets, nb_subiterations)
def generate_data_and_header(config, target): with open(config, 'r') as fin: c = json.load(fin) scanner = get_scanner(c['scanner']['petscanner']) path_data = Path(target) gen_sino_script(c['scanner']['petscanner'], (path_data.abs + 'sinogram')) path_file = c['input']['listmode']['path_file'] data = load_h5(path_file) lor = List( LoR(PositionEvent(data['fst'][i, ]), PositionEvent(data['snd'][i, ]), weight=data['weight'][i]) for i in range(0, data['weight'].size)) lm2sino(scanner, lor, (path_data.abs + '/sinogram.s'))
def setUp(self): dpath = tempfile.mkdtemp() dpath1 = tempfile.mkdtemp() pdr = Path(dpath) pf1 = File(pdr / 'tmp.txt') touch(pf1) pf2 = File(pdr / 'tmp2.txt') touch(pf2) pf3 = File(pdr / 'tmp4.txt') touch(pf3) with open(str(pf3.path), 'w') as fout: print('test3', file=fout) self.root = dpath self.root1 = dpath1
def test_copy_file(self): file1 = File(self.root + '/tmp2.txt') cp(file1, Path(self.root1 + '/tmp2.txt')) file2 = File(self.root1 + '/tmp2.txt') assert file2.exists and file1.exists
def neg1(): path2 = Path(self.root + '/tmpx.txt') assert not File.is_file(path2)
def neg2(): path3 = Path(self.root + '/sub1') assert not File.is_file(path3)
def neg(): file1 = File((Path(self.root) / 'tmpx.txt').abs) assert not (file1.exists)
def pos(): path1 = Path(self.root + '/tmp.txt') assert File.is_file(path1)
def test_fatherpath(self,input,output): assert Path(input).father_path().route ==output
def pos(): file1 = File((Path(self.root) / 'tmp.txt').abs) assert (file1.exists)
def test_absolute(self,input,output): assert Path(input).absolute().route == output
def test_dot(self): self.assertEqual(Path('.').route, '')
def test_eq(self,xi,xo): assert Path(xi) == Path(xo)
def test_relative(self,input,output): assert Path(input).relative == output
def test_isabs(self,input,output): assert Path(input).isabs == output
def test_name(self, input, output): assert Path(input).name == output
def test_suffixes(self,input,output): assert Path(input).suffixes == output
def test_move_file(self): file1 = File(self.root + '/tmp2.txt') mv(file1, Path(self.root1 + '/tmp2.txt')) file2 = File(self.root1 + '/tmp2.txt') assert file2.exists and not file1.exists
def test_join_dot(self): self.assertEqual((Path('.') / 'a').route, 'a')
def test_isroot(self,input,output): assert Path(input).isroot == output
def test_father(self,input,output): assert Path(input).father == output