Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def apply_preprocessors(preprocessors, nbname):
notebooks_path = os.path.join(os.path.split(__file__)[0], 'notebooks')
with open(os.path.join(notebooks_path, nbname)) as f:
nb = nbformat.read(f, nbformat.NO_CONVERT)
exporter = nbconvert.PythonExporter()
for preprocessor in preprocessors:
exporter.register_preprocessor(preprocessor)
source, meta = exporter.from_notebook_node(nb)
return source
def test_notebooks():
"""Run all notebooks in /docs/tutorials/ as tests."""
# Get the notebook names
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
path = os.path.join(root, 'docs', 'tutorials')
notebooks = glob.glob(os.path.join(path, '*.ipynb'))
# Convert them to python scripts
exporter = PythonExporter()
for notebook in notebooks:
# Get the script as a string
script, _ = exporter.from_filename(notebook)
# Get rid of %matplotlib inline commands
script = script.replace("get_ipython().magic('matplotlib inline')", "")
script = script.replace(
"get_ipython().run_line_magic('matplotlib', 'inline')", "")
# Get rid of %run commands
script = re.sub("get_ipython\(\).magic\('run (.*)'\)", r"#", script)
script = re.sub("get_ipython\(\).run_line_magic\('run', '(.*)'\)",
r"#", script)
# Remove the %time wrappers
script = re.sub("get_ipython\(\).magic\('time (.*)'\)", r"\1", script)
def export_python(nb, destfn):
exporter = PythonExporter()
body, resources = exporter.from_notebook_node(nb)
with open(destfn, 'w') as f:
f.write(body)
def export_to_python(filename=None,
preprocessors=[OptsMagicProcessor(),
OutputMagicProcessor(),
StripMagicsProcessor()]):
filename = filename if filename else sys.argv[1]
with open(filename) as f:
nb = nbformat.read(f, nbformat.NO_CONVERT)
exporter = nbconvert.PythonExporter()
for preprocessor in preprocessors:
exporter.register_preprocessor(preprocessor)
source, meta = exporter.from_notebook_node(nb)
return source
def export_to_python(filename=None,
preprocessors=[OptsMagicProcessor(),
OutputMagicProcessor(),
StripMagicsProcessor()]):
filename = filename if filename else sys.argv[1]
with open(filename) as f:
nb = nbformat.read(f, nbformat.NO_CONVERT)
exporter = nbconvert.PythonExporter()
for preprocessor in preprocessors:
exporter.register_preprocessor(preprocessor)
source, meta = exporter.from_notebook_node(nb)
return source
def export_python(nb, destfn):
exporter = PythonExporter()
body, resources = exporter.from_notebook_node(nb)
with open(destfn, 'w') as f:
f.write(body)
def get_hyperopt_model_string(model, data, functions, notebook_name, verbose, stack, data_args):
model_string = inspect.getsource(model)
model_string = remove_imports(model_string)
if notebook_name:
notebook_path = os.getcwd() + "/{}.ipynb".format(notebook_name)
with open(notebook_path, 'r') as f:
notebook = nbformat.reads(f.read(), nbformat.NO_CONVERT)
exporter = PythonExporter()
source, _ = exporter.from_notebook_node(notebook)
else:
calling_script_file = os.path.abspath(inspect.stack()[stack][1])
with open(calling_script_file, 'r') as f:
source = f.read()
cleaned_source = remove_all_comments(source)
imports = extract_imports(cleaned_source, verbose)
parts = hyperparameter_names(model_string)
aug_parts = augmented_names(parts)
hyperopt_params = get_hyperparameters(model_string)
space = get_hyperopt_space(parts, hyperopt_params, verbose)
functions_string = retrieve_function_string(functions, verbose)