Imagine I have .mzn file called abc.mzn and it is as follows.
array[1..3] of int:a;
output[show(a)];
Now I have a .dzn file called cde.dzn and it is as follows.
a=[1,2,3];
I will run minizinc python package as below,
import minizinc as minizinc
from minizinc import Instance,Model,Solver
x=Solver.lookup("geocode")
M1=Model("./abc.mzn")
instance1=Instance(x,M1)
instance1("a")=[1,2,3]
result = instance1.solve()
print(result)
Above code works fine and no problem with that.I am keen to use dzn module in this python code instead of Instance module and to get rid of manually assigning below line.
As you can see we need to manually assign the values for all parameters using instance1=..
instance1("a")=[1,2,3]
Is there any way that we can use .dzn file to assign values(using dzn module).I noted that in the package itself we have the dzn module already there.
can we do in below manner or how to get results.
import minizinc as minizinc
from minizinc import dzn,Model,Solver
M1=Model("./abc.mzn")
D1=dzn("./cde.dzn") etc..
The DZN module in MiniZinc Python is meant to be used through the .add_file method of Instance/Model. Using this method you can add data files (.dzn/.json) or additional model files .mzn to your MiniZinc model or instance.
So for your example would become:
from minizinc import Model
M1 = Model("./abc.mzn")
M1.add_file("./cde.dzn")
Related
I am trying to run a parametric sweep in OpenModelica using OMPython. Let's assume that I have a Modelica model my_model.mo belonging to the library my_library. The model has two parameters: a and b.
I successfully managed to run a single parametric run by using the following code:
from OMPython import OMCSessionZMQ
omc = OMCSessionZMQ()
omc.sendExpression('loadModel(my_library)')
omc.sendExpression('simulate(my_library.my_model, simflags="-overrideFile=parameter_sweep.txt", stopTime=86400)')
where the file parameter_sweep.txt is:
a=5
b=6
Now the question is: how can I run multiple parametric runs? I could add one more line to the code where a new txt file (parameter_sweep1.txt) with a new set of values for the parameters is used:
from OMPython import OMCSessionZMQ
omc = OMCSessionZMQ()
omc.sendExpression('loadModel(my_library)')
omc.sendExpression('simulate(my_library.my_model, simflags="-overrideFile=parameter_sweep.txt", stopTime=86400)')
omc.sendExpression('simulate(my_library.my_model, simflags="-overrideFile=parameter_sweep1.txt", stopTime=86400)')
However, I am afraid that in this way there is the need to recompile. Is there a way to do multiple parametric runs and avoid re-compilation?
Use the buildModel command instead of simulate Then start the process manually in Python using a library such as subprocess. The command is simply something like:
["./my_library.my_model", "-overrideFile=parameter_sweep.txt"]
(If you use Windows, I believe you need to update your PATH environment variable as well, in order to find the used DLLs. If you use Linux, it just works.)
I would like to save to a file multiple instances of a custom Python class.
The class is Loess, taken from https://github.com/joaofig/pyloess, which performs localised regression.
Below's a MWE of the saving process:
import pickle
import numpy as np
from Loess import Loess
xarr = np.linspace(0, 4, 100) * np.pi
yarr = 2*np.sin(xarr) + np.random.rand(len(xarr))
loess = Loess(xarr, yarr)
with open("localised_regression.pkl", "wb") as output:
pickle.dump(loess, output)
and now the retrieval process
import pickle
with open("localised_regression.pkl", 'rb') as input_:
localised_regression = pickle.load(input_)
When I do this on a jupyter notebook (run the first snippet on one notebook and the second on another) it works perfectly.
Dumping the instance of Loess from a notebook and retrieving it from terminal or another machine, it doesn't work.
I get ModuleNotFoundError: No module named 'Loess' error message
I even tried importing the module in python session where I attempt the retrieval, but nothing changes.
It seems that it only works from within the same location where the dumping was performed.
I'm using Python 3.7.7 and the same conda environment for both Python shell and jupyter notebook.
I examined other answers (like how to save/read class wholly in Python) but no luck.
I've tried saving to numpy file, but same story.
I've also tried dumping with marshal and json, but both complained.
Does anybody have a suggestion on how to solve this? Thank you
I'm currently using a python module called petsc4py (https://pypi.org/project/petsc4py/). My main issue is that none of the typical intellisense features seems to work with this module.
I'm guessing it might have something to do with it being a C extension module, but I am not sure exactly why this happens. I initially thought that intellisense was unable to look inside ".so" files, but it seems that numpy is able to do this with the array object, which in my case is inside a file called multiarray.cpython-37m-x86_64-linux-gnu (check example below).
Does anyone know why I see this behaviour in the petsc4py module. Is there anything that I (or the developers of petsc4py) can do to get intellisense to work?
Example:
import sys
import petsc4py
petsc4py.init(sys.argv)
from petsc4py import PETSc
x_p = PETSc.Vec().create()
x_p.setSizes(10)
x_p.setFromOptions()
u_p = x_p.duplicate()
import numpy as np
x_n = np.array([1,2,3])
u_n = x_n.copy()
In this example, when trying to work with a Vec object from petsc4py, doing u_p.duplicate() cannot find the function and the suggestion is simply a repetition of the function immediately before. However, using an array from numpy, doing u_n.copy() works perfectly.
If you're compiling in-place then you're bumping up against https://github.com/microsoft/python-language-server/issues/197.
I am trying to generate a feature set with the Essentia MusicExtractor from a yaml profile as described in the documentation here and here via python.
My code snippet:
from essentia.standard import MusicExtractor
profile = "some_profile.yaml"
audio = "some_audio.mp3"
features, frames = MusicExtractor(profile=profile)(audio)
My yaml profile:
This produces the folling error:
RuntimeError:
Error while configuring MusicExtractor:
Pool: Cannot set/add/merge value to the pool under the name 'rhythm.stats'
because that name already exists but contains a different data type than value.
It does not really look that i am doing something wrong.
I ran into the same problem and fixed it this way:
Downloaded a sample profile from the essentia repos examples.
Ran the profile.
Commented out the conflicting lines after each run, which are just a few. Basically the stats and statsMFCC lines.
From this I could derive a working profile.
I've found many topics related to this on the Internet but I could find no solutions.
Suppose I want to download any PMML model from this examples list, and run them in Python (Python 3 preferably). Is there any way to do this?
I'm looking for a way to import a PMML that was deployed OUTSIDE Python and import it to use it with this language.
You could use PyPMML to apply PMML in Python, for example:
from pypmml import Model
model = Model.fromFile('DecisionTreeIris.pmml')
result = model.predict({
"Sepal_Length" : 5.1,
"Sepal_Width" : 3.5,
"Petal_Length" : 1.4,
"Petal_Width" : 0.2
})
For more info about other PMML libraries, be free to see:
https://github.com/autodeployai
After some research I found the solution to this: the 'openscoring' library.
Using it is very simple:
import subprocess
from openscoring import Openscoring
import numpy as np
p = subprocess.Popen('java -jar openscoring-server-executable-1.4.3.jar',
shell=True)
os = Openscoring("http://localhost:8080/openscoring")
# Deploying a PMML document DecisionTreeIris.pmml as an Iris model:
os.deployFile("Iris", "DecisionTreeIris.pmml")
# Evaluating the Iris model with a data record:
arguments = {
"Sepal_Length" : 5.1,
"Sepal_Width" : 3.5,
"Petal_Length" : 1.4,
"Petal_Width" : 0.2
}
result = os.evaluate("Iris", arguments)
print(result)
This returns the value of the target variable in a dictionary. There is no need to go outside of Python to use PMML models anymore (you just have to run the server with Java, which can be done with Python as well as I showed above).
Isn't it like trying to host H2O models in python apps? Looks like a bridge from python to Java is required here too. Such bridges are not stable at all, been there, tested them. Just a general suggestion: do not mix languages between ML algos and apps code, train in python, serve in python, re-validate also in python. Legacy R and H2O models can be always re-fitted in python.