How to call module written with argparse in iPython notebook
Asked Answered
U

11

60

I am trying to pass BioPython sequences to Ilya Stepanov's implementation of Ukkonen's suffix tree algorithm in iPython's notebook environment. I am stumbling on the argparse component.

I have never had to deal directly with argparse before. How can I use this without rewriting main()?

By the by, this writeup of Ukkonen's algorithm is fantastic.

Uroscopy answered 5/6, 2015 at 1:12 Comment(0)
A
25

I've had a similar problem before, but using optparse instead of argparse.

You don't need to change anything in the original script, just assign a new list to sys.argv like so:

if __name__ == "__main__":
    from Bio import SeqIO
    path = '/path/to/sequences.txt'
    sequences = [str(record.seq) for record in  SeqIO.parse(path, 'fasta')]
    sys.argv = ['-f'] + sequences
    main()
Aeonian answered 5/6, 2015 at 8:57 Comment(2)
Nice. Just add dummy sys.argv works like sys.argv = ['foo'].Lancewood
i don't know for how long i have been searching for this solution, thanks!Supercharger
W
58

Using args = parser.parse_args(args=[]) would solve execution problem.

or you can declare it as class format.

class Args(argparse.Namespace):
  data = './data/penn'
  model = 'LSTM'
  emsize = 200
  nhid = 200

args=Args()
Warner answered 3/11, 2017 at 1:47 Comment(4)
For me this was the most useful way to add arguments to Jupyter notebook. Thank youMaigre
I didnt want to deep dive into the whole topic, just get my 3rd party code, called from a jupyter notebook, to work. This did.Pinworm
such a simple trick, I don't know why I didn't think about it, Thanks !Troche
Could someone elaborate on how to use this? I may be missing something because when I print(args) it shows Args().Considering
O
51

An alternative to use argparse in Ipython notebooks is passing a string to:

args = parser.parse_args() (line 303 from the git repo you referenced.)

Would be something like:

parser = argparse.ArgumentParser(
        description='Searching longest common substring. '
                    'Uses Ukkonen\'s suffix tree algorithm and generalized suffix tree. '
                    'Written by Ilya Stepanov (c) 2013')

parser.add_argument(
        'strings',
        metavar='STRING',
        nargs='*',
        help='String for searching',
    )

parser.add_argument(
        '-f',
        '--file',
        help='Path for input file. First line should contain number of lines to search in'
    )

and

args = parser.parse_args("AAA --file /path/to/sequences.txt".split())

Edit: It works

Olav answered 5/6, 2015 at 11:26 Comment(2)
@mithrado @rjurney Almost, this works: args = parser.parse_args(['--file', '/path/to/sequences.txt']), i.e. you need to pass an array of strings where each element is an argument that would normally be separated by a space in the command line.Candlewood
@Candlewood the way to split the sequence automatically is to use shlex.split: args = parser.parse_args(shlex.split("AAA --file /path/to/sequences.txt"))Hognut
M
28

If all arguments have a default value, then adding this to the top of the notebook should be enough:

import sys
sys.argv = ['']

(otherwise, just add necessary arguments instead of the empty string)

Malvoisie answered 20/3, 2020 at 9:53 Comment(0)
A
25

I've had a similar problem before, but using optparse instead of argparse.

You don't need to change anything in the original script, just assign a new list to sys.argv like so:

if __name__ == "__main__":
    from Bio import SeqIO
    path = '/path/to/sequences.txt'
    sequences = [str(record.seq) for record in  SeqIO.parse(path, 'fasta')]
    sys.argv = ['-f'] + sequences
    main()
Aeonian answered 5/6, 2015 at 8:57 Comment(2)
Nice. Just add dummy sys.argv works like sys.argv = ['foo'].Lancewood
i don't know for how long i have been searching for this solution, thanks!Supercharger
F
4

Clean sys.argv

import sys; sys.argv=['']; del sys

https://github.com/spyder-ide/spyder/issues/3883#issuecomment-269131039

Francoisefrancolin answered 27/8, 2019 at 6:14 Comment(0)
U
3

I ended up using BioPython to extract the sequences and then editing Ilya Steanov's implementation to remove the argparse methods.

import imp
seqs = []
lcsm = imp.load_source('lcsm', '/path/to/ukkonen.py')
for record in SeqIO.parse('/path/to/sequences.txt', 'fasta'):
    seqs.append(record)
lcsm.main(seqs)

For the algorithm, I had main() take one argument, his strings variable, but this sends the algorithm a list of special BioPython Sequence objects, which the re module doesn't like. So I had to extract the sequence string

suffix_tree.append_string(s)

to

suffix_tree.append_string(str(s.seq))

which seems kind of brittle, but that's all I've got for now.

Uroscopy answered 5/6, 2015 at 3:13 Comment(0)
B
3

I face a similar problem in invoking argsparse, the string '-f' was causing this problem. Just removing that from sys.srgv does the trick.

import sys
if __name__ == '__main__':
    if '-f' in sys.argv:
        sys.argv.remove('-f')
    main()
Browbeat answered 25/10, 2018 at 17:57 Comment(0)
H
2

Here is my code which works well and I won't worry about the environment changed.

import sys
temp_argv = sys.argv

try:
    sys.argv = ['']
    print(sys.argv)
    args = argparse.parser_args()
finally:
    sys.argv = temp_argv
    print(sys.argv)
Hescock answered 17/11, 2020 at 14:46 Comment(1)
This worked for me!Sherwin
L
1

Suppose you have this small code in python:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", help="increase output verbosity",
                    action="store_true")
parser.add_argument("-v_1", "--verbose_1", help="increase output verbosity",
                    action="store_true")
args = parser.parse_args()

To write this code in Jupyter notebook write this:

import argparse

args = argparse.Namespace(verbose=False, verbose_1=False)

Note: In python, you can pass arguments on runtime but in the Jupyter notebook that will not be the case so be careful with the data types of your arguments.

Lobscouse answered 7/3, 2022 at 19:13 Comment(0)
A
1

If arguments passed by the iPython environment can be ignored (do not conflict with the specified arguments), then the following works like a charm:

# REPLACE   args = parser.parse_args()   with:
args, unknown = parser.parse_known_args()

From: https://mcmap.net/q/99951/-python-argparse-ignore-unrecognised-arguments

Aquaplane answered 18/6, 2022 at 15:52 Comment(0)
M
-1

If you don't want to change any of the arguments and working mechanisms from the original argparse function you have written or copied.

To let the program work then there is a simple solution that works most of the time.

You could just install jupyter-argparser using the below command:

pip install jupyter_argparser

The codes work without any changes thanks to the maintainer of the package.

Manis answered 25/7, 2020 at 9:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.