From ba29b69508375f9556513caed1271d43138ef9c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hern=C3=A1n=20Morales=20Durand?= Date: Tue, 19 Dec 2023 15:46:27 +0100 Subject: [PATCH] Update CI to Pharo12. Update README --- .github/workflows/test.yml | 2 +- README.md | 10 +- .../AIClusterEngineTest.class.st | 410 +++++++++--------- .../AIDendrogramTest.class.st | 102 ++--- .../AIDistanceSquareTest.class.st | 280 ++++++------ .../AISimilarityItemTest.class.st | 88 ++-- .../IntegerTest.extension.st | 24 +- .../package.st | 2 +- 8 files changed, 462 insertions(+), 456 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b245901..a348586 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,7 +15,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - smalltalk: [ Pharo64-9.0, Pharo64-10, Pharo64-11 ] + smalltalk: [ Pharo64-9.0, Pharo64-10, Pharo64-11, Pharo64-12 ] name: ${{ matrix.smalltalk }} steps: - uses: actions/checkout@v2 diff --git a/README.md b/README.md index 7a3ce69..7c8393d 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,17 @@ [![Build status](https://github.com/pharo-ai/hierarchical-clustering/workflows/CI/badge.svg)](https://github.com/pharo-ai/hierarchical-clustering/actions/workflows/test.yml) [![Coverage Status](https://coveralls.io/repos/github/pharo-ai/hierarchical-clustering/badge.svg?branch=master)](https://coveralls.io/github/pharo-ai/hierarchical-clustering?branch=master) +[![Pharo version](https://img.shields.io/badge/Pharo-9-%23aac9ff.svg)](https://pharo.org/download) +[![Pharo version](https://img.shields.io/badge/Pharo-10-%23aac9ff.svg)](https://pharo.org/download) +[![Pharo version](https://img.shields.io/badge/Pharo-11-%23aac9ff.svg)](https://pharo.org/download) +[![Pharo version](https://img.shields.io/badge/Pharo-12-%23aac9ff.svg)](https://pharo.org/download) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/pharo-ai/hierarchical-clustering/master/LICENSE) +# Description + Hierarchical Clustering algorithms for Pharo -## Installation +# Installation ```smalltalk Metacello new @@ -24,7 +30,7 @@ Metacello new load: #('AIVizRoassalHC') ``` -## How to depend on it? +# How to depend on it? If you want to add a dependency on hierarchical-clustering to your project, include the following lines into your baseline method: diff --git a/src/AI-HierarchicalClustering-Tests/AIClusterEngineTest.class.st b/src/AI-HierarchicalClustering-Tests/AIClusterEngineTest.class.st index a9eeda0..59843f2 100644 --- a/src/AI-HierarchicalClustering-Tests/AIClusterEngineTest.class.st +++ b/src/AI-HierarchicalClustering-Tests/AIClusterEngineTest.class.st @@ -1,205 +1,205 @@ -" -Copyright (c), 2004-2007 Adrian Kuhn. This class is part of Hapax. Hapax is distributed under BSD License, see package comment. -" -Class { - #name : #AIClusterEngineTest, - #superclass : #TestCase, - #category : #'AI-HierarchicalClustering-Tests-Core' -} - -{ #category : #'testing - clustering' } -AIClusterEngineTest >> testClustering300Elements [ - - | input clusty clusters | - input := Random new next: 300. - clusty := AIClusterEngine with: input. - 1 to: 1 do: [ :n | clusty averageLinkage ]. - clusters := clusty dendrogram breakInto: 10. - ^ clusters -] - -{ #category : #'testing - clustering' } -AIClusterEngineTest >> testClusteringWithAverageLinkage [ - - | input clusty clusters | - input := #( #( 1 2 3 5 ) #( 11 12 15 ) #( 21 22 23 25 ) - #( 31 32 35 ) #( 41 42 43 45 47 ) ). - clusty := AIClusterEngine with: input flatten shuffle. - clusty hierarchicalClusteringUsing: #averageLinkage. - clusters := clusty dendrogram breakInto: 5. - self assert: clusters size equals: 5. - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 1) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 2) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 3) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 4) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 5) ]). - ^ clusters -] - -{ #category : #'testing - clustering' } -AIClusterEngineTest >> testClusteringWithCentroid [ - - | input clusty clusters | - input := #( #( 1 2 3 5 ) #( 11 12 15 ) #( 21 22 23 25 ) - #( 31 32 35 ) #( 41 42 43 45 47 ) ). - clusty := AIClusterEngine with: input flatten shuffle. - clusty hierarchicalClusteringUsing: #centroid. - clusters := clusty dendrogram breakInto: 5. - self assert: clusters size equals: 5. - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 1) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 2) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 3) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 4) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 5) ]). - ^ clusters -] - -{ #category : #'testing - clustering' } -AIClusterEngineTest >> testClusteringWithCompleteLinkage [ - - | input clusty clusters | - input := #( #( 1 2 3 5 ) #( 11 12 15 ) #( 21 22 23 25 ) - #( 31 32 35 ) #( 41 42 43 45 47 ) ). - clusty := AIClusterEngine with: input flatten shuffle. - clusty hierarchicalClusteringUsing: #completeLinkage. - clusters := clusty dendrogram breakInto: 5. - self assert: clusters size equals: 5. - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 1) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 2) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 3) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 4) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 5) ]). - ^ clusters -] - -{ #category : #'testing - clustering' } -AIClusterEngineTest >> testClusteringWithMeanLinkage [ - - | input clusty clusters | - input := #( #( 1 2 3 5 ) #( 11 12 15 ) #( 21 22 23 25 ) - #( 31 32 35 ) #( 41 42 43 45 47 ) ). - clusty := AIClusterEngine with: input flatten shuffle. - clusty hierarchicalClusteringUsing: #meanLinkage. - clusters := clusty dendrogram breakInto: 5. - self assert: clusters size equals: 5. - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 1) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 2) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 3) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 4) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 5) ]). - ^ clusters -] - -{ #category : #'testing - clustering' } -AIClusterEngineTest >> testClusteringWithSingleLinkage [ - - | input clusty clusters | - input := #( #( 1 2 3 5 ) #( 11 12 15 ) #( 21 22 23 25 ) - #( 31 32 35 ) #( 41 42 43 45 47 ) ). - clusty := AIClusterEngine with: input flatten shuffle. - clusty hierarchicalClusteringUsing: #singleLinkage. - clusters := clusty dendrogram breakInto: 5. - self assert: clusters size equals: 5. - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 1) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 2) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 3) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 4) ]). - self assert: - (clusters anySatisfy: [ :each | each equalsTo: (input at: 5) ]). - ^ clusters -] - -{ #category : #'testing - clustering' } -AIClusterEngineTest >> testClusteringWithWardsMethod [ - | input clusty clusters | - input := #(#(1 2 3 5) #(11 12 15) #(21 22 23 25) #(31 32 35) #(41 42 43 45 47)). - clusty := AIClusterEngine with: input flatten shuffle. - clusty hierarchicalClusteringUsing: #wardsMethod. - clusters := clusty dendrogram breakInto: 5. - self assert: clusters size equals: 5. - self assert: (clusters anySatisfy: [ :each | each equalsTo: (input at: 1) ]). - self assert: (clusters anySatisfy: [ :each | each equalsTo: (input at: 2) ]). - self assert: (clusters anySatisfy: [ :each | each equalsTo: (input at: 3) ]). - self assert: (clusters anySatisfy: [ :each | each equalsTo: (input at: 4) ]). - self assert: (clusters anySatisfy: [ :each | each equalsTo: (input at: 5) ]). - ^ clusters -] - -{ #category : #test } -AIClusterEngineTest >> testDendrogram [ - - | input clusty expected | - - input := #(3 11 2 15 1 12). - expected := AIDendrogramNode - with: (AIDendrogramNode - with: (AIDendrogramNode - with: (AIDendrogramLeaf with: 3) - with: (AIDendrogramLeaf with: 2) - at: 1) - with: (AIDendrogramLeaf with: 1) - at: 1.5) - with: (AIDendrogramNode - with: (AIDendrogramNode - with: (AIDendrogramLeaf with: 11) - with: (AIDendrogramLeaf with: 12) - at: 1) - with: (AIDendrogramLeaf with: 15) - at: 3.5) - at: 10.666666666666664. - - clusty := AIClusterEngine with: input. - - self assert: clusty dendrogram equals: expected -] - -{ #category : #test } -AIClusterEngineTest >> testDistanceMatrix [ - - | input clusty | - input := #( 3 11 2 15 1 12 ). - clusty := AIClusterEngine new distanceMatrix: (AIDistanceSquare on: input). - self assert: clusty distanceMatrix equals: (AIDistanceSquare on: input) -] - -{ #category : #test } -AIClusterEngineTest >> testItems [ - - | input clusty | - input := #(3 11 2 15 1 12). - clusty := AIClusterEngine new distanceMatrix: (AIDistanceSquare on: input). - self assert: clusty items equals: input. -] - -{ #category : #test } -AIClusterEngineTest >> testSeriationEngine [ - - | input output | - input := #(1 2 3 5 11 12 15 21 22 23 25 31 32 35 41 42 43 45 47). - output := AISeriationEngine applyOn: input copy asArray shuffle. - self assert: output = input | (output reverse = input) -] +" +Copyright (c), 2004-2007 Adrian Kuhn. This class is part of Hapax. Hapax is distributed under BSD License, see package comment. +" +Class { + #name : #AIClusterEngineTest, + #superclass : #TestCase, + #category : #'AI-HierarchicalClustering-Tests-Core' +} + +{ #category : #'testing - clustering' } +AIClusterEngineTest >> testClustering300Elements [ + + | input clusty clusters | + input := Random new next: 300. + clusty := AIClusterEngine with: input. + 1 to: 1 do: [ :n | clusty averageLinkage ]. + clusters := clusty dendrogram breakInto: 10. + ^ clusters +] + +{ #category : #'testing - clustering' } +AIClusterEngineTest >> testClusteringWithAverageLinkage [ + + | input clusty clusters | + input := #( #( 1 2 3 5 ) #( 11 12 15 ) #( 21 22 23 25 ) + #( 31 32 35 ) #( 41 42 43 45 47 ) ). + clusty := AIClusterEngine with: input flatten shuffle. + clusty hierarchicalClusteringUsing: #averageLinkage. + clusters := clusty dendrogram breakInto: 5. + self assert: clusters size equals: 5. + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 1) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 2) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 3) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 4) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 5) ]). + ^ clusters +] + +{ #category : #'testing - clustering' } +AIClusterEngineTest >> testClusteringWithCentroid [ + + | input clusty clusters | + input := #( #( 1 2 3 5 ) #( 11 12 15 ) #( 21 22 23 25 ) + #( 31 32 35 ) #( 41 42 43 45 47 ) ). + clusty := AIClusterEngine with: input flatten shuffle. + clusty hierarchicalClusteringUsing: #centroid. + clusters := clusty dendrogram breakInto: 5. + self assert: clusters size equals: 5. + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 1) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 2) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 3) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 4) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 5) ]). + ^ clusters +] + +{ #category : #'testing - clustering' } +AIClusterEngineTest >> testClusteringWithCompleteLinkage [ + + | input clusty clusters | + input := #( #( 1 2 3 5 ) #( 11 12 15 ) #( 21 22 23 25 ) + #( 31 32 35 ) #( 41 42 43 45 47 ) ). + clusty := AIClusterEngine with: input flatten shuffle. + clusty hierarchicalClusteringUsing: #completeLinkage. + clusters := clusty dendrogram breakInto: 5. + self assert: clusters size equals: 5. + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 1) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 2) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 3) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 4) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 5) ]). + ^ clusters +] + +{ #category : #'testing - clustering' } +AIClusterEngineTest >> testClusteringWithMeanLinkage [ + + | input clusty clusters | + input := #( #( 1 2 3 5 ) #( 11 12 15 ) #( 21 22 23 25 ) + #( 31 32 35 ) #( 41 42 43 45 47 ) ). + clusty := AIClusterEngine with: input flatten shuffle. + clusty hierarchicalClusteringUsing: #meanLinkage. + clusters := clusty dendrogram breakInto: 5. + self assert: clusters size equals: 5. + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 1) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 2) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 3) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 4) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 5) ]). + ^ clusters +] + +{ #category : #'testing - clustering' } +AIClusterEngineTest >> testClusteringWithSingleLinkage [ + + | input clusty clusters | + input := #( #( 1 2 3 5 ) #( 11 12 15 ) #( 21 22 23 25 ) + #( 31 32 35 ) #( 41 42 43 45 47 ) ). + clusty := AIClusterEngine with: input flatten shuffle. + clusty hierarchicalClusteringUsing: #singleLinkage. + clusters := clusty dendrogram breakInto: 5. + self assert: clusters size equals: 5. + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 1) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 2) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 3) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 4) ]). + self assert: + (clusters anySatisfy: [ :each | each equalsTo: (input at: 5) ]). + ^ clusters +] + +{ #category : #'testing - clustering' } +AIClusterEngineTest >> testClusteringWithWardsMethod [ + | input clusty clusters | + input := #(#(1 2 3 5) #(11 12 15) #(21 22 23 25) #(31 32 35) #(41 42 43 45 47)). + clusty := AIClusterEngine with: input flatten shuffle. + clusty hierarchicalClusteringUsing: #wardsMethod. + clusters := clusty dendrogram breakInto: 5. + self assert: clusters size equals: 5. + self assert: (clusters anySatisfy: [ :each | each equalsTo: (input at: 1) ]). + self assert: (clusters anySatisfy: [ :each | each equalsTo: (input at: 2) ]). + self assert: (clusters anySatisfy: [ :each | each equalsTo: (input at: 3) ]). + self assert: (clusters anySatisfy: [ :each | each equalsTo: (input at: 4) ]). + self assert: (clusters anySatisfy: [ :each | each equalsTo: (input at: 5) ]). + ^ clusters +] + +{ #category : #test } +AIClusterEngineTest >> testDendrogram [ + + | input clusty expected | + + input := #(3 11 2 15 1 12). + expected := AIDendrogramNode + with: (AIDendrogramNode + with: (AIDendrogramNode + with: (AIDendrogramLeaf with: 3) + with: (AIDendrogramLeaf with: 2) + at: 1) + with: (AIDendrogramLeaf with: 1) + at: 1.5) + with: (AIDendrogramNode + with: (AIDendrogramNode + with: (AIDendrogramLeaf with: 11) + with: (AIDendrogramLeaf with: 12) + at: 1) + with: (AIDendrogramLeaf with: 15) + at: 3.5) + at: 10.666666666666664. + + clusty := AIClusterEngine with: input. + + self assert: clusty dendrogram equals: expected +] + +{ #category : #test } +AIClusterEngineTest >> testDistanceMatrix [ + + | input clusty | + input := #( 3 11 2 15 1 12 ). + clusty := AIClusterEngine new distanceMatrix: (AIDistanceSquare on: input). + self assert: clusty distanceMatrix equals: (AIDistanceSquare on: input) +] + +{ #category : #test } +AIClusterEngineTest >> testItems [ + + | input clusty | + input := #(3 11 2 15 1 12). + clusty := AIClusterEngine new distanceMatrix: (AIDistanceSquare on: input). + self assert: clusty items equals: input. +] + +{ #category : #test } +AIClusterEngineTest >> testSeriationEngine [ + + | input output | + input := #(1 2 3 5 11 12 15 21 22 23 25 31 32 35 41 42 43 45 47). + output := AISeriationEngine applyOn: input copy asArray shuffle. + self assert: output = input | (output reverse = input) +] diff --git a/src/AI-HierarchicalClustering-Tests/AIDendrogramTest.class.st b/src/AI-HierarchicalClustering-Tests/AIDendrogramTest.class.st index 2ba31d5..624f25c 100644 --- a/src/AI-HierarchicalClustering-Tests/AIDendrogramTest.class.st +++ b/src/AI-HierarchicalClustering-Tests/AIDendrogramTest.class.st @@ -1,51 +1,51 @@ -" -Copyright (c), 2004-2007 Adrian Kuhn. This class is part of Hapax. Hapax is distributed under BSD License, see package comment. -" -Class { - #name : #AIDendrogramTest, - #superclass : #TestCase, - #instVars : [ - 'dendrogram' - ], - #category : #'AI-HierarchicalClustering-Tests-Core' -} - -{ #category : #running } -AIDendrogramTest >> setUp [ - | ab cd ef abcd efg | - super setUp. - ab := (AIDendrogram with: #a) merge: (AIDendrogram with: #b) at: 1. - cd := (AIDendrogram with: #c) merge: (AIDendrogram with: #d) at: 2. - ef := (AIDendrogram with: #e) merge: (AIDendrogram with: #f) at: 3. - abcd := ab merge: cd at: 4. - efg := ef merge: (AIDendrogram with: #g) at: 5. - dendrogram := abcd merge: efg at: 7 "#(((a 1 b) 4 (c 2 d)) 7 ((e 3 f) 5 g))" -] - -{ #category : #test } -AIDendrogramTest >> testAllLeaves [ - self assert: dendrogram elements size equals: 7. - self assert: dendrogram elements equals: #(#a #b #c #d #e #f #g) -] - -{ #category : #test } -AIDendrogramTest >> testIndex [ - self assert: dendrogram index equals: 1. - self assert: dendrogram left index equals: 1. - self assert: dendrogram right index equals: 5. - self assert: dendrogram left left index equals: 1. - self assert: dendrogram left right index equals: 3. - self assert: dendrogram right left index equals: 5. - self assert: dendrogram right right index equals: 7 -] - -{ #category : #test } -AIDendrogramTest >> testSize [ - self assert: dendrogram size equals: 7. - self assert: dendrogram left size equals: 4. - self assert: dendrogram right size equals: 3. - self assert: dendrogram left left size equals: 2. - self assert: dendrogram left right size equals: 2. - self assert: dendrogram right left size equals: 2. - self assert: dendrogram right right size equals: 1 -] +" +Copyright (c), 2004-2007 Adrian Kuhn. This class is part of Hapax. Hapax is distributed under BSD License, see package comment. +" +Class { + #name : #AIDendrogramTest, + #superclass : #TestCase, + #instVars : [ + 'dendrogram' + ], + #category : #'AI-HierarchicalClustering-Tests-Core' +} + +{ #category : #running } +AIDendrogramTest >> setUp [ + | ab cd ef abcd efg | + super setUp. + ab := (AIDendrogram with: #a) merge: (AIDendrogram with: #b) at: 1. + cd := (AIDendrogram with: #c) merge: (AIDendrogram with: #d) at: 2. + ef := (AIDendrogram with: #e) merge: (AIDendrogram with: #f) at: 3. + abcd := ab merge: cd at: 4. + efg := ef merge: (AIDendrogram with: #g) at: 5. + dendrogram := abcd merge: efg at: 7 "#(((a 1 b) 4 (c 2 d)) 7 ((e 3 f) 5 g))" +] + +{ #category : #test } +AIDendrogramTest >> testAllLeaves [ + self assert: dendrogram elements size equals: 7. + self assert: dendrogram elements equals: #(#a #b #c #d #e #f #g) +] + +{ #category : #test } +AIDendrogramTest >> testIndex [ + self assert: dendrogram index equals: 1. + self assert: dendrogram left index equals: 1. + self assert: dendrogram right index equals: 5. + self assert: dendrogram left left index equals: 1. + self assert: dendrogram left right index equals: 3. + self assert: dendrogram right left index equals: 5. + self assert: dendrogram right right index equals: 7 +] + +{ #category : #test } +AIDendrogramTest >> testSize [ + self assert: dendrogram size equals: 7. + self assert: dendrogram left size equals: 4. + self assert: dendrogram right size equals: 3. + self assert: dendrogram left left size equals: 2. + self assert: dendrogram left right size equals: 2. + self assert: dendrogram right left size equals: 2. + self assert: dendrogram right right size equals: 1 +] diff --git a/src/AI-HierarchicalClustering-Tests/AIDistanceSquareTest.class.st b/src/AI-HierarchicalClustering-Tests/AIDistanceSquareTest.class.st index fbc8558..47ebb79 100644 --- a/src/AI-HierarchicalClustering-Tests/AIDistanceSquareTest.class.st +++ b/src/AI-HierarchicalClustering-Tests/AIDistanceSquareTest.class.st @@ -1,140 +1,140 @@ -" -A MLDistanceSquareTest is a test class for testing the behavior of MLDistanceSquare -" -Class { - #name : #AIDistanceSquareTest, - #superclass : #TestCase, - #instVars : [ - 'distanceMatrix' - ], - #category : #'AI-HierarchicalClustering-Tests-Math' -} - -{ #category : #test } -AIDistanceSquareTest >> distanceSquare10Elements [ - " Answer a distance square matrix of ten elements manually built " - - ^ AIDistanceSquare new - items: (1 to: 10) asArray; - partialColumns: { - (self vectorClass withAll: #(0)) . - self vectorClass withAll: #(1 0) . - self vectorClass withAll: #(2 5 0) . - self vectorClass withAll: #(3 6 8 0) . - self vectorClass withAll: #(4 7 9 10 0) }; - yourself. - -] - -{ #category : #test } -AIDistanceSquareTest >> distanceSquare15Elements [ - " Answer a distance square matrix of 15 elements manually built " - - ^ AIDistanceSquare new - items: (1 to: 10) asArray; - partialColumns: { - (self vectorClass withAll: #(0)) . - self vectorClass withAll: #(1 0) . - self vectorClass withAll: #(2 6 0) . - self vectorClass withAll: #(3 7 10 0) . - self vectorClass withAll: #(4 8 11 13 0) . - self vectorClass withAll: #(5 9 12 14 15 0) }; - yourself. - -] - -{ #category : #test } -AIDistanceSquareTest >> matrixClass [ - - ^ AIDistanceSquare -] - -{ #category : #test } -AIDistanceSquareTest >> testAtAndPut [ - - | validDs | - - validDs := self matrixClass on: #(1 2 3). - self should: [ validDs at: 1 and: 1 put: 0 ] raise: Error. - -] - -{ #category : #test } -AIDistanceSquareTest >> testCopyClusteringData [ - - | ds expected | - - ds := self matrixClass on1D: #(1 2 3). - expected := { - self vectorClass with: 0 . - self vectorClass withAll: #(1 0) . - self vectorClass withAll: #(2 3 0) }. - self assert: ds copyClusteringData equals: expected. - - -] - -{ #category : #test } -AIDistanceSquareTest >> testDistanceBlock [ - - | ds | - - ds := self matrixClass new. - self assert: (ds distanceBlock isKindOf: BlockClosure). - ds distanceBlock: [ : a : b | a + b ]. - self assert: (ds distanceBlock isKindOf: BlockClosure). -] - -{ #category : #test } -AIDistanceSquareTest >> testInitializeFrom1D [ - - | invalidItemsSizeCollection aMatrix | - - aMatrix := self matrixClass basicNew. - self assert: (aMatrix initializeFrom1D: (1 to: 10) asArray) equals: self distanceSquare10Elements. - self assert: (aMatrix initializeFrom1D: (1 to: 15) asArray) equals: self distanceSquare15Elements. - - invalidItemsSizeCollection := #(10 20). - self should: [ aMatrix initializeFrom1D: invalidItemsSizeCollection ] raise: Error. - invalidItemsSizeCollection := #(10 20 30 40). - self should: [ aMatrix initializeFrom1D: invalidItemsSizeCollection ] raise: Error. - invalidItemsSizeCollection := #(10 20 30 40 50). - self should: [ aMatrix initializeFrom1D: invalidItemsSizeCollection ] raise: Error. -] - -{ #category : #test } -AIDistanceSquareTest >> testItems [ - - | ds newItems | - - ds := self matrixClass on1D: #(1 2 3). - self assert: ds items equals: #(1 2 3). - - newItems := #(2 3 0). - ds items: newItems. - self assert: ds items equals: newItems. -] - -{ #category : #test } -AIDistanceSquareTest >> testNewVectorOfSizeItemsFromDimensions [ - - | ds vector | - - ds := self matrixClass new. - vector := ds - newVectorOfSize: 1 - itemsFrom: (1 to: 10) asArray - dimensions: 5. - self assert: vector equals: (self vectorClass with: 0). - vector := ds - newVectorOfSize: 5 - itemsFrom: (1 to: 10) asArray - dimensions: 5. - self assert: vector equals: (self vectorClass withAll: #(4 7 9 10 0)). -] - -{ #category : #test } -AIDistanceSquareTest >> vectorClass [ - - ^ AIArrayVector -] +" +A MLDistanceSquareTest is a test class for testing the behavior of MLDistanceSquare +" +Class { + #name : #AIDistanceSquareTest, + #superclass : #TestCase, + #instVars : [ + 'distanceMatrix' + ], + #category : #'AI-HierarchicalClustering-Tests-Math' +} + +{ #category : #test } +AIDistanceSquareTest >> distanceSquare10Elements [ + " Answer a distance square matrix of ten elements manually built " + + ^ AIDistanceSquare new + items: (1 to: 10) asArray; + partialColumns: { + (self vectorClass withAll: #(0)) . + self vectorClass withAll: #(1 0) . + self vectorClass withAll: #(2 5 0) . + self vectorClass withAll: #(3 6 8 0) . + self vectorClass withAll: #(4 7 9 10 0) }; + yourself. + +] + +{ #category : #test } +AIDistanceSquareTest >> distanceSquare15Elements [ + " Answer a distance square matrix of 15 elements manually built " + + ^ AIDistanceSquare new + items: (1 to: 10) asArray; + partialColumns: { + (self vectorClass withAll: #(0)) . + self vectorClass withAll: #(1 0) . + self vectorClass withAll: #(2 6 0) . + self vectorClass withAll: #(3 7 10 0) . + self vectorClass withAll: #(4 8 11 13 0) . + self vectorClass withAll: #(5 9 12 14 15 0) }; + yourself. + +] + +{ #category : #test } +AIDistanceSquareTest >> matrixClass [ + + ^ AIDistanceSquare +] + +{ #category : #test } +AIDistanceSquareTest >> testAtAndPut [ + + | validDs | + + validDs := self matrixClass on: #(1 2 3). + self should: [ validDs at: 1 and: 1 put: 0 ] raise: Error. + +] + +{ #category : #test } +AIDistanceSquareTest >> testCopyClusteringData [ + + | ds expected | + + ds := self matrixClass on1D: #(1 2 3). + expected := { + self vectorClass with: 0 . + self vectorClass withAll: #(1 0) . + self vectorClass withAll: #(2 3 0) }. + self assert: ds copyClusteringData equals: expected. + + +] + +{ #category : #test } +AIDistanceSquareTest >> testDistanceBlock [ + + | ds | + + ds := self matrixClass new. + self assert: (ds distanceBlock isKindOf: BlockClosure). + ds distanceBlock: [ : a : b | a + b ]. + self assert: (ds distanceBlock isKindOf: BlockClosure). +] + +{ #category : #test } +AIDistanceSquareTest >> testInitializeFrom1D [ + + | invalidItemsSizeCollection aMatrix | + + aMatrix := self matrixClass basicNew. + self assert: (aMatrix initializeFrom1D: (1 to: 10) asArray) equals: self distanceSquare10Elements. + self assert: (aMatrix initializeFrom1D: (1 to: 15) asArray) equals: self distanceSquare15Elements. + + invalidItemsSizeCollection := #(10 20). + self should: [ aMatrix initializeFrom1D: invalidItemsSizeCollection ] raise: Error. + invalidItemsSizeCollection := #(10 20 30 40). + self should: [ aMatrix initializeFrom1D: invalidItemsSizeCollection ] raise: Error. + invalidItemsSizeCollection := #(10 20 30 40 50). + self should: [ aMatrix initializeFrom1D: invalidItemsSizeCollection ] raise: Error. +] + +{ #category : #test } +AIDistanceSquareTest >> testItems [ + + | ds newItems | + + ds := self matrixClass on1D: #(1 2 3). + self assert: ds items equals: #(1 2 3). + + newItems := #(2 3 0). + ds items: newItems. + self assert: ds items equals: newItems. +] + +{ #category : #test } +AIDistanceSquareTest >> testNewVectorOfSizeItemsFromDimensions [ + + | ds vector | + + ds := self matrixClass new. + vector := ds + newVectorOfSize: 1 + itemsFrom: (1 to: 10) asArray + dimensions: 5. + self assert: vector equals: (self vectorClass with: 0). + vector := ds + newVectorOfSize: 5 + itemsFrom: (1 to: 10) asArray + dimensions: 5. + self assert: vector equals: (self vectorClass withAll: #(4 7 9 10 0)). +] + +{ #category : #test } +AIDistanceSquareTest >> vectorClass [ + + ^ AIArrayVector +] diff --git a/src/AI-HierarchicalClustering-Tests/AISimilarityItemTest.class.st b/src/AI-HierarchicalClustering-Tests/AISimilarityItemTest.class.st index c6d4148..d0ab4ec 100644 --- a/src/AI-HierarchicalClustering-Tests/AISimilarityItemTest.class.st +++ b/src/AI-HierarchicalClustering-Tests/AISimilarityItemTest.class.st @@ -1,44 +1,44 @@ -" -Copyright (c), 2004-2007 Adrian Kuhn. This class is part of Hapax. Hapax is distributed under BSD License, see package comment. -" -Class { - #name : #AISimilarityItemTest, - #superclass : #TestCase, - #category : #'AI-HierarchicalClustering-Tests-Core' -} - -{ #category : #'As yet unclassified' } -AISimilarityItemTest >> testDistance [ - | a b c elements engine clusters | - a := AIVectorItem with: #a and: #(1 0). - b := AIVectorItem with: #b and: #(0 1). - c := AIVectorItem with: #c and: #(0 7). - elements := Array with: a with: b with: c. - engine := AIClusterEngine with: elements. - engine hierarchicalClusteringUsing: #averageLinkage. - clusters := engine dendrogram breakInto: 2. - self assert: clusters size equals: 2. - self assert: (clusters select: [ :each | each size = 1 ]) size equals: 1. - self assert: (clusters select: [ :each | each size = 2 ]) size equals: 1. - self assert: ((clusters select: [ :each | each size = 1 ]) first includes: c). - self assert: ((clusters select: [ :each | each size = 2 ]) first includes: a). - self assert: ((clusters select: [ :each | each size = 2 ]) first includes: b) -] - -{ #category : #'As yet unclassified' } -AISimilarityItemTest >> testSimilarity [ - | a b c elements engine clusters | - a := AISimilarityItem with: #a and: #(1 0). - b := AISimilarityItem with: #b and: #(0 1). - c := AISimilarityItem with: #c and: #(0 7). - elements := Array with: a with: b with: c. - engine := AIClusterEngine with: elements. - engine hierarchicalClusteringUsing: #averageLinkage. - clusters := engine dendrogram breakInto: 2. - self assert: clusters size equals: 2. - self assert: (clusters select: [ :each | each size = 1 ]) size equals: 1. - self assert: (clusters select: [ :each | each size = 2 ]) size equals: 1. - self assert: ((clusters select: [ :each | each size = 1 ]) first includes: a). - self assert: ((clusters select: [ :each | each size = 2 ]) first includes: b). - self assert: ((clusters select: [ :each | each size = 2 ]) first includes: c) -] +" +Copyright (c), 2004-2007 Adrian Kuhn. This class is part of Hapax. Hapax is distributed under BSD License, see package comment. +" +Class { + #name : #AISimilarityItemTest, + #superclass : #TestCase, + #category : #'AI-HierarchicalClustering-Tests-Core' +} + +{ #category : #'As yet unclassified' } +AISimilarityItemTest >> testDistance [ + | a b c elements engine clusters | + a := AIVectorItem with: #a and: #(1 0). + b := AIVectorItem with: #b and: #(0 1). + c := AIVectorItem with: #c and: #(0 7). + elements := Array with: a with: b with: c. + engine := AIClusterEngine with: elements. + engine hierarchicalClusteringUsing: #averageLinkage. + clusters := engine dendrogram breakInto: 2. + self assert: clusters size equals: 2. + self assert: (clusters select: [ :each | each size = 1 ]) size equals: 1. + self assert: (clusters select: [ :each | each size = 2 ]) size equals: 1. + self assert: ((clusters select: [ :each | each size = 1 ]) first includes: c). + self assert: ((clusters select: [ :each | each size = 2 ]) first includes: a). + self assert: ((clusters select: [ :each | each size = 2 ]) first includes: b) +] + +{ #category : #'As yet unclassified' } +AISimilarityItemTest >> testSimilarity [ + | a b c elements engine clusters | + a := AISimilarityItem with: #a and: #(1 0). + b := AISimilarityItem with: #b and: #(0 1). + c := AISimilarityItem with: #c and: #(0 7). + elements := Array with: a with: b with: c. + engine := AIClusterEngine with: elements. + engine hierarchicalClusteringUsing: #averageLinkage. + clusters := engine dendrogram breakInto: 2. + self assert: clusters size equals: 2. + self assert: (clusters select: [ :each | each size = 1 ]) size equals: 1. + self assert: (clusters select: [ :each | each size = 2 ]) size equals: 1. + self assert: ((clusters select: [ :each | each size = 1 ]) first includes: a). + self assert: ((clusters select: [ :each | each size = 2 ]) first includes: b). + self assert: ((clusters select: [ :each | each size = 2 ]) first includes: c) +] diff --git a/src/AI-HierarchicalClustering-Tests/IntegerTest.extension.st b/src/AI-HierarchicalClustering-Tests/IntegerTest.extension.st index 85f0357..9fa79f4 100644 --- a/src/AI-HierarchicalClustering-Tests/IntegerTest.extension.st +++ b/src/AI-HierarchicalClustering-Tests/IntegerTest.extension.st @@ -1,12 +1,12 @@ -Extension { #name : #IntegerTest } - -{ #category : #'*AI-HierarchicalClustering-Tests' } -IntegerTest >> testFindNK [ - - self assert: 6 findNK equals: #(6 1 6 5 4 2). - self assert: 10 findNK equals: #(10 1 10 9 5 2 5 3). - self assert: 21 findNK equals: #(21 1 21 20 7 2 7 5). - - self should: [ 1 findNK ] raise: Error. - self should: [ 0 findNK ] raise: Error. -] +Extension { #name : #IntegerTest } + +{ #category : #'*AI-HierarchicalClustering-Tests' } +IntegerTest >> testFindNK [ + + self assert: 6 findNK equals: #(6 1 6 5 4 2). + self assert: 10 findNK equals: #(10 1 10 9 5 2 5 3). + self assert: 21 findNK equals: #(21 1 21 20 7 2 7 5). + + self should: [ 1 findNK ] raise: Error. + self should: [ 0 findNK ] raise: Error. +] diff --git a/src/AI-HierarchicalClustering-Tests/package.st b/src/AI-HierarchicalClustering-Tests/package.st index ff060ae..c44954c 100644 --- a/src/AI-HierarchicalClustering-Tests/package.st +++ b/src/AI-HierarchicalClustering-Tests/package.st @@ -1 +1 @@ -Package { #name : #'AI-HierarchicalClustering-Tests' } +Package { #name : #'AI-HierarchicalClustering-Tests' }