Skip to content

Commit c57066c

Browse files
Trim redundant tests from the non-finite dissimilarity fix
Drop the Task timeout helper, since ExUnit already times out a hanging test. Drop the all-NaN case, which exercises the same path as the NaN coordinate one, and the jit_apply case, since jit is already covered elsewhere in the file. Drop the finite data case, which compared fit/2 against itself once the fallback path was removed.
1 parent 6bf7322 commit c57066c

1 file changed

Lines changed: 2 additions & 40 deletions

File tree

test/scholar/cluster/hierarchical_test.exs

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -288,18 +288,6 @@ defmodule Scholar.Cluster.HierarchicalTest do
288288
# always mutually nearest to each other, guaranteeing at least one merge every round.
289289
# When it happens the loop now stops instead of running forever, and the merges it could
290290
# not make are reported as NaN dissimilarities with clade -1 and size 0.
291-
#
292-
# Every case is wrapped in a Task with an explicit timeout so a regression fails fast
293-
# with a clear message instead of hanging the whole test run.
294-
defp fit_within(data, opts, ms \\ 5_000) do
295-
task = Task.async(fn -> Hierarchical.fit(data, opts) end)
296-
297-
case Task.yield(task, ms) || Task.shutdown(task, :brutal_kill) do
298-
{:ok, result} -> result
299-
nil -> flunk("Hierarchical.fit did not terminate within #{ms}ms")
300-
end
301-
end
302-
303291
test "an infinite dissimilarity between the last two clades reports an incomplete merge" do
304292
# Point 3 is infinitely far from everyone. Once points 0, 1 and 2 (mutually close)
305293
# have merged, only two clades remain: {0, 1, 2} and {3}, at distance infinity, and
@@ -312,7 +300,7 @@ defmodule Scholar.Cluster.HierarchicalTest do
312300
[:infinity, :infinity, :infinity, 0.0]
313301
])
314302

315-
model = fit_within(d, dissimilarity: :precomputed, linkage: :single)
303+
model = Hierarchical.fit(d, dissimilarity: :precomputed, linkage: :single)
316304

317305
assert model.num_points == 4
318306
# The two finite merges are made and kept, in ascending order.
@@ -329,38 +317,12 @@ defmodule Scholar.Cluster.HierarchicalTest do
329317
# which is why an incomplete merge is identified by its clade and size, not by NaN.
330318
x = Nx.tensor([[0.0, 0.0], [0.1, 0.0], [0.2, 0.0], [5.0, 5.0], [5.1, 5.0], [:nan, 0.0]])
331319

332-
model = fit_within(x, [])
320+
model = Hierarchical.fit(x)
333321

334322
assert model.num_points == 6
335323
assert Nx.to_number(Nx.all(Nx.not_equal(model.clades, -1))) == 1
336324
assert Nx.to_number(Nx.all(Nx.greater(model.sizes, 0))) == 1
337325
end
338-
339-
test "an all-NaN input still makes every merge" do
340-
x = Nx.broadcast(Nx.tensor(:nan), {6, 2})
341-
342-
model = fit_within(x, [])
343-
344-
assert model.num_points == 6
345-
assert Nx.to_number(Nx.all(Nx.not_equal(model.clades, -1))) == 1
346-
assert Nx.to_number(Nx.all(Nx.greater(model.sizes, 0))) == 1
347-
end
348-
349-
test "does not change the result on finite data" do
350-
# Aborting only ever triggers when a round makes zero progress, which cannot happen
351-
# for finite dissimilarities, so this must match plain `fit/2` exactly.
352-
data = Nx.tensor([[1, 5], [2, 5], [1, 4], [4, 5], [5, 5], [5, 4], [1, 2], [1, 1], [2, 1]])
353-
354-
for linkage <- [:average, :complete, :single, :ward, :weighted] do
355-
assert fit_within(data, linkage: linkage) == Hierarchical.fit(data, linkage: linkage)
356-
end
357-
end
358-
359-
test "works with jit_apply" do
360-
x = Nx.tensor([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [:nan, :nan], [5.0, 5.0]])
361-
362-
assert Nx.Defn.jit_apply(&Hierarchical.fit/1, [x]) == Hierarchical.fit(x)
363-
end
364326
end
365327

366328
describe "errors" do

0 commit comments

Comments
 (0)