-
Notifications
You must be signed in to change notification settings - Fork 50
Б12-514 Беляев Кирилл #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
44bb89a
ce7e3bb
1d6daa4
b4f749b
38977ea
70a0be0
345fb82
bbbc252
d2e8c00
4c6fd0e
f0626c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,11 +9,32 @@ def convert_from_sphere( | |
| distances: np.ndarray, | ||
| azimuth: np.ndarray, | ||
| inclination: np.ndarray, | ||
| ) -> tuple[np.ndarray, np.ndarray, np.ndarray]: ... | ||
| ) -> tuple[np.ndarray, np.ndarray, np.ndarray]: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не проходит ruff format |
||
| if not distances.shape == azimuth.shape == inclination.shape: | ||
| raise ShapeMismatchError | ||
|
|
||
| x = distances * np.sin(inclination) * np.cos(azimuth) | ||
| y = distances * np.sin(inclination) * np.sin(azimuth) | ||
| z = distances * np.cos(inclination) | ||
| return x, y, z | ||
|
|
||
|
|
||
|
|
||
| def convert_to_sphere( | ||
| abscissa: np.ndarray, | ||
| ordinates: np.ndarray, | ||
| applicates: np.ndarray, | ||
| ) -> tuple[np.ndarray, np.ndarray, np.ndarray]: ... | ||
| ) -> tuple[np.ndarray, np.ndarray, np.ndarray]: | ||
|
|
||
| if not abscissa.shape == ordinates.shape == applicates.shape: | ||
| raise ShapeMismatchError | ||
|
|
||
| distances = np.sqrt(abscissa**2 + ordinates**2 + applicates**2) | ||
| azimuth = np.arctan2(ordinates, abscissa) | ||
| inclinatian = np.zeros(distances.shape) | ||
|
|
||
| not_zero = distances > 0 | ||
| devide = applicates[not_zero] / distances[not_zero] | ||
| inclinatian[not_zero] = np.arccos(devide) | ||
|
|
||
| return distances, azimuth, inclinatian | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,4 +3,18 @@ | |
|
|
||
| def get_extremum_indices( | ||
| ordinates: np.ndarray, | ||
| ) -> tuple[np.ndarray, np.ndarray]: ... | ||
| ) -> tuple[np.ndarray, np.ndarray]: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не проходит ruff format |
||
| n = len(ordinates) | ||
| if n < 3: | ||
| raise ValueError | ||
|
|
||
| ordinates_safe = ordinates[1:-1] | ||
|
|
||
| max_mask = (ordinates_safe > ordinates[:-2]) & (ordinates_safe > ordinates[2:]) | ||
| min_mask = (ordinates_safe < ordinates[:-2]) & (ordinates_safe < ordinates[2:]) | ||
| indexes = np.arange(1,n-1) | ||
|
|
||
| ans = (indexes[min_mask], indexes[max_mask]) | ||
|
|
||
| return ans | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| matplotlib==3.8.0 | ||
| numpy==1.26.1 | ||
| numpy | ||
| opencv-python-headless==4.9.0.80 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,16 +2,53 @@ | |
|
|
||
|
|
||
| def pad_image(image: np.ndarray, pad_size: int) -> np.ndarray: | ||
| # ваш код | ||
| return image | ||
| if pad_size < 1: | ||
| raise ValueError | ||
| if len(image.shape) != 2: | ||
| padded = np.zeros( | ||
| (image.shape[0] + pad_size * 2, image.shape[1] + pad_size * 2, image.shape[2]), | ||
| dtype=image.dtype, | ||
| ) | ||
| padded[pad_size:-pad_size, pad_size:-pad_size, :] = image | ||
| else: | ||
| padded = np.zeros( | ||
| (image.shape[0] + pad_size * 2, image.shape[1] + pad_size * 2), dtype=image.dtype | ||
| ) | ||
| padded[pad_size:-pad_size, pad_size:-pad_size] = image | ||
|
|
||
| return padded | ||
|
|
||
|
|
||
| def blur_image( | ||
| image: np.ndarray, | ||
| kernel_size: int, | ||
| ) -> np.ndarray: | ||
| # ваш код | ||
| return image | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Есть решение быстрее |
||
| N = image.shape[0] | ||
| M = image.shape[1] | ||
|
|
||
| if kernel_size % 2 == 0: | ||
| raise ValueError | ||
| if kernel_size < 1: | ||
| raise ValueError | ||
| if kernel_size == 1: | ||
| return image | ||
|
|
||
| blurred_image = np.zeros_like(image) | ||
| image = pad_image(image, kernel_size // 2) | ||
|
|
||
| if len(image.shape) == 3: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно было использовать .ndim |
||
| for i in range(N): | ||
| for j in range(M): | ||
| blurred_image[i, j] = np.mean( | ||
| image[i : i + kernel_size, j : j + kernel_size], axis=(0, 1) | ||
| ) | ||
| else: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно было без if, так код был бы проще |
||
| for i in range(N): | ||
| for j in range(M): | ||
| blurred_image[i, j] = np.mean(image[i : i + kernel_size, j : j + kernel_size]) | ||
|
|
||
| return blurred_image | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,25 @@ def get_dominant_color_info( | |
| image: np.ndarray[np.uint8], | ||
| threshold: int = 5, | ||
| ) -> tuple[np.uint8, float]: | ||
| # ваш код | ||
|
|
||
| return 0, 0 | ||
| if threshold < 1: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Есть решение быстрее |
||
| raise ValueError("threshold must be positive") | ||
|
|
||
| colors = [0] * 256 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше было бы 256 вынести в константу, чтобы было сразу понятно, что вы имеете ввиду количество цветов, и еще вы несколько раз используетее это число |
||
|
|
||
| for i in range(image.shape[0]): | ||
| for j in range(image.shape[1]): | ||
| colors[image[i, j]] += 1 | ||
|
|
||
| max_sum = 0 | ||
| dominant_color = -1 | ||
| for i in range(256): | ||
| if colors[i] > 0: | ||
| start = max(0, i - threshold + 1) | ||
| end = min(255, i + threshold - 1) | ||
| now_sum = sum(colors[start : end + 1]) | ||
| if now_sum > max_sum: | ||
| max_sum = now_sum | ||
| dominant_color = i | ||
|
|
||
| return np.uint8(dominant_color), max_sum / (image.shape[0] * image.shape[1]) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| from typing import Any | ||
|
|
||
| import matplotlib.gridspec as gridspec | ||
| import matplotlib.pyplot as plt | ||
| import numpy as np | ||
|
|
||
|
|
@@ -13,7 +14,66 @@ def visualize_diagrams( | |
| ordinates: np.ndarray, | ||
| diagram_type: Any, | ||
| ) -> None: | ||
| # ваш код | ||
|
|
||
| if abscissa.shape != ordinates.shape: | ||
| raise ShapeMismatchError | ||
| if diagram_type not in ("hist", "violin", "box"): | ||
| raise ValueError | ||
|
|
||
| fig = plt.figure(figsize=(10, 10), facecolor="#cfd3cd") | ||
|
|
||
| gs = gridspec.GridSpec( | ||
| 2, | ||
| 2, | ||
| width_ratios=[1, 4], | ||
| height_ratios=[4, 1], | ||
| hspace=0.1, | ||
| wspace=0.1, | ||
| left=0.08, | ||
| right=0.98, | ||
| top=0.98, | ||
| bottom=0.08, | ||
| ) | ||
|
|
||
| ax_scatter = fig.add_subplot(gs[0, 1]) | ||
| ax_diag_x = fig.add_subplot(gs[1, 1]) | ||
| ax_diag_y = fig.add_subplot(gs[0, 0]) | ||
| ax_diag_x.grid(True) | ||
| ax_diag_y.grid(True) | ||
| ax_scatter.grid(True) | ||
| ax_scatter.tick_params(labelsize=16) | ||
| ax_diag_x.tick_params(labelsize=16) | ||
| ax_diag_y.tick_params(labelsize=16) | ||
|
|
||
| ax_scatter.scatter(abscissa, ordinates, alpha=0.5, marker="o", s=100, color="sandybrown") | ||
|
|
||
| if diagram_type == "hist": | ||
| ax_diag_x.hist( | ||
| abscissa, bins=30, color="cornflowerblue", edgecolor="dimgray", alpha=0.7, density=True | ||
| ) | ||
| ax_diag_y.hist( | ||
| ordinates, | ||
| bins=30, | ||
| orientation="horizontal", | ||
| color="cornflowerblue", | ||
| edgecolor="dimgray", | ||
| alpha=0.7, | ||
| density=True, | ||
| ) | ||
|
|
||
| elif diagram_type == "violin": | ||
| ax_diag_x.violinplot(abscissa, vert=False) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А почему не настроили? |
||
| ax_diag_y.violinplot(ordinates, vert=True) | ||
|
|
||
| elif diagram_type == "box": | ||
| ax_diag_x.boxplot(abscissa, vert=False) | ||
| ax_diag_y.boxplot(ordinates, vert=True) | ||
|
|
||
| ax_diag_x.invert_yaxis() | ||
| ax_diag_y.invert_xaxis() | ||
|
|
||
| plt.savefig("scatter_with_diagrams.png") | ||
|
|
||
| pass | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,48 @@ | ||
| # ваш код (используйте функции или классы для решения данной задачи) | ||
| import json | ||
|
|
||
| import matplotlib.pyplot as plt | ||
| import numpy as np | ||
|
|
||
|
|
||
| def get_data_for_plot(file_path: str) -> tuple[np.ndarray, np.ndarray]: | ||
| with open(file_path, "r") as f: | ||
| data = json.load(f) | ||
| before = np.array(data["before"]) | ||
| after = np.array(data["after"]) | ||
| return before, after | ||
|
|
||
|
|
||
| def sum_classes(before: np.ndarray, after: np.ndarray, classes=["I", "II", "III", "IV"]): | ||
| before_sum = {} | ||
| after_sum = {} | ||
| for cl in classes: | ||
| mask = before == cl | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему бы не использовать classes.count? |
||
| before_sum[cl] = np.sum(mask) | ||
| mask = after == cl | ||
| after_sum[cl] = np.sum(mask) | ||
| return before_sum, after_sum | ||
|
|
||
|
|
||
| classes = ["I", "II", "III", "IV"] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше было бы писать код в фукнции |
||
| before, after = get_data_for_plot("data/medic_data.json") | ||
| before_sum, after_sum = sum_classes(before, after) | ||
|
|
||
| fig, ax = plt.subplots(figsize=(9, 6)) | ||
| x = np.arange(len(classes)) | ||
| ax.tick_params(labelsize=16) | ||
|
|
||
| width = 0.35 | ||
| bars1 = ax.bar(x - width / 2, before_sum.values(), width, label="before", color="cornflowerblue") | ||
| bars2 = ax.bar(x + width / 2, after_sum.values(), width, label="after", color="sandybrown") | ||
|
|
||
| ax.set_xlabel("Mitral disease stage", fontsize=16) | ||
| ax.set_ylabel("Amount of people", fontsize=16) | ||
| ax.set_title("Mitral disease stages", fontsize=16) | ||
| ax.grid(axis="y", color="#cacaca", linewidth=0.8) | ||
| ax.set_axisbelow(True) | ||
| ax.set_xticks(x) | ||
| ax.set_xticklabels(classes) | ||
| ax.legend(fontsize=16) | ||
|
|
||
| plt.savefig("class_distribution.png") | ||
| plt.show() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не проходит ruff format