iPython prompt confusion - ipython

I am having trouble getting the prompt to show the current working directory in iPython 0.13.1 running in windows. As you can see below, after changing the current working directory to 'C:\Users\Lou\Documents\Lou's Software\projects\loutilities', the \Y3 only shows 'C:/Users/Lou'. The same behavior is seen with \w and {cwd} configurations.
I'm sure this is something dumb I'm doing, but I haven't been able to figure it out, or find anything through Google or Stack Overflow.
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 0.13.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
[IPKernelApp] To connect another client to this kernel, use:
[IPKernelApp] --existing kernel-9572.json
C:/Users/Lou> cd "C:\Users\Lou\Documents\Lou's Software\projects\loutilities"
C:\Users\Lou\Documents\Lou's Software\projects\loutilities
C:/Users/Lou> config PromptManager
PromptManager options
-------------------
PromptManager.color_scheme=<Unicode>
Current: u'Linux'
PromptManager.in2_template=<Unicode>
Current: u' .\\D.: '
Continuation prompt.
PromptManager.in_template=<Unicode>
Current: u'\\Y3> '
Input prompt. '\#' will be transformed to the prompt number
PromptManager.justify=<Bool>
Current: False
If True (default), each prompt will be right-aligned with the preceding one.
PromptManager.out_template=<Unicode>
Current: u'Out[\\#]: '
Output prompt. '\#' will be transformed to the prompt number
Thomas asked for output of os.getcwdu(). I used config command to change the prompt (I'd gone back to In [#n] prompt), and In [#n] prompt remained. A clue?
In [1]: config PromptManager.in_template='\\Y3'
In [3]: config PromptManager
PromptManager options
-------------------
PromptManager.color_scheme=<Unicode>
Current: u'Linux'
PromptManager.in2_template=<Unicode>
Current: u' .\\D.: '
Continuation prompt.
PromptManager.in_template=<Unicode>
Current: u'\\Y3'
Input prompt. '\#' will be transformed to the prompt number
PromptManager.justify=<Bool>
Current: True
If True (default), each prompt will be right-aligned with the preceding one.
PromptManager.out_template=<Unicode>
Current: u'Out[\\#]: '
Output prompt. '\#' will be transformed to the prompt number
In [4]: import os
In [5]: os.getcwdu()
Out[5]: u'C:\\Users\\Lou'
In [6]: cd weather
(bookmark:weather) -> C:\Users\Lou\Documents\Lou's Software\projects\weather
C:\Users\Lou\Documents\Lou's Software\projects\weather
In [7]: os.getcwdu()
Out[7]: u"C:\\Users\\Lou\\Documents\\Lou's Software\\projects\\weather"
And the original test duplicated...
C:/Users/Lou> cd weather
(bookmark:weather) -> C:\Users\Lou\Documents\Lou's Software\projects\weather
C:\Users\Lou\Documents\Lou's Software\projects\weather
C:/Users/Lou> import os
C:/Users/Lou> os.getcwdu()
Out[3]: u"C:\\Users\\Lou\\Documents\\Lou's Software\\projects\\weather"
C:/Users/Lou> config PromptManager
PromptManager options
-------------------
PromptManager.color_scheme=<Unicode>
Current: u'Linux'
PromptManager.in2_template=<Unicode>
Current: u' .\\D.: '
Continuation prompt.
PromptManager.in_template=<Unicode>
Current: u'\\Y3> '
Input prompt. '\#' will be transformed to the prompt number
PromptManager.justify=<Bool>
Current: True
If True (default), each prompt will be right-aligned with the preceding one.
PromptManager.out_template=<Unicode>
Current: u'Out[\\#]: '
Output prompt. '\#' will be transformed to the prompt number
11/21/12 - check get_ipython().prompt_manager.templates:
C:/Users/Lou> pwd
Out[1]: u'C:\\Users\\Lou'
C:/Users/Lou> get_ipython().prompt_manager.templates
Out[2]:
{'in': u'{cwd_y[3]}> ',
'in2': u' .{dots}.: ',
'out': u'Out[{color.number}{count}{color.prompt}]: '}
C:/Users/Lou> cd weather
(bookmark:weather) -> C:\Users\Lou\Documents\Lou's Software\projects\weather
C:\Users\Lou\Documents\Lou's Software\projects\weather
C:/Users/Lou> pwd
Out[4]: u"C:\\Users\\Lou\\Documents\\Lou's Software\\projects\\weather"
C:/Users/Lou> get_ipython().prompt_manager.templates
Out[5]:
{'in': u'{cwd_y[3]}> ',
'in2': u' .{dots}.: ',
'out': u'Out[{color.number}{count}{color.prompt}]: '}

Related

Why are my wildcard attributes not being filled in Snakemake?

I am following the tutorial in the documentation (https://snakemake.readthedocs.io/en/stable/tutorial/advanced.html) and have been stuck on the "Step 4: Rule parameter" exercise. I would like to access a float from my config file using a wildcard in my params directive.
I seem to be getting the same error whenever I run snakemake -np in the command line:
InputFunctionException in line 46 of /mnt/c/Users/Matt/Desktop/snakemake-tutorial/Snakefile:
Error:
AttributeError: 'Wildcards' object has no attribute 'sample'
Wildcards:
Traceback:
File "/mnt/c/Users/Matt/Desktop/snakemake-tutorial/Snakefile", line 14, in get_bcftools_call_priors
This is my code so far
import time
configfile: "config.yaml"
rule all:
input:
"plots/quals.svg"
def get_bwa_map_input_fastqs(wildcards):
print(wildcards.__dict__, 1, time.time()) #I have this print as a check
return config["samples"][wildcards.sample]
def get_bcftools_call_priors(wildcards):
print(wildcards.__dict__, 2, time.time()) #I have this print as a check
return config["prior_mutation_rates"][wildcards.sample]
rule bwa_map:
input:
"data/genome.fa",
get_bwa_map_input_fastqs
#lambda wildcards: config["samples"][wildcards.sample]
output:
"mapped_reads/{sample}.bam"
params:
rg=r"#RG\tID:{sample}\tSM:{sample}"
threads: 2
shell:
"bwa mem -R '{params.rg}' -t {threads} {input} | samtools view -Sb - > {output}"
rule samtools_sort:
input:
"mapped_reads/{sample}.bam"
output:
"sorted_reads/{sample}.bam"
shell:
"samtools sort -T sorted_reads/{wildcards.sample} "
"-O bam {input} > {output}"
rule samtools_index:
input:
"sorted_reads/{sample}.bam"
output:
"sorted_reads/{sample}.bam.bai"
shell:
"samtools index {input}"
rule bcftools_call:
input:
fa="data/genome.fa",
bam=expand("sorted_reads/{sample}.bam", sample=config["samples"]),
bai=expand("sorted_reads/{sample}.bam.bai", sample=config["samples"])
#prior=get_bcftools_call_priors
params:
prior=get_bcftools_call_priors
output:
"calls/all.vcf"
shell:
"samtools mpileup -g -f {input.fa} {input.bam} | "
"bcftools call -P {params.prior} -mv - > {output}"
rule plot_quals:
input:
"calls/all.vcf"
output:
"plots/quals.svg"
script:
"scripts/plot-quals.py"
and here is my config.yaml
samples:
A: data/samples/A.fastq
#B: data/samples/B.fastq
#C: data/samples/C.fastq
prior_mutation_rates:
A: 1.0e-4
#B: 1.0e-6
I don't understand why my input function call in bcftools_call says that the wildcards object is empty of attributes, yet an almost identical function call in bwa_map has the attribute sample that I want. From the documentation it seems like the wildcards would be propogated before anything is run, so why is it missing?
This is the full output of the commandline call snakemake -np:
{'_names': {'sample': (0, None)}, '_allowed_overrides': ['index', 'sort'], 'index': functools.partial(<function Namedlist._used_attribute at 0x7f91b1a58f70>, _name='index'), 'sort': functools.partial(<function Namedlist._used_attribute at 0x7f91b1a58f70>, _name='sort'), 'sample': 'A'} 1 1628877061.8831172
Job stats:
job count min threads max threads
-------------- ------- ------------- -------------
all 1 1 1
bcftools_call 1 1 1
bwa_map 1 1 1
plot_quals 1 1 1
samtools_index 1 1 1
samtools_sort 1 1 1
total 6 1 1
[Fri Aug 13 10:51:01 2021]
rule bwa_map:
input: data/genome.fa, data/samples/A.fastq
output: mapped_reads/A.bam
jobid: 4
wildcards: sample=A
resources: tmpdir=/tmp
bwa mem -R '#RG\tID:A\tSM:A' -t 1 data/genome.fa data/samples/A.fastq | samtools view -Sb - > mapped_reads/A.bam
[Fri Aug 13 10:51:01 2021]
rule samtools_sort:
input: mapped_reads/A.bam
output: sorted_reads/A.bam
jobid: 3
wildcards: sample=A
resources: tmpdir=/tmp
samtools sort -T sorted_reads/A -O bam mapped_reads/A.bam > sorted_reads/A.bam
[Fri Aug 13 10:51:01 2021]
rule samtools_index:
input: sorted_reads/A.bam
output: sorted_reads/A.bam.bai
jobid: 5
wildcards: sample=A
resources: tmpdir=/tmp
samtools index sorted_reads/A.bam
[Fri Aug 13 10:51:01 2021]
rule bcftools_call:
input: data/genome.fa, sorted_reads/A.bam, sorted_reads/A.bam.bai
output: calls/all.vcf
jobid: 2
resources: tmpdir=/tmp
{'_names': {}, '_allowed_overrides': ['index', 'sort'], 'index': functools.partial(<function Namedlist._used_attribute at 0x7f91b1a58f70>, _name='index'), 'sort': functools.partial(<function Namedlist._used_attribute at 0x7f91b1a58f70>, _name='sort')} 2 1628877061.927639
InputFunctionException in line 46 of /mnt/c/Users/Matt/Desktop/snakemake-tutorial/Snakefile:
Error:
AttributeError: 'Wildcards' object has no attribute 'sample'
Wildcards:
Traceback:
File "/mnt/c/Users/Matt/Desktop/snakemake-tutorial/Snakefile", line 14, in get_bcftools_call_priors
If anyone knows what is going wrong I would really appreciate an explaination. Also if there is a better way of getting information out of the config.yaml into the different directives, I would gladly appreciate those tips.
Edit:
I have searched around the internet quite a bit, but have yet to understand this issue.
Wildcards for each rule are based on that rule's output file(s). The rule bcftools_call has one output file (calls/all.vcf), which has no wildcards. Because of this, when get_bcftools_call_priors is called, it throws an exception when it tries to access the unset wildcards.sample attribute.
You should probably set a global prior_mutation_rate in your config file and then access that in the bcftools_call rule:
rule bcftools_call:
...
params:
prior=config["prior_mutation_rate"],

Reading ipython's magic "precision" value

You can set printing precision in ipython by using magic function precision:
In [1]: %precision 2
Out[1]: '%.2f'
In [2]: 5/7
Out[2]: 0.71
You can also issue a command through ipython object:
ipython = get_ipython()
ipython.run_line_magic("precision", "2")
But how do you obtain the string '%.2f'?
Is there a command like ipython.get_magic_value('precision')?
Found the solution:
from IPython import get_ipython
ipython = get_ipython()
ipython.run_line_magic("precision", "3")
# you could also type %precision 3
form = ipython.display_formatter.formatters['text/plain']
form.float_format
#yields: '%.3f'
form.float_precision
#yields '3'

use pep8 to check the coding style in jupyter notebook

I have a problem in using pep8 to check the coding style in jupyter notebook.
install by:
in[1]
%install_ext https://raw.githubusercontent.com/SiggyF/notebooks/master/pep8_magic.py
out
Installed pep8_magic.py. To use it, type:
%load_ext pep8_magic
load by
in[2]
%load_ext pep8_magic
out
The pep8_magic extension is already loaded. To reload it, use:
%reload_ext pep8_magic
use it by
in[3]
%%pep8
a=1
out
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-3b6f6dbc7761> in <module>()
----> 1 get_ipython().run_cell_magic(u'pep8', u'', u'\na = 1')
/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.pyc in run_cell_magic(self, magic_name, line, cell)
2118 magic_arg_s = self.var_expand(line, stack_depth)
2119 with self.builtin_trap:
-> 2120 result = fn(magic_arg_s, cell)
2121 return result
2122
/home/cs/.ipython/extensions/pep8_magic.pyc in pep8(line, cell)
38 with tempfile.NamedTemporaryFile() as f:
39 # save to file
---> 40 f.write(bytes(cell + '\n', 'UTF-8'))
41 # make sure it's written
42 f.flush()
TypeError: str() takes at most 1 argument (2 given)
It seems that I have installed and loaded it successfully but I can not run the cell magic.
Later I found the problem was caused by some differences between python2 and python3. By using io.Bytes and f.write(cell+'\n') in pep2_magic.py, it worked. But unfortunately, it still can't output information of pep8, like the example here pep8 style guide

How to continuously show os command output in erlang?

I need continuously show stdout/stderr from os command in erlang.
In ruby I can implement it with following code:
s1, s2 , s3, t = Open3.popen3('for %a in (1 2 3 4 5 6 7 8 9) do (echo message & sleep 2 ) 2>&1 ')
s2.each do |l|
puts l
end
it will show 'message\n message\n' in 'real time' - does not wait end of process.
I've tried os:cmd(..)
and
1> P5 = erlang:open_port({spawn, "ruby rtest.rb"}, [stderr_to_stdout, in, exit_s
tatus, binary,stream, {line, 255}]).
#Port<0.505>
2> receive {P5, Data} -> io:format("Data ~p~n",[Data]) end.
Data {data,{eol,<<>>}}
ok
but both of them wait end of process.
Are the any optional for continuously stdout reading in Erlang?
EDIT:
In other words I look for a popen (c/c++; proc_open(php) and etc) function in erlang
EDIT2
Code, that works on linux (tested on centos6.2). Thanks for vinod:
-module(test).
-export([run/0]).
run() ->
P5 = erlang:open_port({spawn, "sh test.sh"},
[stderr_to_stdout, in, exit_status,stream, {line, 255}]),
loop(P5).
loop(P) ->
receive{P, Data} ->
io:format("Data ~p~n",[Data]),
loop(P)
end.
Output:
10> c(test).
{ok,test}
11> test:run().
Data {data,{eol,"1"}}
Data {data,{eol,"2"}}
Data {data,{eol,"3"}}
Data {data,{eol,"4"}}
Data {data,{eol,"5"}}
Data {data,{eol,"6"}}
Data {data,{eol,"7"}}
Data {data,{eol,"8"}}
Data {data,{eol,"9"}}
Data {data,{eol,"10"}}
Data {exit_status,0}
If I understand correctly, you want to continue to execute your program in concurrent with the os command. In Erlang you can just spawn a process which does that and you can continue. For example
1> spawn(fun() ->
P5 = erlang:open_port({spawn, "ruby rtest.rb"},
[stderr_to_stdout, in, exit_status,
binary,stream, {line, 255}]),
receive {P5, Data} ->
io:format("Data ~p~n",[Data])
end
end).
<0.32.0>
2> 5+5.
10
3>
Better to write in a module so that you can understand it better.

Shell variable name queried from Matlab has additional character

I'm working with the following script, run_test:
#!/bin/sh
temp=$1;
cat <<EOF | matlab
[status name] = unix('echo $temp');
disp(name);
% some Matlab code
test_complete = 1;
save(name)
exit
EOF
I want to pass a name to the script, run some code then save a .mat file with the name that was passed. However, there is a curious piece of behavior:
[energon2] ~ $ ./run_test 'run1'
Warning: No display specified. You will not be able to display graphics on the screen.
< M A T L A B (R) >
Copyright 1984-2010 The MathWorks, Inc.
Version 7.12.0.635 (R2011a) 64-bit (glnxa64)
March 18, 2011
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.
>> >> >> >> run1
>> >> >> >> >>
[energon2] ~ $ ls *.mat
run1?.mat
There is a "?" at the end of the file name when it's saved, but not when displayed on command line. This is acceptable for my needs, but a bit irritating to not know why it's occurring. Any explanation would be appreciated.
Edits, solution:
Yuk was correct below in the underlying cause and the use of save('$temp'). I'm now using the following script
#!/bin/sh
temp=$1;
cat <<EOF | matlab
% some Matlab code
test_complete = 1;
save('$temp')
exit
EOF
Thanks for the help.
You name variable has end-of-line as the last character. When you run echo run1 in unix this command display run1 and then "hit enter". In your script all the output of echo are saved to the name variable.
You can confirm it with the following:
>> format compact
>> [status, name] = unix('echo run1')
status =
0
name =
run1
>> numel(name)
ans =
5
>> int8(name(end))
ans =
10
>> int8(sprintf('\n'))
ans =
10
Apparently this character can be a part of a file name in unix, but shell displays it as ?.
Can't you do save($temp) instead?
EDIT: See my comments below for correction and more explanation.