def evaluate_model(out_dir: str) -> None: """Takes the saved model and the test data set to evaluate the model and create a report Parameters ---------- out_dir: Directory to save files to, no filename included Returns ------- None """ log = logging.getLogger('evaluate-model') save_path = Path(out_dir) / 'document.md' if os.path.exists(out_dir) == False: os.makedirs(out_dir) # Running the report creation pweave.weave(file="./document.py", informat="script", doctype="markdown", output=save_path, figdir=out_dir) # Cleaning up and resaving the file clean_report = _clean_up_report(open(save_path, "r").read()) open(save_path, "w+").write(clean_report) # Creating a zip file out of the report files, as it is required by the # tast log.info(f"Saving document at {out_dir}") shutil.make_archive(Path(out_dir) / '../report.zip', 'zip', out_dir)
def test_markdown(): """Test markdown reader""" pweave.weave("tests/readers/markdown_reader.pmd", doctype="pandoc", informat="markdown") assertSameContent("tests/readers/markdown_reader.md", "tests/readers/markdown_reader_ref.md")
def test_default_shell(): """Test python shell""" pweave.weave("tests/processors/processor_test.mdw", doctype="leanpub", informat="markdown") assertSameContent("tests/processors/processor_test.txt", "tests/processors/processor_default_ref.md")
def test_cache(): """Test caching shell""" shutil.rmtree("tests/processors/cache", ignore_errors=True) pweave.weave("tests/processors/processor_test.pmd", docmode=True) pweave.weave("tests/processors/processor_test.pmd", docmode=True) assertSameContent("tests/processors/processor_test.md", "tests/processors/processor_cache_ref.md")
def weave(path): stdout = sys.stdout sys.stdout = open(os.devnull, 'w') try: pweave.weave(path, doctype='tex', figdir=options.state.figure(), shell=options.state.shell()) except: raise LibError('Failed to Pweave file: ' + path) sys.stdout = stdout
def test_ipython_shell(): """Test ipython python shell""" pweave.weave("tests/processors/ipy_processor_test.mdw", shell="ipython", doctype="leanpub", informat="markdown") assertSameContent("tests/processors/ipy_processor_test.txt", "tests/processors/ipy_processor_ref.md")
def test_external_shell(): """Test external python shell""" pweave.weave("tests/processors/processor_test.mdw", shell="epython", doctype="leanpub", informat="markdown") assertSameContent("tests/processors/processor_test.txt", "tests/processors/processor_external_ref.md")
def test_continue_option(): """Test documenting a class in multiple chunks using continue option""" REF = 'tests/ar_yw_ref.md' infile = 'tests/ar_yw.mdw' outfile = 'tests/ar_yw.md' pweave.weave(file=infile, doctype="pandoc") assertSameContent(REF, outfile)
def testMethod(self): infile = self.absPathTo(self.INFILE) self.setNewOutfile(infile[:-3] + ext) print("") pweave.weave(infile, doctype=doctype, informat='markdown') self.REFERENCE = self.absPathTo(reference) self.assertSameAsReference()
def test_inline_chunks(): """Test inline code""" REF = 'tests/inline_chunks_ref.md' infile = 'tests/inline_chunks.mdw' outfile = 'tests/inline_chunks.md' pweave.weave(file=infile, doctype="pandoc") assertSameContent(REF, outfile)
def make_report(): args = parse_args() config = {} qcfunk.parse_yaml_file(args.config, config) free_text_dict = make_free_text_dict(config) path = os.path.join(config["outdir"], "report", "figures", "svg_figures") try: os.mkdir(path) except FileExistsError: pass config["svg_figdir"] = path with open(config["outfile"], 'w') as pmd_file: change_line_dict = {} for key, value in config.items(): if type(value) == bool: new_value = f'{key} = {value}\n' else: new_value = f'{key} = "{value}"\n' change_line_dict[key] = new_value with open(config["report_template"]) as f: for l in f: line_written = False if "##INSERT_ARGUMENTS" in l: pmd_file.write("".join(list(change_line_dict.values()))) line_written = True elif "##APPENDIX" in l: if config["omit_appendix"]: new_l = "" line_written = True else: line_written = True with open(config['appendix']) as f: for l in f: pmd_file.write(l) else: for k, v in free_text_dict.items(): if k in l: if "'''" not in v: new_l = str(v) else: new_l = v.lstrip("'").rstrip("'") pmd_file.write(new_l) line_written = True if not line_written: pmd_file.write(l) weave(config["outfile"], doctype="pandoc", figdir=config["figdir"])
def testMethod(self): infile = self.absPathTo(self.INFILE) self.setNewOutfile(infile[:-3] + ext) print("") pweave.weave(infile, doctype=doctype, informat='markdown') self.REFERENCE = self.absPathTo(reference) self.assertSameAsReference
def test_url(): pweave.weave( "http://files.mpastell.com/formatters_test.pmd", doctype="pandoc", output="tests/formats/formatters_url.md", ) assertSameContent( "tests/formats/formatters_url.md", "tests/formats/formatters_test_REF.markdown", )
def testMethod(self): self.TESTDIR = os.path.join('weave', doctype) infile = self.absPathTo(filename + 'w') self.setNewOutfile(filename) pweave.weave(infile, doctype=doctype, **kwargs) basename, _, ext = filename.rpartition('.') self.REFERENCE = self.absPathTo(basename + '_REF.' + ext) self.assertSameAsReference()
def test_term(): """Test Python terminal emulation Eval statements might not work with ipython properly (code compiled differently)""" REF = 'tests/term_test_ref.tex' infile = 'tests/term_test.texw' outfile = 'tests/term_test.tex' pweave.weave(file=infile, doctype="tex", shell="python") assertSameContent(REF, outfile)
def test_continue_option(): """Test documenting a class in multiple chunks using continue option""" REF = 'tests/ar_yw_ref.md' infile = 'tests/ar_yw.mdw' outfile = 'tests/ar_yw.md' pweave.weave(file=infile, doctype="pandoc") # Compare the outfile and the ref out = open(outfile) ref = open(REF) assert(out.read() == ref.read())
def test_inline_chunks(): """Test inline code""" REF = 'tests/inline_chunks_ref.md' infile = 'tests/inline_chunks.mdw' outfile = 'tests/inline_chunks.md' pweave.weave(file=infile, doctype="pandoc") # Compare the outfile and the ref out = open(outfile) ref = open(REF) assert (out.read() == ref.read())
def test_octave(): """Test running Octave code""" REF = 'tests/octave_test_ref.md' infile = 'tests/octave_test.mdw' outfile = 'tests/octave_test.md' pweave.weave(file=infile, doctype="pandoc", shell="octave") # Compare the outfile and the ref out = open(outfile) ref = open(REF) assert (out.read() == ref.read())
def test_octave(): """Test running Octave code""" REF = 'tests/octave_test_ref.md' infile = 'tests/octave_test.mdw' outfile = 'tests/octave_test.md' pweave.weave(file=infile, doctype="pandoc", shell="octave") # Compare the outfile and the ref out = open(outfile) ref = open(REF) assert(out.read() == ref.read())
def test_inline_chunks(): """Test inline code""" REF = 'tests/inline_chunks_ref.md' infile = 'tests/inline_chunks.mdw' outfile = 'tests/inline_chunks.md' pweave.weave(file=infile, doctype="pandoc") # Compare the outfile and the ref out = open(outfile) ref = open(REF) assert(out.read() == ref.read())
def test_continue_option(): """Test documenting a class in multiple chunks using continue option""" REF = 'tests/ar_yw_ref.md' infile = 'tests/ar_yw.mdw' outfile = 'tests/ar_yw.md' pweave.weave(file=infile, doctype="pandoc") # Compare the outfile and the ref out = open(outfile) ref = open(REF) assert (out.read() == ref.read())
def test_cache(): """Test caching shell""" pweave.weave("tests/processors/processor_test.mdw", docmode=True, doctype="markdown", informat="markdown") pweave.weave("tests/processors/processor_test.mdw", docmode=True, doctype="markdown", informat="markdown") assertSameContent("tests/processors/processor_test.md", "tests/processors/processor_cache_ref.md")
def make_reports(self): for i, row in targets['Basic information'].iterrows(): # Write some temporary CSV files, invoke the report markdown thing, and then delete the temp files. # That's messy, but it's one of the quicker ways of doing this - especially when dealing with a lot of # targets. # I thought about doing some input (for the report thing) verification, but a better approach would be to # make it robust against anything that's going to come out of here. Any issues should have been sorted out # long ago by this point. print(row['HGNC Name']) temp_gtex_file = "temp/gtex.csv" targets['GTEX'].to_csv(temp_gtex_file) temp_basic_info = "temp/basic.csv" targets['Basic information'].to_csv(temp_basic_info) temp_pharos = "temp/pharos.csv" targets['Pharos'].to_csv(temp_pharos) temp_barres_mouse = "temp/barres_mouse.csv" targets['Barres mouse'].to_csv(temp_barres_mouse) temp_barres_human = "temp/barres_human.csv" targets['Barres human'].to_csv(temp_barres_human) temp_disease_assocs = "temp/disease_assocs.csv" targets['Disease associations'][ targets['Disease associations']['series'] == row['series']].to_csv(temp_disease_assocs) temp_drugs = "temp/drugs.csv" targets['Existing drugs'][targets['Existing drugs']['series'] == row['series']].to_csv(temp_drugs) temp_druggability = "temp/druggability.csv" targets['Druggability'].to_csv(temp_druggability) temp_biopharm = "temp/biopharm.csv" targets['Biopharmability'].to_csv(temp_biopharm) temp_risk = "temp/risk.csv" targets['Risk factors'].to_csv(temp_risk) html_report_file = "Example_output/" + row['HGNC Name'] + ".html" # pweave.weave("report_test.md", doctype='md2html', informat='markdown', output=html_report_file) pweave.weave("summary_report.md", doctype='md2html', informat='markdown', output=html_report_file) os.remove(temp_gtex_file) os.remove(temp_basic_info) os.remove(temp_pharos) os.remove(temp_barres_mouse) os.remove(temp_barres_human) os.remove(temp_disease_assocs) os.remove(temp_drugs) os.remove(temp_druggability) os.remove(temp_biopharm) os.remove(temp_risk) print("HOLD UP - TESTING MODE") input()
def testWhenFigdirGivenFiguresAreWritenThere(self): filename = 'FIR_design_verb/FIR_design_figdir.tex' self.removeFigures('FIR_design_verb/figs') self.setNewOutfile(filename) pweave.weave(self.absPathTo(self.INFILE), output=self.absPathTo(filename), figdir='figs', doctype='tex') self.assertSameAsPattern('FIR_design_verb/REF_tex.pattern', figdir='figs') self.checkFiguresExist('FIR_design_verb/figs')
def testWhenOutputInDifferentDirectoryFiguresDirectoryIsWritenThere(self): filename = 'FIR_design_verb/FIR_design.tex' self.removeFigures('FIR_design_verb/figures') self.setNewOutfile(filename) pweave.weave(self.absPathTo(self.INFILE), output=self.absPathTo(filename), doctype='tex') self.assertSameAsPattern('FIR_design_verb/REF_tex.pattern', figdir='figures') self.checkFiguresExist('FIR_design_verb/figures')
def testMethod(self): self.TESTDIR = os.path.join("weave", doctype) infile = self.absPathTo(filename + "w") self.setNewOutfile(filename) pweave.weave(infile, doctype=doctype, output=self.absPathTo(filename), **kwargs) basename, _, ext = filename.rpartition(".") self.REFERENCE = self.absPathTo(basename + "_REF." + ext) self.assertSameAsReference()
def make_report(filename): mdw_filename = os.path.realpath(filename) md_filename = os.path.splitext(mdw_filename)[0] + ".md" html_filename = os.path.splitext(mdw_filename)[0] + ".html" pweave.weave(mdw_filename, doctype="pandoc", figformat=".svg") with open(md_filename, "r") as md_fileobj: content = md.convert(md_fileobj.read()) metadata = {'title': md.Meta["title"][0], 'content': content} with open(html_filename, "w") as f: f.write(template % metadata)
def evaluate_model(model, in_data, out_dir, name): """Evaluate model and save html report to disc Parameters ---------- model: pickle file that contains model to be evaluated in-data: str name of the parquet file on local disk that contains test data out_dir: directory where output html should be saved to. name: str name of output report when saved Returns ------- None """ log = logging.getLogger('evaluate_model') log.info('Evaluating model.') out_path = Path(out_dir) / f'{name}.html' data = pd.read_parquet(in_data) test = data[data.istest == True].drop('istest', axis=1) label = ['points'] X = test.drop(label, axis=1) y = test[label] loaded_model = pickle.load(open(model, 'rb')) predictions = loaded_model.predict(X) y['predictions'] = predictions preds_path = Path(out_dir) / 'predictions.parquet.gzip' y.to_parquet(str(preds_path), compression='gzip') pweave.weave('report.pmd', doctype="md2html", output=out_path) log.info('Success! Report saved to {out_path}') flag = Path(out_dir) / '.SUCCESS' flag.touch()
def savenBuild(self, name, src): """ Saves the LaTeX in a newly created directory and builds it. """ dirname = self.sim.modelName + r'-report-' Path = dirname + ctime() Path = Path.replace(' ', '-') os.system('mkdir ' + Path) os.chdir(Path) print(f'Saving {name}.pmd') with codecs.open(f'{name}.pmd', 'w', self.encoding) as fs: fs.write(src) pweave.weave(f"{name}.pmd", doctype='markdown')
def run(self): """Run pweave.""" import traceback import pweave from docs import PegdownMarkdownFormatter bad_words = ["Error"] pweave.rcParams["chunk"]["defaultoptions"].update({ 'wrap': False, 'dpi': 175 }) if self.format == 'markdown': pweave.PwebFormats.formats['markdown'] = { 'class': PegdownMarkdownFormatter, 'description': 'Pegdown compatible markdown' } if self.format == 'notebook': # Just convert to an unevaluated notebook. pweave.rcParams["chunk"]["defaultoptions"].update( {'evaluate': False}) for file in sorted(self.files, reverse=False): name = path.splitext(path.basename(file))[0] dest = self.dest_file(file) if (not self.quick) or (not path.exists(dest)) or ( path.getmtime(dest) < path.getmtime(file)): print(_divided('Running %s' % name)) try: pweave.weave(file=str(file), doctype=self.format) if self.format == 'markdown': if not path.exists(dest): raise FileNotFoundError( "Markdown file '%s' didn't get created as expected" % dest) with open(dest, "r") as result: for (n, line) in enumerate(result): for word in bad_words: if word in line: raise ChildProcessError( "Error detected on line %s in %s:\n%s" % (n + 1, dest, line)) except Exception: print(_divided('%s Failed:' % file)) print(traceback.format_exc()) exit(1) else: print(_divided('Skipping %s' % name))
def test_pandoc(): """Integration test pweave by comparing output to a known good reference. N.B. can't use anything in the .mdw that will give different outputs each time. For example, setting term=True and then calling figure() will output a matplotlib figure reference. This has a memory pointer that changes every time. """ REF = 'tests/simple_REF.md' infile = 'tests/simple.mdw' outfile = 'tests/simple.md' pweave.weave(file=infile, doctype="pandoc") assertSameContent(REF, outfile)
def weave(): if len(sys.argv) == 1: print("This is pweave %s, enter Pweave -h for help" % pweave.__version__) sys.exit() # Command line options parser = OptionParser(usage="pweave [options] sourcefile", version="Pweave " + pweave.__version__) parser.add_option("-f", "--format", dest="doctype", default=None, help="The output format. Available formats: " + pweave.PwebFormats.shortformats() + " Use Pweave -l to list descriptions or see http://mpastell.com/pweave/formats.html") parser.add_option("-i", "--input-format", dest="informat", default=None, help="Input format: noweb, markdown, notebook or script") parser.add_option("-k", "--kernel", dest="kernel", default='python3', help="Jupyter kernel used to run code: default is python3") parser.add_option("-o", "--output", dest="output", default=None, help="Name of the output file") parser.add_option("-l", "--list-formats", dest="listformats", action="store_true", default=False, help="List output formats") parser.add_option("-m", "--matplotlib", dest="plot", default=True, action="store_false", help="Disable matplotlib") parser.add_option("-d", "--documentation-mode", dest="docmode", action="store_true", default=False, help="Use documentation mode, chunk code and results will be loaded from cache and inline code will be hidden") parser.add_option("-c", "--cache-results", dest="cache", action="store_true", default=False, help="Cache results to disk for documentation mode") parser.add_option("-F", "--figure-directory", dest="figdir", default='figures', help="Directory path for matplolib graphics: Default 'figures'") parser.add_option("--cache-directory", dest="cachedir", default='cache', help="Directory path for cached results used in documentation mode: Default 'cache'") parser.add_option("-g", "--figure-format", dest="figformat", default=None, help="Figure format for matplotlib graphics: Defaults to 'png' for rst and Sphinx html documents and 'pdf' for tex") parser.add_option("-t", "--mimetype", dest="mimetype", default=None, help="Source document's text mimetype. This is used to set cell " + "type in Jupyter notebooks") (options, args) = parser.parse_args() try: infile = args[0] except IndexError: infile = "" opts_dict = vars(options) if options.figformat is not None: opts_dict["figformat"] = ('.%s' % options.figformat) pweave.weave(infile, **opts_dict)
def make_report(filename): mdw_filename = os.path.realpath(filename) md_filename = os.path.splitext(mdw_filename)[0] + ".md" html_filename = os.path.splitext(mdw_filename)[0] + ".html" pweave.weave(mdw_filename, doctype="pandoc", figformat=".svg") with open(md_filename, "r") as md_fileobj: content = md.convert(md_fileobj.read()) metadata = { 'title' : md.Meta["title"][0], 'content' : content } with open(html_filename, "w") as f: f.write(template % metadata)
def main(): """ Build html from specified files, using pweave. """ src_dir = Path("src") build_dir = Path("build") # Get the pweave files from the source directory as strings files = [f.name for f in src_dir.glob("*.pmd")] for f in files: # Turn python markdown into HTML weave( src_dir.joinpath(f), output=build_dir.joinpath(f.replace("pmd", "html")), cache=True, )
def create_html(self): # timestamp from source file timestamp_source = os.stat(self.repository).st_mtime # timesampt from html file try: timestamp_html = os.stat(self.output_file).st_mtime except (FileNotFoundError): timestamp_html = datetime(2000, 1, 1) if timestamp_source > timestamp_html: self.replace_timestamps_source() # bring the data in the right form modify_data.modify_data() missing.missing() pweave.weave(file=self.input_file, doctype='md2html', output=self.output_file) self.place_tracking_code() else: self.replace_timestamps_output()
def test_markdown(): """Test markdown reader""" pweave.weave("tests/readers/markdown_reader.pmd", doctype = "leanpub", informat = "markdown") assertSameContent("tests/readers/markdown_reader.txt", "tests/readers/markdown_reader_ref.md")
def testPandoc(self): pweave.weave(file=self.absPathTo(self.INFILE), doctype="pandoc") self.assertSameAsReference()
def test_cache(): """Test caching shell""" shutil.rmtree("tests/processors/cache", ignore_errors=True) pweave.weave("tests/processors/processor_test.pmd", docmode = True) pweave.weave("tests/processors/processor_test.pmd", docmode = True) assertSameContent("tests/processors/processor_test.md", "tests/processors/processor_cache_ref.md")
def test_xoctave(): """Test Octave code""" pweave.weave("tests/octave/octave_test.mdw", doctype = "leanpub", shell = "octave") assertSameContent("tests/octave/octave_test.txt", "tests/octave/octave_test_ref.md")
def testTerm(self): pweave.weave(file=self.absPathTo(self.INFILE), doctype="tex", shell="python") self.assertSameAsReference()
def test_cache(): """Test caching shell""" pweave.weave("tests/processors/processor_test.mdw", docmode = True, doctype = "leanpub", informat = "markdown") pweave.weave("tests/processors/processor_test.mdw", docmode = True, doctype = "leanpub", informat = "markdown") assertSameContent("tests/processors/processor_test.txt", "tests/processors/processor_cache_ref.md")
def weave(): if len(sys.argv) == 1: print("This is Pweave %s, enter Pweave -h for help" % pweave.__version__) sys.exit() # Command line options parser = OptionParser(usage="Pweave [options] sourcefile", version="Pweave " + pweave.__version__) parser.add_option( "-f", "--format", dest="doctype", default='rst', help="The output format. Available formats: " + pweave.PwebFormats.shortformats() + " Use Pweave -l to list descriptions or see http://mpastell.com/pweave/formats.html" ) parser.add_option("-i", "--input-format", dest="informat", default='noweb', help="Input format: noweb, notebook or script") parser.add_option( "-s", "--shell", dest="shell", default='python', help= "shell used to run code: python, epython (external python shell), ipython, matlab, octave or julia" ) parser.add_option( "--shell-path", dest="shell_path", default=None, help="Set the path of shell to run code, only affects \"epython\" shell" ) parser.add_option("-l", "--list-formats", dest="listformats", action="store_true", default=False, help="List output formats") parser.add_option("-m", "--matplotlib", dest="plot", default=True, action="store_false", help="Disable matplotlib") parser.add_option( "-d", "--documentation-mode", dest="docmode", action="store_true", default=False, help= "Use documentation mode, chunk code and results will be loaded from cache and inline code will be hidden" ) parser.add_option("-c", "--cache-results", dest="cache", action="store_true", default=False, help="Cache results to disk for documentation mode") parser.add_option( "-F", "--figure-directory", dest="figdir", default='figures', help="Directory path for matplolib graphics: Default 'figures'") parser.add_option( "--cache-directory", dest="cachedir", default='cache', help= "Directory path for cached results used in documentation mode: Default 'cache'" ) parser.add_option( "-g", "--figure-format", dest="figformat", default=None, help= "Figure format for matplotlib graphics: Defaults to 'png' for rst and Sphinx html documents and 'pdf' for tex" ) (options, args) = parser.parse_args() try: infile = args[0] except IndexError: infile = "" opts_dict = vars(options) if options.figformat is not None: opts_dict["figformat"] = ('.%s' % options.figformat) pweave.weave(infile, **opts_dict)
def test_ipython_shell(): """Test ipython python shell""" pweave.weave("tests/processors/ipy_processor_test.mdw", shell = "ipython", doctype = "leanpub", informat = "markdown") assertSameContent("tests/processors/ipy_processor_test.txt", "tests/processors/ipy_processor_ref.md")
def test_external_shell(): """Test external python shell""" pweave.weave("tests/processors/processor_test.mdw", shell = "epython", doctype = "leanpub", informat = "markdown") assertSameContent("tests/processors/processor_test.txt", "tests/processors/processor_external_ref.md")
def test_default_shell(): """Test python shell""" pweave.weave("tests/processors/processor_test.mdw", doctype = "leanpub", informat = "markdown") assertSameContent("tests/processors/processor_test.txt", "tests/processors/processor_default_ref.md")
def testWrap(self): pweave.weave(file=self.absPathTo(self.INFILE), doctype="texminted") self.assertSameAsReference()
def test_url(): pweave.weave("http://files.mpastell.com/formatters_test.pmd", doctype = "pandoc", output = "tests/formats/formatters_url.md") assertSameContent("tests/formats/formatters_url.md", "tests/formats/formatters_test_REF.markdown")