public void TestSerializingSimpleHypergrid() { string originalValidSimpleHypergridJsonString = PythonScriptsAndJsons.SpinlockSearchSpaceJson; SimpleHypergrid spinlockSearchSpace = new SimpleHypergrid( name: "SpinlockSearchSpace", dimensions: new List <IDimension>() { new DiscreteDimension(name: "shortBackOffMilliSeconds", min: 1, max: 1 << 20), new DiscreteDimension(name: "longBackOffMilliSeconds", min: 1, max: 1 << 20), new DiscreteDimension(name: "longBackOffWaitMilliSeconds", min: 1, max: 1 << 20), new DiscreteDimension(name: "minSpinCount", min: 1, max: 1 << 20), new DiscreteDimension(name: "maxSpinCount", min: 1, max: 1 << 20), new DiscreteDimension(name: "maxbackOffAttempts", min: 1, max: 1 << 20), new DiscreteDimension(name: "acquireSpinCount", min: 1, max: 1 << 20), new CategoricalDimension(name: "algorithm", values: new List <object> { "Optimistic", "ExponentialBackoff" }), }); var jsonSerializerOptions = new JsonSerializerOptions { WriteIndented = true, Converters = { new JsonStringEnumConverter(), new SimpleHypergridJsonConverter(), new DimensionJsonConverter(), }, }; string serializedJsonString = JsonSerializer.Serialize(spinlockSearchSpace, jsonSerializerOptions); Assert.Equal(originalValidSimpleHypergridJsonString, serializedJsonString); string yetAnotherSerializedJsonString = spinlockSearchSpace.ToJson(); Assert.Equal(originalValidSimpleHypergridJsonString, yetAnotherSerializedJsonString); }
public TestSerializingAndDeserializing() { string pathToVirtualEnv = Environment.GetEnvironmentVariable("PYTHONHOME"); if (string.IsNullOrEmpty(pathToVirtualEnv)) { pathToVirtualEnv = @"c:\ProgramData\Anaconda3"; } else { Environment.SetEnvironmentVariable("PYTHONHOME", pathToVirtualEnv, EnvironmentVariableTarget.Process); } string pathToPythonPkg = $"{pathToVirtualEnv}\\pkgs\\python-3.7.4-h5263a28_0"; Environment.SetEnvironmentVariable("PATH", $"{pathToVirtualEnv};{pathToPythonPkg}", EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("PYTHONPATH", $"{pathToVirtualEnv}\\Lib\\site-packages;{pathToVirtualEnv}\\Lib", EnvironmentVariableTarget.Process); continuous = new ContinuousDimension(name: "continuous", min: 1, max: 10); discrete = new DiscreteDimension(name: "discrete", min: 1, max: 10); ordinal = new OrdinalDimension(name: "ordinal", orderedValues: new List <object>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, ascending: true); categorical = new CategoricalDimension(name: "categorical", values: new List <object>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); allKindsOfDimensions = new SimpleHypergrid( name: "all_kinds_of_dimensions", dimensions: new List <IDimension>() { continuous, discrete, ordinal, categorical, }); }
public void TestPythonInterop() { var jsonSerializerOptions = new JsonSerializerOptions { WriteIndented = true, Converters = { new JsonStringEnumConverter(), new SimpleHypergridJsonConverter(), new DimensionJsonConverter(), }, }; string csContinuousJsonString = JsonSerializer.Serialize(continuous, jsonSerializerOptions); string csDiscreteJsonString = JsonSerializer.Serialize(discrete, jsonSerializerOptions); string csOrdinalJsonString = JsonSerializer.Serialize(ordinal, jsonSerializerOptions); string csCategoricalJsonString = JsonSerializer.Serialize(categorical, jsonSerializerOptions); string csSimpleHypergridJsonString = JsonSerializer.Serialize(allKindsOfDimensions, jsonSerializerOptions); using (Py.GIL()) { using PyScope pythonScope = Py.CreateScope(); pythonScope.Set("cs_continuous_dimension_json_string", csContinuousJsonString); pythonScope.Set("cs_discrete_dimension_json_string", csDiscreteJsonString); pythonScope.Set("cs_ordinal_dimension_json_string", csOrdinalJsonString); pythonScope.Set("cs_categorical_dimension_json_string", csCategoricalJsonString); pythonScope.Set("cs_simple_hypergrid_json_string", csSimpleHypergridJsonString); pythonScope.Exec(PythonScriptsAndJsons.CreateDimensionsAndSpacesScript); pythonScope.Exec(PythonScriptsAndJsons.DeserializeDimensionsScript); bool successfullyDeserializedDimensions = pythonScope.Get("success").As <bool>(); string exceptionMessage = string.Empty; if (!successfullyDeserializedDimensions) { exceptionMessage = pythonScope.Get("exception_message").As <string>(); } Assert.True(successfullyDeserializedDimensions, exceptionMessage); pythonScope.Exec(PythonScriptsAndJsons.DeserializeSimpleHypergridScript); bool successfullyDeserializedSimpleHypergrid = pythonScope.Get("success").As <bool>(); if (!successfullyDeserializedSimpleHypergrid) { exceptionMessage = pythonScope.Get("exception_message").As <string>(); } Assert.True(successfullyDeserializedSimpleHypergrid, exceptionMessage); string pySimpleHypergridJsonString = pythonScope.Get("py_simple_hypergrid_json_string").As <string>(); SimpleHypergrid simpleHypergridDeserializedFromPython = JsonSerializer.Deserialize <SimpleHypergrid>(pySimpleHypergridJsonString, jsonSerializerOptions); Assert.True(simpleHypergridDeserializedFromPython.Name == "all_kinds_of_dimensions"); Assert.True(simpleHypergridDeserializedFromPython.Dimensions.Count == 4); string reserializedHypergrid = JsonSerializer.Serialize(simpleHypergridDeserializedFromPython, jsonSerializerOptions); pythonScope.Set("cs_reserialized_hypergrid_json_string", reserializedHypergrid); pythonScope.Exec(PythonScriptsAndJsons.ValidateReserializedHypergridScript); bool successfullyValidatedReserializedHypergrid = pythonScope.Get("success").As <bool>(); if (!successfullyValidatedReserializedHypergrid) { exceptionMessage = pythonScope.Get("exception_message").As <string>(); } Assert.True(successfullyValidatedReserializedHypergrid, exceptionMessage); } string currentDirectory = Directory.GetCurrentDirectory(); Console.WriteLine($"Current directory {currentDirectory}"); }