-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithmLoader.cs
More file actions
104 lines (97 loc) · 4.25 KB
/
AlgorithmLoader.cs
File metadata and controls
104 lines (97 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Xml.Linq;
namespace Nurielite
{
/// <summary>
/// Entrypoint class for adding a block associated with a python algorithm to the interface.
/// </summary>
/// <remarks>
/// Loading an algorithm block loads EVERYTHING about an pyalgorithm interface (<see cref="PyAlgorithm"/>, meaning if changes are made without recreating the block, they are NOT applied to this.
/// </remarks>
class AlgorithmLoader
{
//NOTE: python file path can be reconstructed by eFamily/sName/sName.py
//NOTE: THIS IS ENTRYPOINT FOR NEW BLOCK
/// <summary>
/// Entrypoint function for adding a block associated with a python algorithm to the interface.
/// </summary>
/// <param name="sName">Name of the algorithm</param>
/// <param name="eFamily">Type of algorithm</param>
/// <param name="iNumInputs">Number of block inputs</param>
/// <param name="iNumOutputs">Number of block outputs</param>
public static void loadAlgorithmBlock(string sName, AlgorithmType eFamily, int iNumInputs, int iNumOutputs)
{
// load the python algorithm
PythonGenerator pPyGen = new PythonGenerator();
PyAlgorithm pPyAlgorithm = pPyGen.loadPythonAlgorithm(eFamily.ToString() + "/" + sName, sName + ".py");
// get algorithm meta data for default nodule labels (should be under key "default")
Dictionary<string, string> dAlgMetaData = pPyAlgorithm.getMetaData();
List<string> lInputNames = new List<string>();
string sOutputNames = "";
if(dAlgMetaData.ContainsKey("Default")) //TODO add error handling
{
string sRawLabelsXml = dAlgMetaData["Default"];
XElement pLabelsXml = XElement.Parse(sRawLabelsXml);
XElement pInputRootXml = pLabelsXml.Element("inputs");
foreach (XElement pElement in pInputRootXml.Elements("input"))
lInputNames.Add(pElement.Value);
sOutputNames = pLabelsXml.Element("output").Value;
}
else
{
// if no labels provided, set default "unnamed datatype"
sOutputNames = (iNumOutputs == 1) ? "unnamed datatype" : "";
for (int i = 0; i < iNumInputs; i++)
lInputNames.Add("unnamed datatype");
}
Block pBlock = AlgorithmLoader.generateBlock(sName, eFamily, iNumInputs, iNumOutputs, lInputNames, sOutputNames);
pBlock.PyAlgorithm = pPyAlgorithm;
//attempt to fill meta data of block from algorithm interface
//if (dAlgMetaData.ContainsKey("Version")) { pBlock.Version = dAlgMetaData["Version"]; }
Master.Blocks.Add(pBlock);
}
private static Block generateBlock(string sName, AlgorithmType eFamily, int iNumInputs, int iNumOutputs, List<string> lInputNames, string sOutputName)
{
// ensure proper label count
if(iNumInputs > lInputNames.Count)
{
for (int i = lInputNames.Count; i < iNumInputs; i++)
lInputNames.Add("unnamed datatype");
}
else if(iNumInputs < lInputNames.Count)
{
lInputNames.RemoveRange(iNumInputs, lInputNames.Count - iNumInputs);
}
// set the block color by algorithm family
Color pColor = Colors.Gray;
switch(eFamily)
{
case AlgorithmType.Classifier:
pColor = Colors.SeaGreen;
break;
case AlgorithmType.Clustering:
pColor = Colors.Turquoise;
break;
case AlgorithmType.DimensionReduction:
pColor = Colors.Thistle;
break;
case AlgorithmType.Operation:
pColor = Colors.SkyBlue;
break;
case AlgorithmType.Input:
pColor = Colors.Violet;
break;
case AlgorithmType.Output:
pColor = Colors.Violet;
break;
}
return new Block(lInputNames, sOutputName, sName, eFamily, pColor);
}
}
}