def read_and_write_cube(self, path): logger.info("Testing cube from path " + path) # read original cube and calculate hashsum c = CtxCube() c.read(path) _, data_file = CtxCube.parse_path(self.cube000) data_file_path = CtxCube.discover_file(data_file) if data_file_path.endswith(".gz"): f = gzip.open(data_file_path) else: f = open(data_file_path, 'rb') original_md5 = hashlib.md5(f.read()).hexdigest() f.close() # calculate temporary filename fd, outfile = tempfile.mkstemp() os.close(fd) os.remove(outfile) # we need only random name, not a descriptor logger.debug("Generated random file name " + outfile) # save cube and calculate hashsum c.write(outfile) # this will write outfile+".ctx" and outfile+".hed" f = open(outfile + ".ctx", 'rb') generated_md5 = hashlib.md5(f.read()).hexdigest() f.close() logger.debug("Removing " + outfile + ".ctx") os.remove(outfile + ".ctx") logger.debug("Removing " + outfile + ".hed") os.remove(outfile + ".hed") # compare checksums self.assertEqual(original_md5, generated_md5)
def load_from_dicom_thread(self, dicom, close=None): if dicom.has_key('images'): c = CtxCube() c.read_dicom(dicom) self.ct_images = CTImages(c) self.patient_name = c.patient_name self.structures = VoiCollection(self) if dicom.has_key('rtss'): structures = VdxCube("", c) structures.read_dicom(dicom) for voi in structures.vois: self.structures.add_voi(Voi(voi.get_name(), voi), 0) if not close is None: wx.CallAfter(close.close) wx.CallAfter(self.patient_load)
def load_from_voxelplan_thread(self, path, close=None): clean_path = os.path.splitext(path)[0] if os.path.exists(clean_path + ".ctx"): c = CtxCube() c.read(clean_path + ".ctx") self.ct_images = CTImages(c) else: raise InputError("No Images") self.structures = VoiCollection(self) if os.path.exists(clean_path + ".vdx"): structures = VdxCube("", c) structures.read(clean_path + ".vdx") for voi in structures.vois: self.structures.add_voi(Voi(voi.get_name(), voi), 0) self.patient_name = c.patient_name if not close is None: wx.CallAfter(close.close) wx.CallAfter(self.patient_load)
def read_and_write_cube(self, path): logger.info("Testing cube from path " + path) # read original cube and calculate hashsum c = CtxCube() c.read(path) # get path to the cube data file, extracting it from a partial path data_file_path = TRiP98FileLocator(self.cube000, CtxCube).datafile # get the hashsum if data_file_path.endswith(".gz"): f = gzip.open(data_file_path) else: f = open(data_file_path, 'rb') original_md5 = hashlib.md5(f.read()).hexdigest() f.close() # calculate temporary filename fd, outfile = tempfile.mkstemp() os.close(fd) os.remove(outfile) # we need only random name, not a descriptor logger.debug("Generated random file name " + outfile) # save cube and calculate hashsum saved_header_path, saved_cubedata_path = c.write( outfile) # this will write outfile+".ctx" and outfile+".hed" # check if generated files exists self.assertTrue(os.path.exists(saved_header_path)) self.assertTrue(os.path.exists(saved_cubedata_path)) # get checksum f = open(saved_cubedata_path, 'rb') generated_md5 = hashlib.md5(f.read()).hexdigest() f.close() logger.debug("Removing " + saved_cubedata_path) os.remove(saved_cubedata_path) logger.debug("Removing " + saved_header_path) os.remove(saved_header_path) # compare checksums self.assertEqual(original_md5, generated_md5)
def _set_model_from_view(self) -> None: ctx = CtxCube() ctx.create_empty_cube(dimx=self.parameters["pixel_number_x"], dimy=self.parameters["pixel_number_y"], dimz=self.parameters["slice_number"], xoffset=float(self.view.xoffset.text), yoffset=float(self.view.yoffset.text), slice_offset=float(self.view.slice_offset.text), slice_distance=self.parameters["slice_distance"], pixel_size=self.parameters["pixel_size"], value=int(self.view.hu_value.text)) self.model.ctx = ctx self.model.ctx.basename = self.view.name.text vdxCube = VdxCube(self.model.ctx) vdxCube.basename = self.view.name.text self.model.vdx = vdxCube self.model.name = self.view.name.text
def test_filename_parsing(self): bare_name = "frodo_baggins" cube_ext = ".ctx" header_ext = ".hed" gzip_ext = ".gz" # all possible reasonable names for input file testing_names = (bare_name, bare_name + cube_ext, bare_name + cube_ext + gzip_ext, bare_name + header_ext, bare_name + header_ext + gzip_ext) # loop over all names for name in testing_names: logger.info("Parsing " + name) header_filename, cube_filename = CtxCube.parse_path(name) self.assertIsNotNone(header_filename) self.assertIsNotNone(cube_filename) logger.info("Parsing output: " + header_filename + " , " + cube_filename) # test if got what was expected self.assertEqual(header_filename, bare_name + header_ext) self.assertEqual(cube_filename, bare_name + cube_ext) dos_cube_ext = ".dos" # all possible reasonable names for input file testing_names = (bare_name, bare_name + dos_cube_ext, bare_name + dos_cube_ext + gzip_ext, bare_name + header_ext, bare_name + header_ext + gzip_ext) # loop over all names for name in testing_names: logger.info("Parsing " + name) dos_header_filename, dos_cube_filename = DosCube.parse_path(name) self.assertIsNotNone(dos_header_filename) self.assertIsNotNone(dos_cube_filename) logger.info("Parsing output: " + dos_header_filename + " , " + dos_cube_filename) # test if got what was expected self.assertEqual(dos_header_filename, bare_name + header_ext) self.assertEqual(dos_cube_filename, bare_name + dos_cube_ext)
def _finish(self, plan): """ return requested results, copy them back in to plan._working_dir """ for _file_name in plan._out_files: _path = os.path.join(plan._temp_dir, _file_name) # only copy files back, if we actually have been running TRiP if self._norun: logger.info("dummy run: would now copy {:s} to {:s}".format( _path, plan._working_dir)) else: logger.info("copy {:s} to {:s}".format(_path, plan._working_dir)) shutil.copy(_path, plan._working_dir) for _file_name in plan._out_files: _path = os.path.join(plan._temp_dir, _file_name) if ".phys.dos" in _file_name: _ctx_cube = DosCube() if not self._norun: _ctx_cube.read(_path) plan.dosecubes.append(_ctx_cube) if ".bio.dos" in _file_name: _ctx_cube = CtxCube() if not self._norun: _ctx_cube.read(_path) plan.dosecubes.append(_ctx_cube) if ".dosemlet.dos" in _file_name: _let_cube = LETCube() if not self._norun: _let_cube.read(_path) plan.letcubes.append(_let_cube) if ".rst" in _file_name: logger.warning( "attaching fields to class not implemented yet {:s}". format(_path)) # TODO # need to access the RstClass here for each rst file. This will then need to be attached # to the proper field in the list of fields. if self._cleanup: logger.debug("Delete {:s}".format(plan._temp_dir)) shutil.rmtree(plan._temp_dir)
def voxelplan_header_path(tmpdir_factory): ctx = CtxCube() ctx.create_empty_cube(value=0, dimx=100, dimy=100, dimz=25, pixel_size=1, slice_distance=2, slice_offset=0) file_obj = tmpdir_factory.mktemp("data").join("patient") patient_base_path = str(file_obj) ctx.write(patient_base_path) vdx = VdxCube(cube=ctx) voi = create_sphere(cube=ctx, name="target", center=[50, 50, 25], radius=10) vdx.add_voi(voi) vdx.write(patient_base_path + '.vdx') yield patient_base_path + '.hed'
def test_read(self): c = CtxCube() c.read(self.cube000)
def test_addition(self): # read cube c = CtxCube() c.read(self.cube000) d = c + 5 self.assertEqual(c.cube[10][20][30] + 5, d.cube[10][20][30])