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
22 changes: 18 additions & 4 deletions solutions/sem02/lesson03/task1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,27 @@ class ShapeMismatchError(Exception):
def sum_arrays_vectorized(
lhs: np.ndarray,
rhs: np.ndarray,
) -> np.ndarray: ...
) -> np.ndarray:
if lhs.shape != rhs.shape:
raise ShapeMismatchError
return lhs + rhs


def compute_poly_vectorized(abscissa: np.ndarray) -> np.ndarray: ...
def compute_poly_vectorized(abscissa: np.ndarray) -> np.ndarray:
return 3 * (abscissa ** 2) + 2 * abscissa + 1


def get_mutual_l2_distances_vectorized(
lhs: np.ndarray,
rhs: np.ndarray,
) -> np.ndarray: ...
) -> np.ndarray:
if lhs.shape[1] != rhs.shape[1]:
raise ShapeMismatchError

lhsn = np.sum(lhs ** 2, axis = 1, keepdims=True)
rhsn = np.sum(rhs ** 2, axis = 1, keepdims=True)

inter_of_matrices = lhs @ rhs.T

distances = np.sqrt(lhsn + rhsn.T - 2 * inter_of_matrices)

return distances
22 changes: 20 additions & 2 deletions solutions/sem02/lesson03/task2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,29 @@ 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]:

if not (distances.shape == azimuth.shape == inclination.shape):
raise ShapeMismatchError

abcissa = distances * np.sin(inclination) * np.cos(azimuth)
ordinates = distances * np.sin(inclination) * np.sin(azimuth)
applicates = distances * np.cos(inclination)

return (abcissa, ordinates, applicates)


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)
inclination = np.arctan2(applicates, np.sqrt(abscissa ** 2 + ordinates ** 2))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Надо было (x2 + y2) ** 0.5 / z. tg = sin / cos. А вы пытаетесь ctg посчитать.


return (distances, azimuth, inclination)
16 changes: 15 additions & 1 deletion solutions/sem02/lesson03/task3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,18 @@

def get_extremum_indices(
ordinates: np.ndarray,
) -> tuple[np.ndarray, np.ndarray]: ...
) -> tuple[np.ndarray, np.ndarray]:
if len(ordinates) < 3:
raise ValueError

left = ordinates[:-2]
mid = ordinates[1:-1]
right = ordinates[2:]

mins = (left > mid) & (mid < right)
maxes = (left < mid) & (mid > right)

min_indices = np.where(mins)[0] + 1
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np.where нельзя было использовать

max_indices = np.where(maxes)[0] + 1

return (min_indices, max_indices)
52 changes: 48 additions & 4 deletions solutions/sem02/lesson04/task1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,60 @@


def pad_image(image: np.ndarray, pad_size: int) -> np.ndarray:
# ваш код
return image

if pad_size < 1:
raise ValueError

if image.ndim == 2:
height, width = image.shape
padded_image = np.zeros((height + 2 * pad_size, width + 2 * pad_size), dtype=image.dtype)
padded_image[pad_size:pad_size + height, pad_size:pad_size + width] = image

elif image.ndim == 3:
height, width, channels = image.shape
padded_image = np.zeros((height + 2 * pad_size, width + 2 * pad_size, channels), dtype=image.dtype)
padded_image[pad_size:pad_size + height, pad_size:pad_size + width, :] = image

return padded_image


def blur_image(
image: np.ndarray,
kernel_size: int,
) -> np.ndarray:
# ваш код
return image

if kernel_size < 1 or kernel_size % 2 == 0:
raise ValueError

pad_size = kernel_size // 2

if image.ndim == 2:
height, width = image.shape
padded_image = np.zeros((height + 2 * pad_size, width + 2 * pad_size), dtype=image.dtype)
padded_image[pad_size:pad_size + height, pad_size:pad_size + width] = image

result = np.zeros((height, width), dtype=image.dtype)

for i in range(height):
for j in range(width):
window = padded_image[i:i+kernel_size, j:j+kernel_size]
result[i, j] = np.mean(window)


elif image.ndim == 3:
height, width, channels = image.shape
padded_image = np.zeros((height + 2 * pad_size, width + 2 * pad_size, channels), dtype=image.dtype)
padded_image[pad_size:pad_size + height, pad_size:pad_size + width, :] = image

result = np.zeros((height, width, channels), dtype=image.dtype)

for i in range(height):
for j in range(width):
for k in range(channels):
window = padded_image[i:i+kernel_size, j:j+kernel_size, k]
result[i, j, k] = np.mean(window)

return result.astype(image.dtype)


if __name__ == "__main__":
Expand Down
24 changes: 22 additions & 2 deletions solutions/sem02/lesson04/task2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@ def get_dominant_color_info(
image: np.ndarray[np.uint8],
threshold: int = 5,
) -> tuple[np.uint8, float]:
# ваш код
if threshold < 1:
raise ValueError

return 0, 0
pixels = image.flatten()
zerose = np.zeros(256, dtype=np.int64)
for color in range(256):
mask = pixels == color
zerose[color] = np.sum(mask)

max_pixels = 0
best_color = 0
for current_color in range(256):
if zerose[current_color] != 0:
left = max(0, current_color - (threshold - 1))
right = min(255, current_color + (threshold - 1))
current_sum = np.sum(zerose[left : right + 1])

if current_sum > max_pixels:
max_pixels = current_sum
best_color = current_color

percent = (max_pixels / pixels.size) * 100
return np.uint8(best_color), float(percent)
Loading