forked from amkovkov/GranuSightSoftware2
1) remove unused components: camera view (due it causes segmentation faults), compute resources and all related, illuminator controller (due in useless without camera view), image storage service, temperature and humidity service 2) database schema - reduce tables references, features storing in records themselves in compressed form, add records creation and editing date and time, add separator comment column 3) analysis - rework of pipeline and ui, now database storing only raw data and all display values calculated from it 4) lights service - add reconnection if disconnected 5) add width and height command line arguments 6) fix some typos and other issues
78 lines
3.0 KiB
C#
78 lines
3.0 KiB
C#
using System.Collections;
|
|
|
|
namespace GSS2.Core.Extensions;
|
|
|
|
public static class EnumerableExtensions
|
|
{
|
|
public static IEnumerable<(int i, T value)> Enumerate<T>(this IEnumerable<T> values) => values.Select((value, i) => (i, value));
|
|
|
|
public static string ToString<TKey, TValue>(this IDictionary<TKey, TValue> values, string separator, int indent) => string.Join(separator, values.Select(v => $"{new string(' ', indent)} {v.Key}: {v.Value}"));
|
|
public static string ToString(this IDictionary values, string separator, int indent)
|
|
{
|
|
var strs = new List<string?>();
|
|
var enumerator = values.GetEnumerator();
|
|
while (enumerator.MoveNext())
|
|
strs.Add($"{enumerator.Key}: {enumerator.Value}");
|
|
return string.Join(separator, strs.Select(s => $"{new string(' ', indent)}{s}"));
|
|
}
|
|
|
|
public static string ToString<TValue>(this IEnumerable<TValue> values, string separator, int indent) => (values as IEnumerable).ToString(separator, indent);
|
|
public static string ToString(this IEnumerable values, string separator, int indent)
|
|
{
|
|
var strs = new List<string?>();
|
|
var enumerator = values.GetEnumerator();
|
|
while (enumerator.MoveNext())
|
|
strs.Add(enumerator.Current.ToString());
|
|
return string.Join(separator, strs.Select(s => $"{new string(' ', indent)}{s}"));
|
|
}
|
|
|
|
public static bool All(this IEnumerable<bool> values) => values.All(v => v);
|
|
public static bool Any(this IEnumerable<bool> values) => values.Any(v => v);
|
|
|
|
public static void AddRange<TValue, TKey>(this IDictionary<TValue, TKey> values, IEnumerable<KeyValuePair<TValue, TKey>> collection)
|
|
{
|
|
foreach (var kv in collection)
|
|
values.Add(kv);
|
|
}
|
|
|
|
public static double Variance(this IEnumerable<double> values)
|
|
{
|
|
if (values.Count() == 1)
|
|
return 0;
|
|
|
|
double mean = values.Average();
|
|
double variance = 0.0;
|
|
foreach (int value in values)
|
|
variance += Math.Pow(value - mean, 2.0);
|
|
return variance / values.Count();
|
|
}
|
|
public static double StdDev(this IEnumerable<double> values) => Math.Sqrt(Variance(values));
|
|
public static float Variance(this IEnumerable<float> values)
|
|
{
|
|
if (values.Count() == 1)
|
|
return 0;
|
|
|
|
float mean = values.Average();
|
|
float variance = 0.0f;
|
|
foreach (int value in values)
|
|
variance += MathF.Pow(value - mean, 2.0f);
|
|
return variance / values.Count();
|
|
}
|
|
public static float StdDev(this IEnumerable<float> values) => MathF.Sqrt(Variance(values));
|
|
|
|
public static IEnumerable<IEnumerable<TValue>> Chunk<TValue>(this IEnumerable<TValue> values, IEnumerable<int> chunks)
|
|
{
|
|
if (chunks.Count() == 0)
|
|
throw new InvalidDataException();
|
|
if (values.Count() != chunks.Sum())
|
|
throw new InvalidDataException();
|
|
|
|
var start = 0;
|
|
foreach (var chunk in chunks)
|
|
{
|
|
yield return values.Skip(start).Take(chunk);
|
|
start += chunk;
|
|
}
|
|
}
|
|
}
|