def test_dump_to_csv(tmpdir): f = tmpdir.join('out.csv') matrix, attributes = preprocess.to_matrix(fx.data(), do_binarize=True, bins=4) preprocess.dump_to_csv(matrix, attributes, str(f)) df = pd.read_csv(f, sep=';') assert len(df) == 10 assert list(df.columns) == [ 'id', '2.6429less_than=lefthippocampusless_than2.850575', '2.850575less_than=lefthippocampusless_than3.05825', '3.05825less_than=lefthippocampusless_than3.2659249999999997', '3.2659249999999997less_than=lefthippocampusless_than3.4736', '63.0less_than=subjectageyearsless_than67.0', '67.0less_than=subjectageyearsless_than71.0', '71.0less_than=subjectageyearsless_than75.0', '75.0less_than=subjectageyearsless_than79.0', 'alzheimerbroadcategory' ]
def main(clean_files=False): """ :param clean_files: if True, clean files afterwards """ # Read inputs inputs = io_helper.fetch_data() data = inputs["data"] beam = parameters.get_parameter('beam', int, 10) support = parameters.get_parameter('support', float, '0.00001') out_file = 'input.csv' rules_out_file = 'rules.txt' matrix, attributes = preprocess.to_matrix(data) preprocess.dump_to_csv(matrix, attributes, out_file) # Call hedwig with sensible defaults examples_file = out_file empty_bk = tempfile.mkdtemp() call([ 'python', '-m' 'hedwig.__main__', empty_bk, examples_file, '--beam', str(beam), '--support', str(support), '-f', 'csv', '-l', '-o', rules_out_file, '--nocache' ]) with open(rules_out_file) as f: results = f.read() if clean_files: os.remove(out_file) os.remove(rules_out_file) io_helper.save_results(results.replace('less_than', '<'), shapes.Shapes.TEXT)
DEFAULT_DOCKER_IMAGE = 'python-hedwig' if __name__ == '__main__': # Configure logging logging.basicConfig(level=logging.INFO) # Read inputs inputs = io_helper.fetch_data() data = inputs["data"] out_file = 'input.csv' rules_out_file = 'rules.txt' matrix, attributes = preprocess.to_matrix(data) preprocess.dump_to_csv(matrix, attributes, out_file) # Call hedwig with sensible defaults examples_file = out_file empty_bk = tempfile.mkdtemp() call([ 'python', '-m', 'hedwig', empty_bk, examples_file, '-f', 'csv', '-l', '-o', rules_out_file, '--nocache' ]) results = '' with open(rules_out_file) as f: results = f.read() io_helper.save_results(results, '', 'text/plain')