Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F121690526
visu.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Jul 13, 04:56
Size
4 KB
Mime Type
text/x-python
Expires
Tue, Jul 15, 04:56 (2 d)
Engine
blob
Format
Raw Data
Handle
27374949
Attached To
R13271 Optical_Trapping_ML
visu.py
View Options
import
matplotlib.pyplot
as
plt
from
collections
import
Counter
import
numpy
as
np
import
seaborn
as
sns
import
pandas
as
pd
def
plot_predictions_vs_actual
(
y_true
,
y_pred
,
title
):
plt
.
figure
(
figsize
=
(
10
,
6
))
sns
.
scatterplot
(
x
=
y_true
,
y
=
y_pred
)
plt
.
xlabel
(
'Actual'
)
plt
.
ylabel
(
'Predicted'
)
plt
.
title
(
title
)
plt
.
show
()
def
plot_classifications_by_antibiotic_level_with_counts
(
docs
):
antibiotics_levels
=
[]
living_states
=
[]
for
doc
in
docs
:
antibiotics_levels
.
append
(
doc
[
'antibiotics_quantity'
])
living_states
.
append
(
doc
[
'living_state'
])
data
=
pd
.
DataFrame
({
'antibiotics_quantity'
:
antibiotics_levels
,
'living_state'
:
living_states
})
# Create a count plot
plt
.
figure
(
figsize
=
(
14
,
8
))
sns
.
countplot
(
x
=
'antibiotics_quantity'
,
hue
=
'living_state'
,
data
=
data
,
palette
=
'viridis'
)
plt
.
xlabel
(
'Antibiotics Quantity'
)
plt
.
ylabel
(
'Count'
)
plt
.
title
(
'Model Classifications by Antibiotics Levels'
)
plt
.
legend
(
title
=
'Living State'
,
loc
=
'upper right'
,
labels
=
[
'Alive (0)'
,
'Dead (1)'
])
plt
.
show
()
def
plot_classifications_by_antibiotic_level
(
docs
,
model
,
feature_set
):
antibiotics_levels
=
[]
living_states
=
[]
for
doc
in
docs
:
antibiotics_levels
.
append
(
doc
[
'antibiotics_quantity'
])
living_states
.
append
(
doc
[
'living_state'
])
# Create a scatter plot
plt
.
figure
(
figsize
=
(
12
,
8
))
plt
.
scatter
(
antibiotics_levels
,
living_states
,
c
=
living_states
,
cmap
=
'viridis'
,
marker
=
'o'
,
edgecolor
=
'k'
)
plt
.
xlabel
(
'Antibiotics Quantity'
)
plt
.
ylabel
(
'Living State'
)
plt
.
title
(
'Model Classifications by Antibiotics Levels'
)
plt
.
colorbar
(
label
=
'Living State'
)
plt
.
xticks
(
list
(
set
(
antibiotics_levels
)))
plt
.
show
()
def
plot_feature_correlations
(
features
):
plt
.
figure
(
figsize
=
(
20
,
16
))
# Increase figure size for better readability
correlation_matrix
=
features
.
corr
()
sns
.
heatmap
(
correlation_matrix
,
annot
=
True
,
fmt
=
".2f"
,
cmap
=
"coolwarm"
,
annot_kws
=
{
"size"
:
8
},
cbar_kws
=
{
"shrink"
:
0.75
})
plt
.
title
(
"Feature Correlation Matrix"
,
fontsize
=
20
)
plt
.
xticks
(
rotation
=
90
,
fontsize
=
10
)
plt
.
yticks
(
fontsize
=
10
)
plt
.
tight_layout
()
# Adjust layout to prevent clipping of tick-labels
plt
.
savefig
(
'feature_corr.png'
)
# Save plot to file
plt
.
close
()
def
plot_feature_importances
(
model
,
features
):
importances
=
model
.
feature_importances_
indices
=
np
.
argsort
(
importances
)[::
-
1
]
feature_names
=
features
.
columns
plt
.
figure
(
figsize
=
(
14
,
8
))
plt
.
title
(
"Feature Importances"
)
plt
.
bar
(
range
(
len
(
importances
)),
importances
[
indices
],
align
=
"center"
)
plt
.
xticks
(
range
(
len
(
importances
)),
feature_names
[
indices
],
rotation
=
90
)
plt
.
tight_layout
()
plt
.
savefig
(
'feature_imp.png'
)
# Save plot to file
plt
.
close
()
def
display_counts
(
docs
,
label
):
antibiotics_counts
=
Counter
(
doc
[
'antibiotics_quantity'
]
for
doc
in
docs
)
print
(
f
"
\n
{label} Set Antibiotics Counts:"
)
for
qty
,
count
in
antibiotics_counts
.
items
():
print
(
f
"{qty}: {count}"
)
def
display_data
(
data
):
mic_counter
=
Counter
(
doc
[
'antibiotics_quantity'
]
for
doc
in
data
)
print
(
"
\n
antibiotics Counts:"
)
for
mic
,
count
in
mic_counter
.
items
():
print
(
f
"{mic}: {count}"
)
def
plot_normalized_data
(
normalized_doc
):
plt
.
figure
(
figsize
=
(
14
,
8
))
# Plot data for OFF state
if
'data_OFF'
in
normalized_doc
:
x_off
=
range
(
normalized_doc
[
'indices_OFF'
][
0
],
normalized_doc
[
'indices_OFF'
][
0
]
+
len
(
normalized_doc
[
'data_OFF'
]))
plt
.
plot
(
x_off
,
normalized_doc
[
'data_OFF'
],
label
=
'OFF'
,
color
=
'blue'
)
# Plot data for ON state
if
'data_ON'
in
normalized_doc
:
x_on
=
range
(
normalized_doc
[
'indices_ON'
][
0
],
normalized_doc
[
'indices_ON'
][
0
]
+
len
(
normalized_doc
[
'data_ON'
]))
plt
.
plot
(
x_on
,
normalized_doc
[
'data_ON'
],
label
=
'ON'
,
color
=
'green'
)
# Plot data for Trapping state
if
'data_Trapping'
in
normalized_doc
:
x_trap
=
range
(
normalized_doc
[
'indices_Trapping'
][
0
],
normalized_doc
[
'indices_Trapping'
][
0
]
+
len
(
normalized_doc
[
'data_Trapping'
]))
plt
.
plot
(
x_trap
,
normalized_doc
[
'data_Trapping'
],
label
=
'Trapping'
,
color
=
'red'
)
# Plot data for on_to_trapping state
if
'data_on_to_trapping'
in
normalized_doc
:
x_on_to_trapping
=
range
(
normalized_doc
[
'indices_on_to_trapping'
][
0
],
normalized_doc
[
'indices_on_to_trapping'
][
0
]
+
len
(
normalized_doc
[
'data_on_to_trapping'
]))
plt
.
plot
(
x_on_to_trapping
,
normalized_doc
[
'data_on_to_trapping'
],
label
=
'on_to_trapping'
,
color
=
'purple'
)
# Adding labels and title
plt
.
xlabel
(
'Indices'
)
plt
.
ylabel
(
'Normalized Transmission'
)
plt
.
title
(
'Normalized Transmission Data for Different States'
)
plt
.
legend
()
plt
.
show
()
Event Timeline
Log In to Comment