Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
RSE 23 Group Assignment Shervud Pitawanik Franz
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
Container 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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Niklas Franz
RSE 23 Group Assignment Shervud Pitawanik Franz
Commits
1e804ebf
Commit
1e804ebf
authored
1 year ago
by
niklasfranz
Browse files
Options
Downloads
Patches
Plain Diff
refactor: added logging and error handling
parent
1e6ed859
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Plotter.py
+129
-2
129 additions, 2 deletions
src/Plotter.py
with
129 additions
and
2 deletions
src/Plotter.py
+
129
−
2
View file @
1e804ebf
...
@@ -3,10 +3,14 @@ import matplotlib.pyplot as plt
...
@@ -3,10 +3,14 @@ import matplotlib.pyplot as plt
import
pandas
as
pd
import
pandas
as
pd
import
seaborn
as
sns
import
seaborn
as
sns
from
.Dataset
import
Dataset
from
.Dataset
import
Dataset
import
logging
class
Plotter
:
class
Plotter
:
def
__init__
(
self
,
dataset
:
Dataset
):
def
__init__
(
self
,
dataset
:
Dataset
):
if
type
(
dataset
)
!=
Dataset
:
logging
.
error
(
"
dataset parameter is not of type Dataset
"
)
raise
ValueError
(
f
"
{
dataset
}
is not of type Dataset
"
)
self
.
ds
=
dataset
self
.
ds
=
dataset
self
.
df
=
dataset
.
get_dataframe
()
self
.
df
=
dataset
.
get_dataframe
()
...
@@ -25,7 +29,7 @@ class Plotter:
...
@@ -25,7 +29,7 @@ class Plotter:
if
styling_params
.
get
(
"
title
"
):
if
styling_params
.
get
(
"
title
"
):
ax
.
set_title
(
styling_params
[
"
title
"
])
ax
.
set_title
(
styling_params
[
"
title
"
])
def
distribution_plot
(
self
,
target
)
->
None
:
def
distribution_plot
(
self
,
target
,
styling_params
=
{}
)
->
None
:
"""
plot a distribution plot.
"""
plot a distribution plot.
Args:
Args:
...
@@ -36,6 +40,24 @@ class Plotter:
...
@@ -36,6 +40,24 @@ class Plotter:
Returns:
Returns:
None
None
"""
"""
# implementing sensible logging and error catching
if
(
type
(
target
)
!=
str
):
logging
.
error
(
"
parameter target should be a string.
"
)
raise
ValueError
(
"
parameter target should be a string.
"
)
if
not
(
target
in
self
.
df
.
columns
):
logging
.
error
(
"
parameter target cannot be found in the dataset.
"
)
raise
ValueError
(
"
parameter target cannot be found in the dataset.
"
)
if
(
type
(
styling_params
)
!=
dict
):
logging
.
error
(
"
parameter styling params should be a dict.
"
)
raise
ValueError
(
"
parameter styling params should be a dict.
"
)
# plotting the plot
grouped_data
=
self
.
df
.
groupby
(
target
).
size
()
grouped_data
=
self
.
df
.
groupby
(
target
).
size
()
plt
.
barh
(
grouped_data
.
index
,
grouped_data
.
values
)
plt
.
barh
(
grouped_data
.
index
,
grouped_data
.
values
)
print
(
print
(
...
@@ -61,6 +83,32 @@ class Plotter:
...
@@ -61,6 +83,32 @@ class Plotter:
Returns:
Returns:
None
None
"""
"""
# implementing sensible logging and error catching
if
(
type
(
category1
)
!=
str
):
logging
.
error
(
"
parameter category1 should be a string.
"
)
raise
ValueError
(
"
parameter category1 should be a string.
"
)
if
not
(
category1
in
self
.
df
.
columns
):
logging
.
error
(
"
parameter category1 cannot be found in the dataset.
"
)
raise
ValueError
(
"
parameter category1 cannot be found in the dataset.
"
)
if
(
type
(
category2
)
!=
str
):
logging
.
error
(
"
parameter category2 should be a string.
"
)
raise
ValueError
(
"
parameter category2 should be a string.
"
)
if
not
(
category2
in
self
.
df
.
columns
):
logging
.
error
(
"
parameter category2 cannot be found in the dataset.
"
)
raise
ValueError
(
"
parameter category2 cannot be found in the dataset.
"
)
if
(
type
(
styling_params
)
!=
dict
):
logging
.
error
(
"
parameter styling params should be a dict.
"
)
raise
ValueError
(
"
parameter styling params should be a dict.
"
)
# plotting the plot
ct
=
pd
.
crosstab
(
self
.
df
[
category1
],
self
.
df
[
category2
])
ct
=
pd
.
crosstab
(
self
.
df
[
category1
],
self
.
df
[
category2
])
# Calculate percentages by row
# Calculate percentages by row
ct_percent
=
ct
.
apply
(
lambda
r
:
r
/
r
.
sum
()
*
100
,
axis
=
0
)
ct_percent
=
ct
.
apply
(
lambda
r
:
r
/
r
.
sum
()
*
100
,
axis
=
0
)
...
@@ -82,7 +130,32 @@ class Plotter:
...
@@ -82,7 +130,32 @@ class Plotter:
Returns:
Returns:
None
None
"""
"""
# implementing sensible logging and error catching
if
(
type
(
target
)
!=
str
):
logging
.
error
(
"
parameter target should be a string.
"
)
raise
ValueError
(
"
parameter target should be a string.
"
)
if
not
(
target
in
self
.
df
.
columns
):
logging
.
error
(
"
parameter target cannot be found in the dataset.
"
)
raise
ValueError
(
"
parameter target cannot be found in the dataset.
"
)
if
(
type
(
category
)
!=
str
):
logging
.
error
(
"
parameter category should be a string.
"
)
raise
ValueError
(
"
parameter category should be a string.
"
)
if
not
(
category
in
self
.
df
.
columns
):
logging
.
error
(
"
parameter category cannot be found in the dataset.
"
)
raise
ValueError
(
"
parameter category cannot be found in the dataset.
"
)
if
(
type
(
styling_params
)
!=
dict
):
logging
.
error
(
"
parameter styling params should be a dict.
"
)
raise
ValueError
(
"
parameter styling params should be a dict.
"
)
# plotting the plot
fig
,
ax
=
plt
.
subplots
()
fig
,
ax
=
plt
.
subplots
()
self
.
customize_plot
(
fig
,
ax
,
styling_params
)
self
.
customize_plot
(
fig
,
ax
,
styling_params
)
sns
.
boxplot
(
x
=
category
,
y
=
target
,
data
=
self
.
df
,
palette
=
"
rainbow
"
)
sns
.
boxplot
(
x
=
category
,
y
=
target
,
data
=
self
.
df
,
palette
=
"
rainbow
"
)
...
@@ -101,6 +174,33 @@ class Plotter:
...
@@ -101,6 +174,33 @@ class Plotter:
Returns:
Returns:
None
None
"""
"""
# implementing sensible logging and error catching
if
(
type
(
target
)
!=
str
):
logging
.
error
(
"
parameter target should be a string.
"
)
raise
ValueError
(
"
parameter target should be a string.
"
)
if
not
(
target
in
self
.
df
.
columns
):
logging
.
error
(
"
parameter target cannot be found in the dataset.
"
)
raise
ValueError
(
"
parameter target cannot be found in the dataset.
"
)
if
(
type
(
category
)
!=
str
):
logging
.
error
(
"
parameter category should be a string.
"
)
raise
ValueError
(
"
parameter category should be a string.
"
)
if
not
(
category
in
self
.
df
.
columns
):
logging
.
error
(
"
parameter category cannot be found in the dataset.
"
)
raise
ValueError
(
"
parameter category cannot be found in the dataset.
"
)
if
(
type
(
styling_params
)
!=
dict
):
logging
.
error
(
"
parameter styling params should be a dict.
"
)
raise
ValueError
(
"
parameter styling params should be a dict.
"
)
# plotting the plot
uniques
=
self
.
ds
.
get_unique_column_values
(
category
)
uniques
=
self
.
ds
.
get_unique_column_values
(
category
)
fig
,
ax
=
plt
.
subplots
()
fig
,
ax
=
plt
.
subplots
()
self
.
customize_plot
(
fig
,
ax
,
styling_params
)
self
.
customize_plot
(
fig
,
ax
,
styling_params
)
...
@@ -126,6 +226,33 @@ class Plotter:
...
@@ -126,6 +226,33 @@ class Plotter:
Returns:
Returns:
None
None
"""
"""
# implementing sensible logging and error catching
if
(
type
(
target1
)
!=
str
):
logging
.
error
(
"
parameter target1 should be a string.
"
)
raise
ValueError
(
"
parameter target1 should be a string.
"
)
if
not
(
target1
in
self
.
df
.
columns
):
logging
.
error
(
"
parameter target1 cannot be found in the dataset.
"
)
raise
ValueError
(
"
parameter target1 cannot be found in the dataset.
"
)
if
(
type
(
target2
)
!=
str
):
logging
.
error
(
"
parameter target2 should be a string.
"
)
raise
ValueError
(
"
parameter target2 should be a string.
"
)
if
not
(
target2
in
self
.
df
.
columns
):
logging
.
error
(
"
parameter target2 cannot be found in the dataset.
"
)
raise
ValueError
(
"
parameter target2 cannot be found in the dataset.
"
)
if
(
type
(
styling_params
)
!=
dict
):
logging
.
error
(
"
parameter styling params should be a dict.
"
)
raise
ValueError
(
"
parameter styling params should be a dict.
"
)
# plotting the plot
fig
,
ax
=
plt
.
subplots
()
fig
,
ax
=
plt
.
subplots
()
self
.
customize_plot
(
fig
,
ax
,
styling_params
)
self
.
customize_plot
(
fig
,
ax
,
styling_params
)
ax
.
scatter
(
self
.
df
[
target1
],
self
.
df
[
target2
])
ax
.
scatter
(
self
.
df
[
target1
],
self
.
df
[
target2
])
\ 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