Improve readability of duplicate report frontend parsing / generation

This commit is contained in:
Liam 2024-07-19 08:50:16 -04:00
parent 11b414bf7b
commit 79f508f603
3 changed files with 26 additions and 22 deletions

View file

@ -15,7 +15,8 @@ defmodule Philomena.DuplicateReports do
def generate_reports(source) do def generate_reports(source) do
source = Repo.preload(source, :intensity) source = Repo.preload(source, :intensity)
duplicates_of(source.intensity, source.image_aspect_ratio, 0.2, 0.05) {source.intensity, source.image_aspect_ratio}
|> find_duplicates(dist: 0.2)
|> where([i, _it], i.id != ^source.id) |> where([i, _it], i.id != ^source.id)
|> Repo.all() |> Repo.all()
|> Enum.map(fn target -> |> Enum.map(fn target ->
@ -25,7 +26,11 @@ defmodule Philomena.DuplicateReports do
end) end)
end end
def duplicates_of(intensities, aspect_ratio, dist \\ 0.25, aspect_dist \\ 0.05, limit \\ 10) do def find_duplicates({intensities, aspect_ratio}, opts \\ []) do
aspect_dist = Keyword.get(opts, :aspect_dist, 0.05)
limit = Keyword.get(opts, :limit, 10)
dist = Keyword.get(opts, :dist, 0.25)
# for each color channel # for each color channel
dist = dist * 3 dist = dist * 3

View file

@ -17,10 +17,11 @@ defmodule PhilomenaWeb.ImageReverse do
{analysis, intensities} -> {analysis, intensities} ->
{width, height} = analysis.dimensions {width, height} = analysis.dimensions
aspect = width / height aspect = width / height
dist = normalize_dist(image_params) dist = parse_dist(image_params)
limit = parse_limit(image_params) limit = parse_limit(image_params)
DuplicateReports.duplicates_of(intensities, aspect, dist, dist, limit) {intensities, aspect}
|> DuplicateReports.find_duplicates(dist: dist, aspect_dist: dist, limit: limit)
|> preload([:user, :intensity, [:sources, tags: :aliases]]) |> preload([:user, :intensity, [:sources, tags: :aliases]])
|> Repo.all() |> Repo.all()
end end
@ -43,24 +44,17 @@ defmodule PhilomenaWeb.ImageReverse do
# The distance metric is taxicab distance, not Euclidean, # The distance metric is taxicab distance, not Euclidean,
# because this is more efficient to index. # because this is more efficient to index.
defp normalize_dist(%{"distance" => distance}) do defp parse_dist(%{"distance" => distance}) do
distance distance
|> parse_dist() |> Decimal.parse()
|> max(0.01) |> case do
|> min(1.0) {value, _rest} -> Decimal.to_float(value)
_ -> 0.25
end
|> clamp(0.01, 1.0)
end end
defp normalize_dist(_dist), do: 0.25 defp parse_dist(_params), do: 0.25
defp parse_dist(dist) do
case Decimal.parse(dist) do
{value, _rest} ->
Decimal.to_float(value)
_ ->
0.0
end
end
defp parse_limit(%{"limit" => limit}) do defp parse_limit(%{"limit" => limit}) do
limit limit
@ -69,9 +63,12 @@ defmodule PhilomenaWeb.ImageReverse do
{limit, _rest} -> limit {limit, _rest} -> limit
_ -> 10 _ -> 10
end end
|> max(1) |> clamp(1, 50)
|> min(50)
end end
defp parse_limit(_), do: 10 defp parse_limit(_params), do: 10
defp clamp(n, min, _max) when n < min, do: min
defp clamp(n, _min, max) when n > max, do: max
defp clamp(n, _min, _max), do: n
end end

View file

@ -28,6 +28,8 @@ h1 Reverse Search
br br
= number_input f, :distance, value: 0.25, min: 0, max: 1, step: 0.01, class: "input" = number_input f, :distance, value: 0.25, min: 0, max: 1, step: 0.01, class: "input"
= hidden_input f, :limit, value: 10
.field .field
= submit "Reverse Search", class: "button" = submit "Reverse Search", class: "button"