def save_snapshot_nml_read_and_write(): for i in range(0, len(INPUT_FILES)): input_file = INPUT_FILES[i] output_file = OUTPUT_FILES[i] parsed = wknml.parse_nml(input_file) with open(output_file + '.snapshot', 'wb') as f: wknml.write_nml(f, parsed)
def test_read_and_write_and_read(): for i in range(0, len(INPUT_FILES)): input_file = INPUT_FILES[i] output_file = OUTPUT_FILES[i] first = wknml.parse_nml(input_file) with open(output_file, 'wb') as f: wknml.write_nml(f, first) second = wknml.parse_nml(output_file) assert first == second
def test_snapshot_read_and_compare_nml(): for i in range(0, len(INPUT_FILES)): input_file = INPUT_FILES[i] snapshot_file = SNAPSHOT_FILES[i] output_file = OUTPUT_FILES[i] parsed = wknml.parse_nml(input_file) with open(output_file, 'wb') as f: wknml.write_nml(f, parsed) assert filecmp.cmp(snapshot_file + '.snapshot', output_file)
def test_calc_supervoxel_eftpl(tmp_path): dummy_data = np.random.randint(100, size=(10, 10, 10)) with h5py.File(tmp_path / 'test.h5', 'w') as h5file: h5file.create_dataset('with_background/test_data/labels', data=dummy_data) nml = util_get_nml() with open(tmp_path / "test.nml", "wb") as f: wknml.write_nml(f, nml) calc_supervoxel_eftpl(tmp_path / 'test.nml', tmp_path / 'test.h5') return
def write_nml(self, nml_write_path): """ Writes the present state of the skeleton object to a .nml file. Args: nml_write_path: Path to which .nml file should be written """ # If the object does not have any trees, construct an empty tree before writing to enable webKnossos import if self.num_trees() == 0: self.add_tree() nml = self._skeleton_to_nml() with open(nml_write_path, "wb") as f: wknml.write_nml(f, nml)
def load_and_save_merged_nml_trees(source: str, destination: str, scale): assert os.path.isfile(source), "No file was provided as source." assert os.path.exists(os.path.dirname(os.path.realpath(destination))), "The destination directory does not exists." logger.info("Reading data") with open(source, "rb") as f: nml = parse_nml(f) logger.info("Starting to merge tree groups") merged_nml = create_merged_nml(nml, scale) logger.info("Writing data") with open(destination, "wb") as f: write_nml(f, merged_nml) logger.info("Done")
def test_generate_nml(): with open("testdata/nml_with_invalid_ids.nml", "r") as file: test_nml = parse_nml(file) (graph, parameter_dict) = generate_graph(test_nml) test_result_nml = generate_nml(tree_dict=graph, parameters=parameter_dict) with open("testdata/expected_result.nml", "r") as file: expected_nml = parse_nml(file) # need to save and load the test_result_nml since reading applies default values # thus this is needed to be able to compare the nmls with open("testoutput/temp.nml", "wb") as file: write_nml(file=file, nml=test_result_nml) with open("testoutput/temp.nml", "r") as file: test_result_nml = parse_nml(file) assert test_result_nml == expected_nml
def test_no_default_values_written(): input_file_name = "testdata/nml_without_default_values.nml" output_file_name = "testoutput/nml_without_default_values.nml" # read and write the test file with open(input_file_name, "r") as file: test_nml = parse_nml(file) with open(output_file_name, "wb") as output_file: write_nml(file=output_file, nml=test_nml) # read the written testfile and compare the content with open(input_file_name, "r") as file: test_nml = parse_nml(file) with open(output_file_name, "r") as output_file: test_result_nml = parse_nml(output_file) assert test_nml == test_result_nml, "The testdata file and the testoutput file do not have the same content." # test if both files have the same content assert filecmp.cmp( input_file_name, output_file_name ), "The testdata and the testoutput file do not have the same content."
nml.trees) new_tree = wknml.Tree( id=i, color=old_tree.color, name=old_tree.name, groupId=old_tree.groupId, nodes=[n for n in all_nodes if n.id in node_ids], edges=[ e for e in all_edges if e.source in node_ids or e.target in node_ids ], ) old_new_mapping[old_tree.id].append(i) new_trees.append(new_tree) new_trees_with_groups = [] new_groups = [] for i, (old_id, new_ids) in enumerate(old_new_mapping.items()): group_id = i + 1 old_tree = find(lambda t: t.id == old_id, nml.trees) new_groups.append(wknml.Group(id=group_id, name=old_tree.name, children=[])) for new_id in new_ids: new_tree = find(lambda t: t.id == new_id, new_trees) new_trees_with_groups.append(new_tree._replace(groupId=group_id)) nml = nml._replace(trees=new_trees_with_groups, groups=new_groups) with open(args.target, "wb") as f: wknml.write_nml(f, nml)