From 8072b8bdc649b14eb52064d54dd4c4cd6ce0d386 Mon Sep 17 00:00:00 2001 From: Katter Date: Mon, 17 Jun 2024 00:12:01 +0500 Subject: [PATCH] ClassifierWeights and PoserWeights, Weights tweaks --- .../DataSets/Classifier/ClassifierDataSet.cs | 2 ++ .../DataSets/Classifier/ClassifierWeights.cs | 29 ++++++++++++++++ .../Classifier/ClassifierWeightsLibrary.cs | 11 ++++++ SightKeeper.Domain/Model/DataSets/DataSet.cs | 6 +--- .../Detector/DetectorAssetsLibrary.cs | 5 ++- .../DataSets/Detector/DetectorDataSet.cs | 5 +-- .../DataSets/Detector/DetectorWeights.cs | 24 ++++++------- .../Detector/DetectorWeightsLibrary.cs | 33 +++++------------- .../Model/DataSets/Poser/PoserWeights.cs | 29 ++++++++++++++++ .../DataSets/Poser/PoserWeightsLibrary.cs | 21 ++++++++++++ SightKeeper.Domain/Model/DataSets/Weights.cs | 15 ++++++++ .../Model/DataSets/WeightsDateComparer.cs | 10 +++--- .../Model/DataSets/WeightsLibrary.cs | 34 +++++++++++++++++++ 13 files changed, 171 insertions(+), 53 deletions(-) create mode 100644 SightKeeper.Domain/Model/DataSets/Classifier/ClassifierWeights.cs create mode 100644 SightKeeper.Domain/Model/DataSets/Classifier/ClassifierWeightsLibrary.cs create mode 100644 SightKeeper.Domain/Model/DataSets/Poser/PoserWeights.cs create mode 100644 SightKeeper.Domain/Model/DataSets/Poser/PoserWeightsLibrary.cs create mode 100644 SightKeeper.Domain/Model/DataSets/Weights.cs create mode 100644 SightKeeper.Domain/Model/DataSets/WeightsLibrary.cs diff --git a/SightKeeper.Domain/Model/DataSets/Classifier/ClassifierDataSet.cs b/SightKeeper.Domain/Model/DataSets/Classifier/ClassifierDataSet.cs index dc2b1fe3..2b059780 100644 --- a/SightKeeper.Domain/Model/DataSets/Classifier/ClassifierDataSet.cs +++ b/SightKeeper.Domain/Model/DataSets/Classifier/ClassifierDataSet.cs @@ -4,10 +4,12 @@ public sealed class ClassifierDataSet : DataSet { public ClassifierTagsLibrary Tags { get; } public ClassifierAssetsLibrary Assets { get; } + public ClassifierWeightsLibrary Weights { get; } public ClassifierDataSet(string name, ushort resolution) : base(name, resolution) { Tags = new ClassifierTagsLibrary(this); Assets = new ClassifierAssetsLibrary(this); + Weights = new ClassifierWeightsLibrary(this); } } \ No newline at end of file diff --git a/SightKeeper.Domain/Model/DataSets/Classifier/ClassifierWeights.cs b/SightKeeper.Domain/Model/DataSets/Classifier/ClassifierWeights.cs new file mode 100644 index 00000000..0ee3ce71 --- /dev/null +++ b/SightKeeper.Domain/Model/DataSets/Classifier/ClassifierWeights.cs @@ -0,0 +1,29 @@ +using System.Collections.Immutable; +using CommunityToolkit.Diagnostics; + +namespace SightKeeper.Domain.Model.DataSets.Classifier; + +public sealed class ClassifierWeights : Weights +{ + public IReadOnlyCollection Tags { get; } + public ClassifierWeightsLibrary Library { get; } + public ClassifierDataSet DataSet => Library.DataSet; + + internal ClassifierWeights( + ModelSize size, + WeightsMetrics metrics, + IEnumerable tags, + ClassifierWeightsLibrary library) + : base(size, metrics) + { + Tags = tags.ToImmutableArray(); + Library = library; + ValidateTags(); + } + + private void ValidateTags() + { + foreach (var tag in Tags) + Guard.IsReferenceEqualTo(tag.DataSet, DataSet); + } +} \ No newline at end of file diff --git a/SightKeeper.Domain/Model/DataSets/Classifier/ClassifierWeightsLibrary.cs b/SightKeeper.Domain/Model/DataSets/Classifier/ClassifierWeightsLibrary.cs new file mode 100644 index 00000000..59620214 --- /dev/null +++ b/SightKeeper.Domain/Model/DataSets/Classifier/ClassifierWeightsLibrary.cs @@ -0,0 +1,11 @@ +namespace SightKeeper.Domain.Model.DataSets.Classifier; + +public sealed class ClassifierWeightsLibrary : WeightsLibrary +{ + public ClassifierDataSet DataSet { get; } + + internal ClassifierWeightsLibrary(ClassifierDataSet dataSet) + { + DataSet = dataSet; + } +} \ No newline at end of file diff --git a/SightKeeper.Domain/Model/DataSets/DataSet.cs b/SightKeeper.Domain/Model/DataSets/DataSet.cs index f1baa6d5..e4cb25c2 100644 --- a/SightKeeper.Domain/Model/DataSets/DataSet.cs +++ b/SightKeeper.Domain/Model/DataSets/DataSet.cs @@ -1,6 +1,4 @@ -using SightKeeper.Domain.Model.DataSets.Detector; - -namespace SightKeeper.Domain.Model.DataSets; +namespace SightKeeper.Domain.Model.DataSets; public abstract class DataSet { @@ -9,7 +7,6 @@ public abstract class DataSet public Game? Game { get; set; } public ushort Resolution { get; } public ScreenshotsLibrary Screenshots { get; } - public DetectorWeightsLibrary Weights { get; } public override string ToString() => Name; @@ -19,6 +16,5 @@ protected DataSet(string name, ushort resolution) Description = string.Empty; Resolution = resolution; Screenshots = new ScreenshotsLibrary(); - Weights = new DetectorWeightsLibrary(); } } \ No newline at end of file diff --git a/SightKeeper.Domain/Model/DataSets/Detector/DetectorAssetsLibrary.cs b/SightKeeper.Domain/Model/DataSets/Detector/DetectorAssetsLibrary.cs index 2263044a..28549a9c 100644 --- a/SightKeeper.Domain/Model/DataSets/Detector/DetectorAssetsLibrary.cs +++ b/SightKeeper.Domain/Model/DataSets/Detector/DetectorAssetsLibrary.cs @@ -2,6 +2,8 @@ public sealed class DetectorAssetsLibrary : AssetsLibrary { + public DetectorDataSet DataSet { get; } + public DetectorAsset MakeAsset(Screenshot screenshot) { DetectorAsset asset = new(screenshot); @@ -9,7 +11,8 @@ public DetectorAsset MakeAsset(Screenshot screenshot) return asset; } - internal DetectorAssetsLibrary() + internal DetectorAssetsLibrary(DetectorDataSet dataSet) { + DataSet = dataSet; } } \ No newline at end of file diff --git a/SightKeeper.Domain/Model/DataSets/Detector/DetectorDataSet.cs b/SightKeeper.Domain/Model/DataSets/Detector/DetectorDataSet.cs index d8f2b199..6dfd9968 100644 --- a/SightKeeper.Domain/Model/DataSets/Detector/DetectorDataSet.cs +++ b/SightKeeper.Domain/Model/DataSets/Detector/DetectorDataSet.cs @@ -4,11 +4,12 @@ public sealed class DetectorDataSet : DataSet { public DetectorTagsLibrary Tags { get; } public DetectorAssetsLibrary Assets { get; } + public DetectorWeightsLibrary Weights { get; } public DetectorDataSet(string name, ushort resolution) : base(name, resolution) { Tags = new DetectorTagsLibrary(this); - Assets = new DetectorAssetsLibrary(); - + Assets = new DetectorAssetsLibrary(this); + Weights = new DetectorWeightsLibrary(this); } } \ No newline at end of file diff --git a/SightKeeper.Domain/Model/DataSets/Detector/DetectorWeights.cs b/SightKeeper.Domain/Model/DataSets/Detector/DetectorWeights.cs index 62815c58..5ed2682d 100644 --- a/SightKeeper.Domain/Model/DataSets/Detector/DetectorWeights.cs +++ b/SightKeeper.Domain/Model/DataSets/Detector/DetectorWeights.cs @@ -1,35 +1,31 @@ using System.Collections.Immutable; +using CommunityToolkit.Diagnostics; namespace SightKeeper.Domain.Model.DataSets.Detector; -public sealed class DetectorWeights +public sealed class DetectorWeights : Weights { - public DateTime CreationDate { get; } - public ModelSize Size { get; } - public WeightsMetrics WeightsMetrics { get; } public IReadOnlyCollection Tags { get; } public DetectorWeightsLibrary Library { get; } + public DetectorDataSet DataSet => Library.DataSet; internal DetectorWeights( - ModelSize modelSize, - WeightsMetrics weightsMetrics, + ModelSize size, + WeightsMetrics metrics, IEnumerable tags, DetectorWeightsLibrary library) + : base(size, metrics) { - CreationDate = DateTime.Now; - Size = modelSize; - WeightsMetrics = weightsMetrics; + Tags = tags.ToImmutableArray(); Library = library; - Tags = tags.ToImmutableList(); ValidateTags(); } - public override string ToString() => $"{nameof(Size)}: {Size}, {WeightsMetrics}"; + public override string ToString() => $"{nameof(Size)}: {Size}, {Metrics}"; private void ValidateTags() { - // TODO - /*var isAllTagsBelongsToGivenLibrary = Tags.All(tag => Library.DataSet.Tags.Contains(tag)); - Guard.IsTrue(isAllTagsBelongsToGivenLibrary);*/ + foreach (var tag in Tags) + Guard.IsReferenceEqualTo(tag.DataSet, DataSet); } } \ No newline at end of file diff --git a/SightKeeper.Domain/Model/DataSets/Detector/DetectorWeightsLibrary.cs b/SightKeeper.Domain/Model/DataSets/Detector/DetectorWeightsLibrary.cs index ddf43b4b..a85228ce 100644 --- a/SightKeeper.Domain/Model/DataSets/Detector/DetectorWeightsLibrary.cs +++ b/SightKeeper.Domain/Model/DataSets/Detector/DetectorWeightsLibrary.cs @@ -1,16 +1,13 @@ -using System.Collections; -using CommunityToolkit.Diagnostics; +namespace SightKeeper.Domain.Model.DataSets.Detector; -namespace SightKeeper.Domain.Model.DataSets.Detector; - -public sealed class DetectorWeightsLibrary : IReadOnlyCollection +public sealed class DetectorWeightsLibrary : WeightsLibrary { - public int Count => _weights.Count; + public DetectorDataSet DataSet { get; } - public IEnumerator GetEnumerator() - { - return _weights.GetEnumerator(); - } + internal DetectorWeightsLibrary(DetectorDataSet dataSet) + { + DataSet = dataSet; + } internal DetectorWeights CreateWeights( ModelSize modelSize, @@ -18,21 +15,7 @@ internal DetectorWeights CreateWeights( IEnumerable tags) { DetectorWeights weights = new(modelSize, metrics, tags, this); - var isAdded = _weights.Add(weights); - Guard.IsTrue(isAdded); + AddWeights(weights); return weights; } - - internal void RemoveWeights(DetectorWeights weights) - { - var isRemoved = _weights.Remove(weights); - Guard.IsTrue(isRemoved); - } - - private readonly SortedSet _weights = new(WeightsDateComparer.Instance); - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } } \ No newline at end of file diff --git a/SightKeeper.Domain/Model/DataSets/Poser/PoserWeights.cs b/SightKeeper.Domain/Model/DataSets/Poser/PoserWeights.cs new file mode 100644 index 00000000..9eecc5ef --- /dev/null +++ b/SightKeeper.Domain/Model/DataSets/Poser/PoserWeights.cs @@ -0,0 +1,29 @@ +using System.Collections.Immutable; +using CommunityToolkit.Diagnostics; + +namespace SightKeeper.Domain.Model.DataSets.Poser; + +public sealed class PoserWeights : Weights +{ + public IReadOnlyCollection Tags { get; } + public PoserWeightsLibrary Library { get; } + public PoserDataSet DataSet => Library.DataSet; + + public PoserWeights( + ModelSize size, + WeightsMetrics metrics, + IEnumerable tags, + PoserWeightsLibrary library) + : base(size, metrics) + { + Tags = tags.ToImmutableArray(); + Library = library; + ValidateTags(); + } + + private void ValidateTags() + { + foreach (var tag in Tags) + Guard.IsReferenceEqualTo(tag.DataSet, DataSet); + } +} \ No newline at end of file diff --git a/SightKeeper.Domain/Model/DataSets/Poser/PoserWeightsLibrary.cs b/SightKeeper.Domain/Model/DataSets/Poser/PoserWeightsLibrary.cs new file mode 100644 index 00000000..8ad64d0d --- /dev/null +++ b/SightKeeper.Domain/Model/DataSets/Poser/PoserWeightsLibrary.cs @@ -0,0 +1,21 @@ +namespace SightKeeper.Domain.Model.DataSets.Poser; + +public sealed class PoserWeightsLibrary : WeightsLibrary +{ + public PoserDataSet DataSet { get; } + + internal PoserWeightsLibrary(PoserDataSet dataSet) + { + DataSet = dataSet; + } + + internal PoserWeights CreateWeights( + ModelSize modelSize, + WeightsMetrics metrics, + IEnumerable tags) + { + PoserWeights weights = new(modelSize, metrics, tags, this); + AddWeights(weights); + return weights; + } +} \ No newline at end of file diff --git a/SightKeeper.Domain/Model/DataSets/Weights.cs b/SightKeeper.Domain/Model/DataSets/Weights.cs new file mode 100644 index 00000000..da326ef6 --- /dev/null +++ b/SightKeeper.Domain/Model/DataSets/Weights.cs @@ -0,0 +1,15 @@ +namespace SightKeeper.Domain.Model.DataSets; + +public abstract class Weights +{ + public DateTime CreationDate { get; } + public ModelSize Size { get; } + public WeightsMetrics Metrics { get; } + + protected Weights(ModelSize size, WeightsMetrics metrics) + { + CreationDate = DateTime.Now; + Size = size; + Metrics = metrics; + } +} \ No newline at end of file diff --git a/SightKeeper.Domain/Model/DataSets/WeightsDateComparer.cs b/SightKeeper.Domain/Model/DataSets/WeightsDateComparer.cs index 7190daa6..75215bd8 100644 --- a/SightKeeper.Domain/Model/DataSets/WeightsDateComparer.cs +++ b/SightKeeper.Domain/Model/DataSets/WeightsDateComparer.cs @@ -1,12 +1,10 @@ -using SightKeeper.Domain.Model.DataSets.Detector; +namespace SightKeeper.Domain.Model.DataSets; -namespace SightKeeper.Domain.Model.DataSets; - -internal sealed class WeightsDateComparer : IComparer +internal sealed class WeightsDateComparer : IComparer where TWeights : Weights { - public static WeightsDateComparer Instance { get; } = new(); + public static WeightsDateComparer Instance { get; } = new(); - public int Compare(DetectorWeights? x, DetectorWeights? y) + public int Compare(TWeights? x, TWeights? y) { if (ReferenceEquals(x, y)) return 0; if (ReferenceEquals(null, y)) return 1; diff --git a/SightKeeper.Domain/Model/DataSets/WeightsLibrary.cs b/SightKeeper.Domain/Model/DataSets/WeightsLibrary.cs new file mode 100644 index 00000000..e93b53a8 --- /dev/null +++ b/SightKeeper.Domain/Model/DataSets/WeightsLibrary.cs @@ -0,0 +1,34 @@ +using System.Collections; +using CommunityToolkit.Diagnostics; + +namespace SightKeeper.Domain.Model.DataSets; + +public abstract class WeightsLibrary : IReadOnlyCollection where TWeights : Weights +{ + public int Count => _weights.Count; + + public IEnumerator GetEnumerator() + { + return _weights.GetEnumerator(); + } + + internal void RemoveWeights(TWeights weights) + { + var isRemoved = _weights.Remove(weights); + Guard.IsTrue(isRemoved); + } + + protected void AddWeights(TWeights weights) + { + bool isAdded = _weights.Add(weights); + Guard.IsTrue(isAdded); + } + + private readonly SortedSet _weights = new(WeightsDateComparer.Instance); + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + +} \ No newline at end of file