Skip to content

Commit

Permalink
Backend: Upgrade all packages from "math/rand" to "math/rand/v2"
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Mayer <michael@photoprism.app>
  • Loading branch information
lastzero committed May 19, 2024
1 parent 3d908c7 commit 8396170
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 42 deletions.
11 changes: 4 additions & 7 deletions internal/entity/entity_save_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package entity

import (
"math/rand"
"math/rand/v2"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/photoprism/photoprism/pkg/rnd"
)

func TestSave(t *testing.T) {
var r = rand.New(rand.NewSource(time.Now().UnixNano()))

t.Run("HasCreatedUpdatedAt", func(t *testing.T) {
id := 99999 + r.Intn(10000)
id := 99999 + rand.IntN(10000)
m := Photo{ID: uint(id), PhotoUID: rnd.GenerateUID(PhotoUID), UpdatedAt: TimeStamp(), CreatedAt: TimeStamp()}

if err := m.Save(); err != nil {
Expand All @@ -25,7 +22,7 @@ func TestSave(t *testing.T) {
assert.NotNil(t, FindPhoto(m))
})
t.Run("HasCreatedAt", func(t *testing.T) {
id := 99999 + r.Intn(10000)
id := 99999 + rand.IntN(10000)
m := Photo{ID: uint(id), PhotoUID: rnd.GenerateUID(PhotoUID), CreatedAt: TimeStamp()}

if err := m.Save(); err != nil {
Expand All @@ -36,7 +33,7 @@ func TestSave(t *testing.T) {
assert.NotNil(t, FindPhoto(m))
})
t.Run("NoCreatedAt", func(t *testing.T) {
id := 99999 + r.Intn(10000)
id := 99999 + rand.IntN(10000)
m := Photo{ID: uint(id), PhotoUID: rnd.GenerateUID(PhotoUID), CreatedAt: TimeStamp()}

if err := m.Save(); err != nil {
Expand Down
9 changes: 4 additions & 5 deletions internal/entity/entity_update_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package entity

import (
"math/rand"
"math/rand/v2"
"testing"
"time"

Expand All @@ -11,7 +11,6 @@ import (
)

func TestUpdate(t *testing.T) {
var r = rand.New(rand.NewSource(time.Now().UnixNano()))
t.Run("IDMissing", func(t *testing.T) {
uid := rnd.GenerateUID(PhotoUID)
m := &Photo{ID: 0, PhotoUID: uid, UpdatedAt: TimeStamp(), CreatedAt: TimeStamp(), PhotoTitle: "Foo"}
Expand All @@ -27,7 +26,7 @@ func TestUpdate(t *testing.T) {
assert.Equal(t, m.UpdatedAt.UTC(), updatedAt.UTC())
})
t.Run("UIDMissing", func(t *testing.T) {
id := 99999 + r.Intn(10000)
id := 99999 + rand.IntN(10000)
m := &Photo{ID: uint(id), PhotoUID: "", UpdatedAt: TimeStamp(), CreatedAt: TimeStamp(), PhotoTitle: "Foo"}
updatedAt := m.UpdatedAt

Expand All @@ -41,7 +40,7 @@ func TestUpdate(t *testing.T) {
assert.Equal(t, m.UpdatedAt.UTC(), updatedAt.UTC())
})
t.Run("NotUpdated", func(t *testing.T) {
id := 99999 + r.Intn(10000)
id := 99999 + rand.IntN(10000)
uid := rnd.GenerateUID(PhotoUID)
m := &Photo{ID: uint(id), PhotoUID: uid, UpdatedAt: time.Now(), CreatedAt: TimeStamp(), PhotoTitle: "Foo"}
updatedAt := m.UpdatedAt
Expand Down Expand Up @@ -84,7 +83,7 @@ func TestUpdate(t *testing.T) {
})
t.Run("NonExistentKeys", func(t *testing.T) {
m := PhotoFixtures.Pointer("Photo01")
m.ID = uint(10000000 + r.Intn(10000))
m.ID = uint(10000000 + rand.IntN(10000))
m.PhotoUID = rnd.GenerateUID(PhotoUID)
updatedAt := m.UpdatedAt
if err := Update(m, "ID", "PhotoUID"); err == nil {
Expand Down
11 changes: 4 additions & 7 deletions internal/face/embeddings_random.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package face

import (
"math/rand"
"time"
"math/rand/v2"
)

type Kind int
Expand All @@ -14,16 +13,14 @@ const (
AmbiguousFace
)

var r = rand.New(rand.NewSource(time.Now().UnixNano()))

// RandomDist returns a distance threshold for matching RandomDEmbeddings.
func RandomDist() float64 {
return RandomFloat64(0.75, 0.15)
}

// RandomFloat64 adds a random distance offset to a float64.
func RandomFloat64(f, d float64) float64 {
return f + (r.Float64()-0.5)*d
return f + (rand.Float64()-0.5)*d
}

// RandomEmbeddings returns random embeddings for testing.
Expand Down Expand Up @@ -73,7 +70,7 @@ func RandomKidsEmbedding() (result Embedding) {
result = make(Embedding, 512)

d := 0.1 / 512.0
n := 1 + r.Intn(len(KidsEmbeddings)-1)
n := 1 + rand.IntN(len(KidsEmbeddings)-1)
e := KidsEmbeddings[n]

for i := range result {
Expand All @@ -88,7 +85,7 @@ func RandomIgnoredEmbedding() (result Embedding) {
result = make(Embedding, 512)

d := 0.1 / 512.0
n := 1 + r.Intn(len(IgnoredEmbeddings)-1)
n := 1 + rand.IntN(len(IgnoredEmbeddings)-1)
e := IgnoredEmbeddings[n]

for i := range result {
Expand Down
2 changes: 1 addition & 1 deletion internal/frame/angle.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package frame

import (
"math/rand"
"math/rand/v2"
)

// RandomAngle returns a random angle between -max and max.
Expand Down
2 changes: 1 addition & 1 deletion internal/frame/point.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package frame

import (
"image"
"math/rand"
"math/rand/v2"
)

// RandomPoint returns a random image position within the specified range.
Expand Down
2 changes: 1 addition & 1 deletion pkg/clusters/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package clusters

import (
"container/heap"
"math/rand"
"math/rand/v2"
"sync"
)

Expand Down
9 changes: 2 additions & 7 deletions pkg/clusters/kmeans.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package clusters

import (
"math"
"math/rand"
"math/rand/v2"
"sync"
"time"

"gonum.org/v1/gonum/floats"
)
Expand Down Expand Up @@ -221,15 +220,13 @@ func (c *kmeansClusterer) initializeMeansWithData() {
c.m = make([][]float64, c.number)
c.n = make([][]float64, c.number)

rand.Seed(time.Now().UTC().Unix())

var (
k int
s, t, l, f float64
d []float64 = make([]float64, len(c.d))
)

c.m[0] = c.d[rand.Intn(len(c.d)-1)]
c.m[0] = c.d[rand.IntN(len(c.d)-1)]

for i := 1; i < c.number; i++ {
s = 0
Expand Down Expand Up @@ -264,8 +261,6 @@ func (c *kmeansClusterer) initializeMeansWithData() {
func (c *kmeansClusterer) initializeMeans() {
c.m = make([][]float64, c.number)

rand.Seed(time.Now().UTC().Unix())

for i := 0; i < c.number; i++ {
c.m[i] = make([]float64, c.dimension)
for j := 0; j < c.dimension; j++ {
Expand Down
12 changes: 4 additions & 8 deletions pkg/clusters/kmeans_estimator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package clusters

import (
"math"
"math/rand"
"time"
"math/rand/v2"

"gonum.org/v1/gonum/floats"
)
Expand All @@ -25,9 +24,8 @@ type kmeansEstimator struct {
d [][]float64
}

// Implementation of cluster number estimator using gap statistic
// ("Estimating the number of clusters in a data set via the gap statistic", Tibshirani et al.) with k-means++ as
// clustering algorithm
// KMeansEstimator implements a cluster number estimator using the gap statistic ("Estimating the number of clusters
// in a data set via the gap statistic", Tibshirani et al.) with k-means++ as clustering algorithm.
func KMeansEstimator(iterations, clusters int, distance DistFunc) (Estimator, error) {
if iterations < 1 {
return nil, errZeroIterations
Expand Down Expand Up @@ -128,15 +126,13 @@ func (c *kmeansEstimator) initializeMeansWithData() {
c.m = make([][]float64, c.number)
c.n = make([][]float64, c.number)

rand.Seed(time.Now().UTC().Unix())

var (
k int
s, t, l, f float64
d []float64 = make([]float64, len(c.d))
)

c.m[0] = c.d[rand.Intn(len(c.d)-1)]
c.m[0] = c.d[rand.IntN(len(c.d)-1)]

for i := 1; i < c.number; i++ {
s = 0
Expand Down
7 changes: 2 additions & 5 deletions pkg/geo/randomize.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package geo

import (
"math/rand"
"time"
"math/rand/v2"
)

var r = rand.New(rand.NewSource(time.Now().UnixNano()))

// Randomize adds a random offset to a value.
func Randomize(value, diameter float64) float64 {
return value + (r.Float64()-0.5)*diameter
return value + (rand.Float64()-0.5)*diameter
}

0 comments on commit 8396170

Please sign in to comment.