Skip to content

Make invalid k-means options an %Image.Error{} + return {:ok, model} from Image.Scholar.k_means/2 - #227

Open
hlindset wants to merge 1 commit into
elixir-image:mainfrom
hlindset:fix/k-means-error-contract
Open

Make invalid k-means options an %Image.Error{} + return {:ok, model} from Image.Scholar.k_means/2#227
hlindset wants to merge 1 commit into
elixir-image:mainfrom
hlindset:fix/k-means-error-contract

Conversation

@hlindset

@hlindset hlindset commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Image.k_means/2 and Image.reduce_colors/2 both pass options straight to Scholar.Cluster.KMeans.fit/2, which validates with NimbleOptions.validate!/2 and raises.

Image.k_means(img, num_clusters: 0)
# ** (NimbleOptions.ValidationError) invalid value for :num_clusters option: expected positive integer, got: 0

Changes

  • Added Image.Scholar.fit/2 that rescues and translates NimbleOptions.ValidationError into %Image.Error{reason: :invalid_option}
  • Made Image.reduce_colors/2 and Image.Scholar.k_means/2 both go through the new Image.Scholar.fit/2
  • Moved the scholar call in Image.k_means/2 into the with to properly propagate errors

Breaking changes

Affects Image.k_means/2, Image.reduce_colors/2, Image.Scholar.k_means/2 and the bang variants.

  • Invalid or unknown options return {:error, %Image.Error{reason: :invalid_option}}. Previously raised NimbleOptions.ValidationError
  • The bang variants raise Image.Error. Previously raised NimbleOptions.ValidationError
  • Image.Scholar.k_means/2 now returns {:ok, %Scholar.Cluster.KMeans{}}. Previously returned a bare %Scholar.Cluster.KMeans{} on success

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant