Python scripting with OVITO - python

I have a quick question regarding my python code. I am using a tool called OVITO, which works quite nicely when I run in the GUI. I am trying to run it using the python scripting interface, but I am not wanting to get the results that I want.
The code should create a list of files with a single number. However, the number for each file should change. I am calculating a specific property for my material (vacancies), and I know vacancies are created, but my code says 0 for all my files. Can someone tell me if I am overwriting somehow or if something is obviously wrong? Again, I do not expect anyone here to know OVITO, but just with the python part, curious if something is clearly wrong.
Import OVITO modules.
from ovito.io import *
from ovito.modifiers import *
# Import NumPy module.
import numpy
import sys
node = import_file("../cascade.dump",multiple_frames = True)
for i in range(node.source.num_frames):
with open("{}.out".format(i),'w') as f:
mod = WignerSeitzAnalysisModifier(per_type_occupancies = True)
mod.reference.load("../../../../../../STP/position_perfect_300.dump")
node.modifiers.append(mod)
node.compute()
node.modifiers.append(SelectExpressionModifier(expression = 'Occupancy.1==0&&Occupancy.2==0 && ParticleType==1'))
node.compute()
f.write("%i\n" % numpy.count_nonzero(node.output.particle_properties['Selection']))
f.close()

It's a very old post, but to whom it can help the solution is just to give the frame to compute in node.compute().
Import OVITO modules.
from ovito.io import *
from ovito.modifiers import *
# Import NumPy module.
import numpy
import sys
node = import_file("../cascade.dump",multiple_frames = True)
for i in range(node.source.num_frames):
with open("{}.out".format(i),'w') as f:
mod = WignerSeitzAnalysisModifier(per_type_occupancies = True)
mod.reference.load("../../../../../../STP/position_perfect_300.dump")
node.modifiers.append(mod)
node.compute(i)
node.modifiers.append(SelectExpressionModifier(expression = 'Occupancy.1==0&&Occupancy.2==0 && ParticleType==1'))
node.compute(i)
f.write("%i\n" % numpy.count_nonzero(node.output.particle_properties['Selection']))
f.close()

Related

Why does a python function work in parallel even if it should not?

I am running this code using the healpy package. I am not using multiprocessing and I need it to run on a single core. It worked for a certain amount of time, but, when I run it now, the function healpy.projector.GnomonicProj.projmap takes all the available cores.
This is the incriminated code block:
def Stacking () :
f = lambda x,y,z: pixelfunc.vec2pix(xsize,x,y,z,nest=False)
map_array = pixelfunc.ma_to_array(data)
im = np.zeros((xsize, xsize))
plt.figure()
for i in range (nvoids) :
sys.stdout.write("\r" + str(i+1) + "/" + str(nvoids))
sys.stdout.flush()
proj = hp.projector.GnomonicProj(rot=[rav[i],decv[i]], xsize=xsize, reso=2*nRad*rad_deg[i]*60/(xsize))
im += proj.projmap(map_array, f)
im/=nvoids
plt.imshow(im)
plt.colorbar()
plt.title(title + " (Map)")
plt.savefig("../Plots/stackedMap_"+name+".png")
return im
Does someone know why this function is running in parallel? And most important, does someone know a way to run it in a single core?
Thank you!
In this thread they recommend to set the environment variable OMP_NUM_THREADS accordingly:
Worked with:
import os
os.environ['OMP_NUM_THREADS'] = '1'
import healpy as hp
import numpy as np
os.environ['OMP_NUM_THREADS'] = '1' have to be done before import numpy and healpy libraries.
As to the why: probably they use some parallelization techniques wrapped within their implementation of the functions you use. According to the name of the variable, I would guess OpenMP it is.

Can I import modules in python using a function

I am working on a small library and I need to know can I import modules like numpy, sklearn and etc. Using functions. For example:
def ml():
import numpy as np
import pandas as pd
x = np.array([1,2,647,345,3,7,3,8,36,64])
Is this possible ?
Simply can I import a module using a function and then use that later outside the function
The main idea is when the user calls the function ml he has all the modules related to machine learning imported and then he can use them. X = np.array was just kind of an example.
UPDATED
This should work
import importlib
def importmd(modulex):
return importlib.import_module(modulex) #Returning the module
np = importmd("numpy") #Same as import numpy as np

How to do a combined boolean operation in neptune using gremlin and python?

I am trying to do a query where I have multiple boolean operations to be done but can't figure out how to do it.
The query is something like (A and B) or (C and D)
I first tried
g.V()\
.has("attra", P.lte(20))\
.has("attrb", P.gte(10))\
.or_()\
.has("attrc", P.lte(20))\
.has("attrd", P.gte(10))
but it turns out anything after the or_() in the query is ored and that is not what I want. Because I have other complex boolean logic down the line as well.
and I also tried
g.V()\
.or_(
has("attra", P.lte(20)).and().has("attrb", P.gte(10)),
has("attrc", P.lte(20)).and().has("attrd", P.gte(10))
)
but it says that has is not defined. Is this how you do it? Where is this has even defined?
Help would be really appreciated
EDIT: I have the following imports in my file
from __future__ import print_function
from gremlin_python.structure.graph import Graph
from gremlin_python.process.strategies import *
from gremlin_python.process.traversal import *
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
from gremlin_python.process.graph_traversal import __ as AnonymousTraversal
I found where the has is defined.
It can be found here
from gremlin_python.process.graph_traversal import __ as AnonymousTraversal
so we just do
has = AnonymousTraversal.has
and query like this
g.V()\
.or_(
has("attra", P.lte(20)).and().has("attrb", P.gte(10)),
has("attrc", P.lte(20)).and().has("attrd", P.gte(10))
)

how can I import "Dense3D" and use it in code?

I tried to run a code from git but i found a problem with importing Dense3D.
here the part of code witch relate to the problem:
here the part of code that relate to the problem:
import torch
from torch.models import Dense3D
import torch.nn as nn
#some lines deleted
print("Loading options...")
with open(sys.argv[1], 'r') as optionsFile:
options = toml.loads(optionsFile.read())
#Create the model.
model = Dense3D(options)
it make error that "can't import Dense3D" or something like this in another tries with some changes.
and i used colab .

Pyopencl array sum to add an array

I am new to opencl and pyopencl. I am trying to write a basic program to add. I came across this documentation and tried this small code in python. Obviously, it is not working.
import pyopencl as cl
import pyopencl.tools
import pyopencl.array
import numpy
context = cl.create_some_context()
queue = cl.CommandQueue(context)
h_a = numpy.random.rand(3,3)
d_a = cl.Buffer(context, cl.mem_flags.READ_ONLY |
cl.mem_flags.COPY_HOST_PTR, hostbuf=h_a)
print cl.array.sum(d_a, dtype=None, queue=queue)
As you can assess, I am not sure about how to use those predefined functions.
My PyOpenCl tutorial has an array sum example with inline comments explaining what each line does: https://github.com/benshope/PyOpenCL-Tutorial
Try running that, I hope it is helpful!

Categories