Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added func.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added labyrinth.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added loaded_labyrinth.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added medic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added modulated_signal.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
109 changes: 107 additions & 2 deletions solutions/sem02/lesson07/task1.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,110 @@ def visualize_diagrams(
ordinates: np.ndarray,
diagram_type: Any,
) -> None:
# ваш код
pass
if abscissa.shape != ordinates.shape:
raise ShapeMismatchError

if not (diagram_type == "hist" or diagram_type == "violin" or diagram_type == "box"):
raise ValueError

space = 0.2

figure = plt.figure(figsize=(8, 8))
grid = plt.GridSpec(4, 4, wspace=space, hspace=space)

axis_scatter = figure.add_subplot(grid[:-1, 1:])
axis_vert = figure.add_subplot(
grid[:-1, 0],
sharey=axis_scatter,
)

axis_hor = figure.add_subplot(
grid[-1, 1:],
sharex=axis_scatter,
)

axis_scatter.scatter(abscissa, ordinates, color="deeppink", alpha=0.3)

if diagram_type == "hist":
axis_hor.hist(
abscissa,
bins=50,
color="lightpink",
density=True,
alpha=0.5,
edgecolor="palevioletred",
linewidth=1,
)

axis_vert.hist(
ordinates,
bins=50,
color="lightpink",
orientation="horizontal",
density=True,
alpha=0.5,
edgecolor="palevioletred",
linewidth=1,
)

axis_hor.invert_yaxis()
axis_vert.invert_xaxis()

if diagram_type == "violin":
violin_hor = axis_hor.violinplot(
abscissa,
vert=False,
showmedians=True,
)

for body in violin_hor["bodies"]:
body.set_facecolor("violet")
body.set_edgecolor("darkmagenta")
body.set_linewidth(2)

for part in violin_hor:
if part == "bodies":
continue

violin_hor[part].set_edgecolor("darkmagenta")

violin_vert = axis_vert.violinplot(
ordinates,
vert=True,
showmedians=True,
)

for body in violin_vert["bodies"]:
body.set_facecolor("violet")
body.set_edgecolor("darkmagenta")
body.set_linewidth(2)

for part in violin_vert:
if part == "bodies":
continue

violin_vert[part].set_edgecolor("darkmagenta")

if diagram_type == "box":
axis_hor.boxplot(
ordinates,
vert=False,
patch_artist=True,
boxprops=dict(facecolor="lightcoral"),
medianprops=dict(color="firebrick"),
)
axis_vert.set_xticks([])
axis_vert.set_xlabel("y values")

axis_vert.boxplot(
abscissa,
vert=True,
patch_artist=True,
boxprops=dict(facecolor="lightcoral"),
medianprops=dict(color="firebrick"),
)
axis_hor.set_yticks([])
axis_hor.set_xlabel("x values")


if __name__ == "__main__":
Expand All @@ -25,4 +127,7 @@ def visualize_diagrams(
abscissa, ordinates = np.random.multivariate_normal(mean, cov, size=1000).T

visualize_diagrams(abscissa, ordinates, "hist")
visualize_diagrams(abscissa, ordinates, "violin")
visualize_diagrams(abscissa, ordinates, "box")

plt.show()
42 changes: 41 additions & 1 deletion solutions/sem02/lesson07/task2.py
Original file line number Diff line number Diff line change
@@ -1 +1,41 @@
# ваш код (используйте функции или классы для решения данной задачи)
import matplotlib.pyplot as plt
import numpy as np

before = [0]*4
after = [0]*4
file_path = 'D:/GitHub/python_mipt_dafe_tasks/solutions/sem02/lesson07/data/medic_data.json'
with open(file_path, 'r') as file:
content = file.read()

before_part = content.split('"before"')[1].split('"after"')[0]
after_part = content.split('"after"')[1]

before[0] = before_part.count('"I"') - before_part.count('"IV"')
before[1] = before_part.count('"II"')
before[2] = before_part.count('"III"')
before[3] = before_part.count('"IV"')

after[0] = after_part.count('"I"') - after_part.count('"IV"')
after[1] = after_part.count('"II"')
after[2] = after_part.count('"III"')
after[3] = after_part.count('"IV"')

figure, axis = plt.subplots(figsize=(9, 9))

stages = np.array(['I', 'II', 'III', 'IV'])
axis.set_xticks(np.arange(stages.size), labels=stages, weight="bold")

x = np.arange(4)
width = 0.35

axis.bar(x - width/2, before, width, color='goldenrod', edgecolor='brown')
axis.bar(x + width/2, after, width, color='lightgreen', edgecolor='olive')

axis.set_ylabel('amount of people')
axis.set_title('Mitral desease stages')
axis.legend(["before", "after"], title="Desease stages")

figure.savefig("medic.png", bbox_inches="tight")
plt.show()


67 changes: 50 additions & 17 deletions solutions/sem02/lesson08/task1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,60 @@


def create_modulation_animation(
modulation,
fc,
num_frames,
plot_duration,
time_step=0.001,
animation_step=0.01,
save_path=""
modulation, fc, num_frames, plot_duration, time_step=0.001, animation_step=0.01, save_path=""
) -> FuncAnimation:
# ваш код
return FuncAnimation()
abscissa = np.arange(0, plot_duration, time_step)

def signal(abscissa, modulation, fc):
f = np.sin(2 * np.pi * fc * abscissa)

if modulation is None:
return f
else:
return modulation(abscissa) * f

figure, axis = plt.subplots(figsize=(16, 9))
axis.set_xlim(0, plot_duration)

line, *_ = axis.plot(
abscissa,
signal(abscissa, modulation, fc),
c="deeppink",
)

def update_frame(
frame_id: int,
) -> tuple[plt.Line2D]:
new_x = abscissa + frame_id * animation_step
sign = signal(new_x, modulation, fc)
line.set_ydata(sign)

return (line,)

animation = FuncAnimation(
figure,
update_frame,
frames=num_frames,
interval=50,
blit=True,
)

if save_path:
animation.save("func.gif", writer="pillow", fps=24)

return animation


if __name__ == "__main__":

def modulation_function(t):
return np.cos(t * 6)
return np.cos(t * 6)

num_frames = 100
plot_duration = np.pi / 2
time_step = 0.001
animation_step = np.pi / 200
fc = 50
num_frames = 100
plot_duration = np.pi / 2
time_step = 0.001
animation_step = np.pi / 200
fc = 50
save_path_with_modulation = "modulated_signal.gif"

animation = create_modulation_animation(
Expand All @@ -38,6 +71,6 @@ def modulation_function(t):
plot_duration=plot_duration,
time_step=time_step,
animation_step=animation_step,
save_path=save_path_with_modulation
save_path=save_path_with_modulation,
)
HTML(animation.to_jshtml())
plt.show()
Loading
Loading