Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
bigWig_visu
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LBMC
ReGArDS
Projects_Analyzes
bigWig_visu
Commits
6fd5c886
Commit
6fd5c886
authored
4 years ago
by
nfontrod
Browse files
Options
Downloads
Patches
Plain Diff
src/gc_content/stat_annot.py: major pep8 modifications
parent
0117f4ca
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/gc_content/stat_annot.py
+114
-106
114 additions, 106 deletions
src/gc_content/stat_annot.py
with
114 additions
and
106 deletions
src/gc_content/stat_annot.py
+
114
−
106
View file @
6fd5c886
#!/usr/bin/env python3
# -*- coding
;
:
utf
-8 -*-
# -*- coding:
UTF
-8 -*-
"""
Description:
...
...
@@ -9,6 +9,7 @@ Description:
github.com/webermarcolivier/statannot/blob/master/statannot/statannot.py.
"""
import
matplotlib.pyplot
as
plt
from
matplotlib
import
lines
import
matplotlib.transforms
as
mtransforms
...
...
@@ -19,196 +20,203 @@ from seaborn.utils import remove_na
def
add_stat_annotation
(
ax
,
data
=
None
,
x
=
None
,
y
=
None
,
hue
=
None
,
order
=
None
,
hue_order
=
None
,
boxPairList
=
None
,
loc
=
'
inside
'
,
useFixedOffset
=
False
,
lineYOffsetToBoxAxesCoord
=
None
,
lineYOffsetAxesCoord
=
None
,
lineHeightAxesCoord
=
0.02
,
textYOffsetPoints
=
1
,
color
=
'
0.2
'
,
linewidth
=
1.5
,
fontsize
=
'
medium
'
,
verbose
=
1
):
hue_order
=
None
,
box_pair_list
=
None
,
loc
=
'
inside
'
,
use_fixed_offset
=
False
,
liney_offset2box_axes_coord
=
None
,
liney_offset_axes_coord
=
None
,
line_height_axes_coord
=
0.02
,
text_y_offset_points
=
1
,
color
=
'
0.2
'
,
linewidth
=
1.5
,
fontsize
=
'
medium
'
,
verbose
=
1
,
min_pval
=
1e-16
):
"""
User should use the same argument for the data, x, y, hue, order,
\
hue_order as the seaborn boxplot function.
box
P
air
L
ist can be of either form:
box
_p
air
_l
ist can be of either form:
boxplot: [(cat1, cat2, pval), (cat3, cat4, pval)]
"""
def
find_x_position_box
(
box
P
lotter
,
box
N
ame
):
def
find_x_position_box
(
box
_p
lotter
,
box
_n
ame
):
"""
box
N
ame can be either a name
"
cat
"
or a tuple (
"
cat
"
,
"
hue
"
)
box
_n
ame can be either a name
"
cat
"
or a tuple (
"
cat
"
,
"
hue
"
)
"""
if
box
P
lotter
.
plot_hues
is
None
:
cat
=
box
N
ame
hue
O
ffset
=
0
if
box
_p
lotter
.
plot_hues
is
None
:
cat
=
box
_n
ame
hue
_o
ffset
=
0
else
:
cat
=
box
N
ame
[
0
]
hue
=
box
N
ame
[
1
]
hue
O
ffset
=
box
P
lotter
.
hue_offsets
[
box
P
lotter
.
hue_names
.
index
(
hue
)]
cat
=
box
_n
ame
[
0
]
c
hue
=
box
_n
ame
[
1
]
hue
_o
ffset
=
box
_p
lotter
.
hue_offsets
[
box
_p
lotter
.
hue_names
.
index
(
c
hue
)]
group
P
os
=
box
P
lotter
.
group_names
.
index
(
cat
)
box
P
os
=
group
P
os
+
hue
O
ffset
return
box
P
os
group
_p
os
=
box
_p
lotter
.
group_names
.
index
(
cat
)
box
_p
os
=
group
_p
os
+
hue
_o
ffset
return
box
_p
os
def
get_box_data
(
box
P
lotter
,
box
N
ame
):
def
get_box_data
(
c
box
_p
lotter
,
box
_n
ame
):
"""
box
N
ame can be either a name
"
cat
"
or a tuple (
"
cat
"
,
"
hue
"
)
box
_n
ame can be either a name
"
cat
"
or a tuple (
"
cat
"
,
"
hue
"
)
Here we really have to duplicate seaborn code, because there is not
direct access to the
box_data in the BoxPlotter class.
"""
cat
=
box
N
ame
if
box
P
lotter
.
plot_hues
is
None
:
cat
=
box
_n
ame
if
c
box
_p
lotter
.
plot_hues
is
None
:
box_max_l
=
[]
for
cat
in
box
P
lotter
.
group_names
:
i
=
box
P
lotter
.
group_names
.
index
(
cat
)
group_data
=
box
P
lotter
.
plot_data
[
i
]
for
cat
in
c
box
_p
lotter
.
group_names
:
i
=
c
box
_p
lotter
.
group_names
.
index
(
cat
)
group_data
=
c
box
_p
lotter
.
plot_data
[
i
]
box_data
=
remove_na
(
group_data
)
box_max_l
.
append
(
np
.
m
ax
(
box_data
))
box_max_l
.
append
(
np
.
m
ean
(
box_data
))
return
max
(
box_max_l
)
else
:
i
=
box
P
lotter
.
group_names
.
index
(
cat
[
0
])
group_data
=
box
P
lotter
.
plot_data
[
i
]
i
=
c
box
_p
lotter
.
group_names
.
index
(
cat
[
0
])
group_data
=
c
box
_p
lotter
.
plot_data
[
i
]
box_data
=
[]
for
hue
in
box
P
lotter
.
hue_names
:
hue_mask
=
box
P
lotter
.
plot_hues
[
i
]
==
hue
for
c
hue
in
c
box
_p
lotter
.
hue_names
:
hue_mask
=
c
box
_p
lotter
.
plot_hues
[
i
]
==
c
hue
box_data
.
append
(
np
.
max
(
remove_na
(
group_data
[
hue_mask
])))
return
max
(
box_data
)
fig
=
plt
.
gcf
()
valid
L
ist
=
[
'
inside
'
,
'
outside
'
]
if
loc
not
in
valid
L
ist
:
valid
_l
ist
=
[
'
inside
'
,
'
outside
'
]
if
loc
not
in
valid
_l
ist
:
raise
ValueError
(
f
"
loc value should be one of the following:
"
f
"
{
'
,
'
.
join
(
valid
L
ist
)
}
.
"
)
f
"
{
'
,
'
.
join
(
valid
_l
ist
)
}
.
"
)
# Create the same BoxPlotter object as seaborn's boxplot
box
P
lotter
=
sns
.
categorical
.
_BoxPlotter
(
x
,
y
,
hue
,
data
,
order
,
hue_order
,
orient
=
None
,
width
=
.
8
,
color
=
None
,
palette
=
None
,
saturation
=
.
75
,
dodge
=
True
,
fliersize
=
5
,
linewidth
=
None
)
box
_p
lotter
=
sns
.
categorical
.
_BoxPlotter
(
x
,
y
,
hue
,
data
,
order
,
hue_order
,
orient
=
None
,
width
=
.
8
,
color
=
None
,
palette
=
None
,
saturation
=
.
75
,
dodge
=
True
,
fliersize
=
5
,
linewidth
=
None
)
ylim
=
ax
.
get_ylim
()
y
R
ange
=
ylim
[
1
]
-
ylim
[
0
]
y
_r
ange
=
ylim
[
1
]
-
ylim
[
0
]
if
line
YO
ffset
A
xes
C
oord
is
None
:
if
line
y_o
ffset
_a
xes
_c
oord
is
None
:
if
loc
==
'
inside
'
:
line
YO
ffset
A
xes
C
oord
=
0.005
if
line
YO
ffset
ToBoxA
xes
C
oord
is
None
:
line
YO
ffset
ToBoxA
xes
C
oord
=
0.1
line
y_o
ffset
_a
xes
_c
oord
=
0.005
if
line
y_o
ffset
2box_a
xes
_c
oord
is
None
:
line
y_o
ffset
2box_a
xes
_c
oord
=
0.1
elif
loc
==
'
outside
'
:
line
YO
ffset
A
xes
C
oord
=
0.03
line
YO
ffset
ToBoxA
xes
C
oord
=
line
YO
ffset
A
xes
C
oord
line
y_o
ffset
_a
xes
_c
oord
=
0.03
line
y_o
ffset
2box_a
xes
_c
oord
=
line
y_o
ffset
_a
xes
_c
oord
else
:
if
loc
==
'
inside
'
:
if
line
YO
ffset
ToBoxA
xes
C
oord
is
None
:
line
YO
ffset
ToBoxA
xes
C
oord
=
0.06
if
line
y_o
ffset
2box_a
xes
_c
oord
is
None
:
line
y_o
ffset
2box_a
xes
_c
oord
=
0.06
elif
loc
==
'
outside
'
:
line
YO
ffset
ToBoxA
xes
C
oord
=
line
YO
ffset
A
xes
C
oord
y
O
ffset
=
line
YO
ffset
A
xes
C
oord
*
y
R
ange
y
O
ffset
ToB
ox
=
line
YO
ffset
ToBoxA
xes
C
oord
*
y
R
ange
line
y_o
ffset
2box_a
xes
_c
oord
=
line
y_o
ffset
_a
xes
_c
oord
y
_o
ffset
=
line
y_o
ffset
_a
xes
_c
oord
*
y
_r
ange
y
_o
ffset
_to_b
ox
=
line
y_o
ffset
2box_a
xes
_c
oord
*
y
_r
ange
yStack
=
[]
annList
=
[]
for
box1
,
box2
,
pval
in
boxPairList
:
y_stack
=
[]
ann_list
=
[]
max_val
=
[]
for
box1
,
box2
,
pval
in
box_pair_list
:
group
N
ames
=
box
P
lotter
.
group_names
group
_n
ames
=
box
_p
lotter
.
group_names
cat1
=
box1
cat2
=
box2
if
isinstance
(
cat1
,
tuple
):
hue_names
=
box
P
lotter
.
hue_names
valid
=
cat1
[
0
]
in
group
N
ames
and
cat2
[
0
]
in
group
N
ames
and
\
cat1
[
1
]
in
hue_names
and
cat2
[
1
]
in
hue_names
hue_names
=
box
_p
lotter
.
hue_names
valid
=
cat1
[
0
]
in
group
_n
ames
and
cat2
[
0
]
in
group
_n
ames
and
\
cat1
[
1
]
in
hue_names
and
cat2
[
1
]
in
hue_names
else
:
valid
=
cat1
in
group
N
ames
and
cat2
in
group
N
ames
valid
=
cat1
in
group
_n
ames
and
cat2
in
group
_n
ames
if
valid
:
# Get position of boxes
x1
=
find_x_position_box
(
box
P
lotter
,
box1
)
x2
=
find_x_position_box
(
box
P
lotter
,
box2
)
box_data1
=
get_box_data
(
box
P
lotter
,
box1
)
box_data2
=
get_box_data
(
box
P
lotter
,
box2
)
x1
=
find_x_position_box
(
box
_p
lotter
,
box1
)
x2
=
find_x_position_box
(
box
_p
lotter
,
box2
)
box_data1
=
get_box_data
(
box
_p
lotter
,
box1
)
box_data2
=
get_box_data
(
box
_p
lotter
,
box2
)
ymax1
=
box_data1
ymax2
=
box_data2
if
pval
>
1e-16
:
text
=
"
p = {:.2e}
"
.
format
(
pval
)
else
:
text
=
"
p <
1e-16
"
text
=
f
"
p <
{
min_pval
:
.
0
e
}
"
if
loc
==
'
inside
'
:
y
R
ef
=
max
(
ymax1
,
ymax2
)
y
_r
ef
=
max
(
ymax1
,
ymax2
)
else
:
yRef
=
ylim
[
1
]
if
len
(
yStack
)
>
0
and
isinstance
(
box1
,
str
):
yRef2
=
max
(
yRef
,
max
(
yStack
))
y_ref
=
ylim
[
1
]
if
len
(
y_stack
)
>
0
:
y_ref2
=
max
(
y_ref
,
max
(
y_stack
))
else
:
y
R
ef2
=
y
R
ef
y
_r
ef2
=
y
_r
ef
if
len
(
y
S
tack
)
==
0
or
not
isinstance
(
box1
,
str
)
:
y
=
y
R
ef2
+
y
O
ffset
ToB
ox
if
len
(
y
_s
tack
)
==
0
:
y
=
y
_r
ef2
+
y
_o
ffset
_to_b
ox
else
:
y
=
y
R
ef2
+
y
O
ffset
h
=
line
H
eight
A
xes
C
oord
*
y
R
ange
line
X
,
line
Y
=
[
x1
,
x1
,
x2
,
x2
],
[
y
,
y
+
h
,
y
+
h
,
y
]
y
=
y
_r
ef2
+
y
_o
ffset
h
=
line
_h
eight
_a
xes
_c
oord
*
y
_r
ange
line
_x
,
line
_y
=
[
x1
,
x1
,
x2
,
x2
],
[
y
,
y
+
h
,
y
+
h
,
y
]
if
loc
==
'
inside
'
:
ax
.
plot
(
line
X
,
line
Y
,
lw
=
linewidth
,
c
=
color
)
ax
.
plot
(
line
_x
,
line
_y
,
lw
=
linewidth
,
c
=
color
)
elif
loc
==
'
outside
'
:
line
=
lines
.
Line2D
(
line
X
,
line
Y
,
lw
=
linewidth
,
c
=
color
,
line
=
lines
.
Line2D
(
line
_x
,
line
_y
,
lw
=
linewidth
,
c
=
color
,
transform
=
ax
.
transData
)
line
.
set_clip_on
(
False
)
ax
.
add_line
(
line
)
if
text
is
not
None
:
ann
=
ax
.
annotate
(
text
,
xy
=
(
np
.
mean
([
x1
,
x2
]),
y
+
h
),
xytext
=
(
0
,
text
YO
ffset
P
oints
),
xytext
=
(
0
,
text
_y_o
ffset
_p
oints
),
textcoords
=
'
offset points
'
,
xycoords
=
'
data
'
,
ha
=
'
center
'
,
va
=
'
bottom
'
,
fontsize
=
fontsize
,
clip_on
=
False
,
annotation_clip
=
False
)
ann
L
ist
.
append
(
ann
)
fontsize
=
fontsize
,
clip_on
=
False
,
annotation_clip
=
False
)
ann
_l
ist
.
append
(
ann
)
new_max_ylim
=
1.1
*
(
y
+
h
)
new_max_ylim
=
1.1
*
(
y
+
h
)
if
new_max_ylim
>
ylim
[
1
]:
ax
.
set_ylim
((
ylim
[
0
],
1.1
*
(
y
+
h
)))
ax
.
set_ylim
((
ylim
[
0
],
1.1
*
(
y
+
h
)))
if
text
is
not
None
:
plt
.
draw
()
y
TopA
nnot
=
None
got
M
atplotlib
E
rror
=
False
if
not
use
F
ixed
O
ffset
:
y
_top_a
nnot
=
None
got
_m
atplotlib
_e
rror
=
False
if
not
use
_f
ixed
_o
ffset
:
try
:
bbox
=
ann
.
get_window_extent
()
bbox_data
=
bbox
.
transformed
(
ax
.
transData
.
inverted
())
y
TopA
nnot
=
bbox_data
.
ymax
y
_top_a
nnot
=
bbox_data
.
ymax
except
RuntimeError
:
got
M
atplotlib
E
rror
=
True
got
_m
atplotlib
_e
rror
=
True
if
use
F
ixed
O
ffset
or
got
M
atplotlib
E
rror
:
if
use
_f
ixed
_o
ffset
or
got
_m
atplotlib
_e
rror
:
if
verbose
>=
1
:
print
(
"
Warning: cannot get the text bounding box.
"
"
Falling back to a fixed y offset. Layout may
"
"
be not optimal.
"
)
# We will apply a fixed offset in points,
based on the font size of the annotation.
fontsize
Points
=
FontProperties
(
size
=
'
medium
'
).
get_size_in_points
()
offsetTrans
=
mtransforms
.
offset_copy
(
ax
.
transData
,
fig
=
fig
,
x
=
0
,
y
=
1.0
*
fontsize
P
oints
+
text
YO
ffset
P
oints
,
units
=
'
points
'
)
y
TopD
isplay
=
offset
T
rans
.
transform
((
0
,
y
+
h
))
y
TopA
nnot
=
ax
.
transData
.
inverted
().
transform
(
yTopDisplay
)[
1
]
#
We will apply a fixed offset in points,
# based on the
font
size
of the annotation.
fontsize_points
=
FontProperties
(
size
=
'
medium
'
).
get_size_in_points
()
offset_trans
=
mtransforms
.
offset_copy
(
ax
.
transData
,
fig
=
fig
,
x
=
0
,
y
=
1.0
*
fontsize
_p
oints
+
text
_y_o
ffset
_p
oints
,
units
=
'
points
'
)
y
_top_d
isplay
=
offset
_t
rans
.
transform
((
0
,
y
+
h
))
y
_top_a
nnot
=
ax
.
transData
.
inverted
().
\
transform
(
y_top_display
)[
1
]
else
:
y
TopA
nnot
=
y
+
h
y
_top_a
nnot
=
y
+
h
yStack
.
append
(
yTopAnnot
)
y_stack
.
append
(
y_top_annot
)
max_val
.
append
(
max
(
y_stack
))
if
hue
is
not
None
and
len
(
y_stack
)
==
len
(
box_plotter
.
hue_names
):
y_stack
=
[]
else
:
raise
ValueError
(
"
box
P
air
L
ist contains an unvalid box pair.
"
)
raise
ValueError
(
"
box
_p
air
_l
ist contains an unvalid box pair.
"
)
y
S
tack
M
ax
=
max
(
yS
tack
)
y
_s
tack
_m
ax
=
max
(
max_val
+
y_s
tack
)
if
loc
==
'
inside
'
:
if
ylim
[
1
]
<
1.03
*
y
S
tack
M
ax
:
ax
.
set_ylim
((
ylim
[
0
],
1.03
*
y
S
tack
M
ax
))
if
ylim
[
1
]
<
1.03
*
y
_s
tack
_m
ax
:
ax
.
set_ylim
((
ylim
[
0
],
1.03
*
y
_s
tack
_m
ax
))
elif
loc
==
'
outside
'
:
ax
.
set_ylim
((
ylim
[
0
],
ylim
[
1
]))
return
ax
return
ax
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment