Skip to content

Commit

Permalink
ClassifierWeights and PoserWeights, Weights tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Neakita committed Jun 16, 2024
1 parent 2df11b5 commit 8072b8b
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
29 changes: 29 additions & 0 deletions SightKeeper.Domain/Model/DataSets/Classifier/ClassifierWeights.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Immutable;
using CommunityToolkit.Diagnostics;

namespace SightKeeper.Domain.Model.DataSets.Classifier;

public sealed class ClassifierWeights : Weights
{
public IReadOnlyCollection<ClassifierTag> Tags { get; }
public ClassifierWeightsLibrary Library { get; }
public ClassifierDataSet DataSet => Library.DataSet;

internal ClassifierWeights(
ModelSize size,
WeightsMetrics metrics,
IEnumerable<ClassifierTag> 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace SightKeeper.Domain.Model.DataSets.Classifier;

public sealed class ClassifierWeightsLibrary : WeightsLibrary<ClassifierWeights>
{
public ClassifierDataSet DataSet { get; }

internal ClassifierWeightsLibrary(ClassifierDataSet dataSet)
{
DataSet = dataSet;
}
}
6 changes: 1 addition & 5 deletions SightKeeper.Domain/Model/DataSets/DataSet.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using SightKeeper.Domain.Model.DataSets.Detector;

namespace SightKeeper.Domain.Model.DataSets;
namespace SightKeeper.Domain.Model.DataSets;

public abstract class DataSet
{
Expand All @@ -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;

Expand All @@ -19,6 +16,5 @@ protected DataSet(string name, ushort resolution)
Description = string.Empty;
Resolution = resolution;
Screenshots = new ScreenshotsLibrary();
Weights = new DetectorWeightsLibrary();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

public sealed class DetectorAssetsLibrary : AssetsLibrary<DetectorAsset>
{
public DetectorDataSet DataSet { get; }

public DetectorAsset MakeAsset(Screenshot screenshot)
{
DetectorAsset asset = new(screenshot);
AddAsset(asset);
return asset;
}

internal DetectorAssetsLibrary()
internal DetectorAssetsLibrary(DetectorDataSet dataSet)
{
DataSet = dataSet;
}
}
5 changes: 3 additions & 2 deletions SightKeeper.Domain/Model/DataSets/Detector/DetectorDataSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
24 changes: 10 additions & 14 deletions SightKeeper.Domain/Model/DataSets/Detector/DetectorWeights.cs
Original file line number Diff line number Diff line change
@@ -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<DetectorTag> Tags { get; }
public DetectorWeightsLibrary Library { get; }
public DetectorDataSet DataSet => Library.DataSet;

internal DetectorWeights(
ModelSize modelSize,
WeightsMetrics weightsMetrics,
ModelSize size,
WeightsMetrics metrics,
IEnumerable<DetectorTag> 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);
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,21 @@
using System.Collections;
using CommunityToolkit.Diagnostics;
namespace SightKeeper.Domain.Model.DataSets.Detector;

namespace SightKeeper.Domain.Model.DataSets.Detector;

public sealed class DetectorWeightsLibrary : IReadOnlyCollection<DetectorWeights>
public sealed class DetectorWeightsLibrary : WeightsLibrary<DetectorWeights>
{
public int Count => _weights.Count;
public DetectorDataSet DataSet { get; }

public IEnumerator<DetectorWeights> GetEnumerator()
{
return _weights.GetEnumerator();
}
internal DetectorWeightsLibrary(DetectorDataSet dataSet)
{
DataSet = dataSet;
}

internal DetectorWeights CreateWeights(
ModelSize modelSize,
WeightsMetrics metrics,
IEnumerable<DetectorTag> 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<DetectorWeights> _weights = new(WeightsDateComparer.Instance);

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
29 changes: 29 additions & 0 deletions SightKeeper.Domain/Model/DataSets/Poser/PoserWeights.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Immutable;
using CommunityToolkit.Diagnostics;

namespace SightKeeper.Domain.Model.DataSets.Poser;

public sealed class PoserWeights : Weights
{
public IReadOnlyCollection<PoserTag> Tags { get; }
public PoserWeightsLibrary Library { get; }
public PoserDataSet DataSet => Library.DataSet;

public PoserWeights(
ModelSize size,
WeightsMetrics metrics,
IEnumerable<PoserTag> 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);
}
}
21 changes: 21 additions & 0 deletions SightKeeper.Domain/Model/DataSets/Poser/PoserWeightsLibrary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace SightKeeper.Domain.Model.DataSets.Poser;

public sealed class PoserWeightsLibrary : WeightsLibrary<PoserWeights>
{
public PoserDataSet DataSet { get; }

internal PoserWeightsLibrary(PoserDataSet dataSet)
{
DataSet = dataSet;
}

internal PoserWeights CreateWeights(
ModelSize modelSize,
WeightsMetrics metrics,
IEnumerable<PoserTag> tags)
{
PoserWeights weights = new(modelSize, metrics, tags, this);
AddWeights(weights);
return weights;
}
}
15 changes: 15 additions & 0 deletions SightKeeper.Domain/Model/DataSets/Weights.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
10 changes: 4 additions & 6 deletions SightKeeper.Domain/Model/DataSets/WeightsDateComparer.cs
Original file line number Diff line number Diff line change
@@ -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<DetectorWeights>
internal sealed class WeightsDateComparer<TWeights> : IComparer<TWeights> where TWeights : Weights
{
public static WeightsDateComparer Instance { get; } = new();
public static WeightsDateComparer<TWeights> 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;
Expand Down
34 changes: 34 additions & 0 deletions SightKeeper.Domain/Model/DataSets/WeightsLibrary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections;
using CommunityToolkit.Diagnostics;

namespace SightKeeper.Domain.Model.DataSets;

public abstract class WeightsLibrary<TWeights> : IReadOnlyCollection<TWeights> where TWeights : Weights
{
public int Count => _weights.Count;

public IEnumerator<TWeights> 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<TWeights> _weights = new(WeightsDateComparer<TWeights>.Instance);

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

}

0 comments on commit 8072b8b

Please sign in to comment.