class Add(MathNode): Input('F1', float) Input('F2', float) Output('Sum', float) def run(self): self._Sum(self._F1 + self._F2)
class CreateVector(MathNode): Input('X', float) Input('Y', float) Input('Z', float) Output('V', float, list=True) def run(self): self._Output(self._X, self._Y, self._Z)
class RotateAbout(VectorNode): Input('Point', float, list=True) Input('PointOnAxis', float, list=True) Input('AxisDirection', float, list=True) Input('Degree', float) Output('RotatedPoint', float, list=True) def run(self): super(RotateAbout, self).run() point = self._Point angle = self._Degree axisDirection = self._AxisDirection axisOrigin = self._PointOnAxis t = angle * (pi / 180) x, y, z = point[0], point[1], point[2] a, b, c = axisOrigin[0], axisOrigin[1], axisOrigin[2] axisDirection /= norm(axisDirection) u, v, w = axisDirection[0], axisDirection[1], axisDirection[2] xx = (a * (v**2 + w**2) - u * (b * v + c * w - u * x - v * y - w * z)) * ( 1 - cos(t)) + x * cos(t) + (-1 * c * v + b * w - w * y + v * z) * sin(t) yy = (b * (u**2 + w**2) - v * (a * u + c * w - u * x - v * y - w * z)) * ( 1 - cos(t)) + y * cos(t) + (1 * c * u - a * w + w * x - u * z) * sin(t) zz = (c * (u**2 + v**2) - w * (a * u + b * v - u * x - v * y - w * z)) * ( 1 - cos(t)) + z * cos(t) + (-1 * b * u + a * v - v * x + u * y) * sin(t) self._RotatedPoint([xx, yy, zz])
class Frac2Cart(CrystNode): Input('Position', float, list=True) Input('Cell', float, list=True) Output('Cart', float, list=True) def run(self): super(Frac2Cart, self).run() self._Cart(frac2cart(self._Position, self._Cell))
class SelectAtom(CrystNode): Input('AtomList', Atom, list=True) Input('AtomName', str) Output('Atom', Atom) def run(self): super(SelectAtom, self).run() name = self._AtomName self._Atom([atom for atom in self._AtomList if atom.get_name() == name][0])
class DotProduct(VectorNode): Input('Vector1', float, list=True) Input('Vector2', float, list=True) Output('DotProduct', float, list=True) def run(self): super(DotProduct, self).run() v1 = self._Vector1 v2 = self._Vector2 self._DotProduct(v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2])
class AMyNode(Node): Input('Inta', int) Input('Intb', int) Input('Int1', int, select=[1,2,3,4]) Input('Int3', int, select=[1,2,3,4]) Output('Int2', int) def run(self): self._Int1 self._Int2(self._Int1 + 1)
class Difference(VectorNode): Input('Vector1', float, list=True) Input('Vector2', float, list=True) Output('Difference', float, list=True) def run(self): super(Difference, self).run() v1 = self._Position1 v2 = self._Position2 self._Difference((v1[0] - v2[0]), (v1[1] - v2[1]), (v1[2] - v2[2]))
class VectorSum(VectorNode): Input('Vector1', float, list=True) Input('Vector2', float, list=True) Output('Sum', float, list=True) def run(self): super(VectorSum, self).run() v1 = self._Position1 v2 = self._Position2 self._Difference((v1[0] + v2[0]), (v1[1] + v2[1]), (v1[2] + v2[2]))
class CrossProduct(VectorNode): Input('Vector1', float, list=True) Input('Vector2', float, list=True) Output('XProduct', float, list=True) def run(self): super(CrossProduct, self).run() v1 = self._Vector1 v2 = self._Vector2 self._XProduct(v1[1]*v2[2]-v1[2]*v2[1], v1[2]*v2[0]-v1[0]*v2[2], v1[0]*v2[1]-v1[1]*v2[0])
class Distance(VectorNode): Input('Position1', float, list=True) Input('Position2', float, list=True) Output('Distance', float, ) def run(self): super(Distance, self).run() v1 = self._Position1 v2 = self._Position2 d = (v1[0]-v2[0])**2 + (v1[1]-v2[1])**2 + (v1[2]-v2[2])**2 self._Distance(d**.5)
class DotProduct(VectorNode): """ Compute Dot product of two input Vectors. """ Input('Vector1', float, list=True) Input('Vector2', float, list=True) Output('DotProduct', float, list=False) def run(self): super(DotProduct, self).run() v1 = self._Vector1 v2 = self._Vector2 self._DotProduct(v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2])
class MyNode(Node): Input('Int1', int) Output('Int2', int) def run(self): self._Int1 self._Int2(self._Int1 + 1)
class BreakAtom(CrystNode): Input('Atom', Atom) Output('Name', str) Output('Element', str) Output('frac', float, list=True) Output('cart', float, list=True) Output('ADP',float, list=True) Output('ADP_Flag', str) Output('Cell',float, list=True) def run(self): super(BreakAtom, self).run() atom = self._Atom # print(atom, atom.molecule.get_cell(degree=True)) self._Name(atom.get_name()) self._Element(atom.get_element()) self._frac(atom.get_frac()) self._cart(atom.get_cart()) try: adp = atom.adp['cart_meas'] except ADPDataError: adp = [0, 0, 0, 0, 0, 0] self._ADP(adp) self._ADP_Flag(atom.adp['flag']) self._Cell(atom.molecule.get_cell(degree=True))
class PlotNode2(Node): Input('XX', str) Output('YY', str) def __init__(self, *args, **kwargs): super(PlotNode2, self).__init__(*args, **kwargs) self.time = time.time() self.points = [] self.counts = 0 def check(self): t = time.time() if t - self.time > 3: self.time = t return True def run(self): super(PlotNode2, self).run() self.counts += 1 self.points.append( (self.counts, (random.randint(5, 20), random.randint(5, 20), random.randint(5, 20), random.randint(5, 20)))) def report(self): r = super(PlotNode2, self).report() r['template'] = 'PlotTemplate' r['points'] = self.points[:] r['keep'] = 'points' self.points = [] return r
class FakeWorkNode(Node): Input('inp', object) Output('out', object) def run(self): print('Working @ {}'.format(str(self._inp))) time.sleep(random.randrange(1,5)) print('Done')
class PairedLinePlot(PlotNode): Input('A', float) Input('B', float) Output('Trigger', object) def setup(self): self.points = [] def run(self): super(PairedLinePlot, self).run() self.points.append((None, (self._A, self._B))) def report(self): r = super(PairedLinePlot, self).report() r['template'] = 'PlotTemplate' r['points'] = self.points[:] self.points = [] return r
class Normalize(VectorNode): Input('Vector', float, list=True) Output('NVector', float, list=True) def run(self): super(Normalize, self).run() # v = self._Vector # d = (v[0]**2 + v[1]**2 + v[2]**2)**.5 # self._NVector((v[0]/d, v[1]/d, v[2]/d)) self._NVector(norm(self._Vector))
class PlotBarsGrouped(PlotNode): Input('A', float) Input('B', float) Output('Trigger', object) def __init__(self, *args, **kwargs): super(PlotBarsGrouped, self).__init__(*args, **kwargs) self.data = [] def run(self): super(PlotBarsGrouped, self).run() self.data.append((self._A, self._B)) def report(self): r = super(PlotBarsGrouped, self).report() r['template'] = 'plotBarsGroupedTemplate' r['points'] = self.data[:] # r['keep'] = 'points' # self.data = [] return r
class RunProgram(Node): """ Node for calling an external program of given name and given command line arguments. Returns the program's return value and its stdout output. """ Input('ProgramName', str) Input('Arguments', str) Output('ReturnValue', int) Output('StdOut', str) def run(self): programName = self._ProgramName args = [programName] + self._Arguments.split() r = 0 try: out = subprocess.check_output(args, shell=True) except subprocess.CalledProcessError as e: out = '' r = e[-1] self._ReturnValue(r) self._StdOut(out)
class ReadAtoms(CrystNode): Input('FileName', str) Output('Atoms', Atom, list=True) def run(self): super(ReadAtoms, self).run() from lauescript.laueio.loader import Loader loader = Loader() loader.create(self._FileName) print('1') mol = loader.load('quickloadedMolecule') print('2') self._Atoms(mol.atoms)
class RunShelxl(ShelxNode): Input('INS', str) Input('HKL', str) Input('List', int, optional=True) Input('Cycles', int) Input('DAMP', int) Input('Type', str, select=('CGLS', 'L.S.')) Output('RES', str) Output('LST', str) Output('FCF', str) Output('R1', float) def __init__(self, *args, **kwargs): super(RunShelxl, self).__init__(*args, **kwargs) self.p = None self.stdout = '' def run(self): super(RunShelxl, self).run() with open('__tmp__.ins', 'w') as fp: fp.write(self._INS) with open('__tmp__.hkl', 'w') as fp: fp.write(self._HKL) self.p = subprocess.Popen('shelxl {}'.format('__tmp__'), shell=True, stdout=subprocess.PIPE) while True: line = self.p.stdout.readline() if not line: break self.stdout += str(line)[1:] #os.waitpid(self.p.pid, 0) output = '' with open('__tmp__.res', 'r') as fp: output = fp.read() self._RES(output) with open('__tmp__.lst', 'r') as fp: output = fp.read() for line in output.splitlines(): if line.startswith(' R1 ='): line = [i for i in line.split() if i] R1 = float(line[2]) break self._R1(R1) self._LST(output) with open('__tmp__.fcf', 'r') as fp: output = fp.read() self._FCF(output) for file in os.listdir(): if file.startswith('__tmp__'): os.remove(file) def report(self): r = super(RunShelxl, self).report() r['template'] = 'ProgramTemplate' r['stdout'] = self.stdout return r
class BreakPDB(CrystNode): Input('PDB', str) Output('Code', str) Output('R1', float) def run(self): for line in self._PDB.splitlines(): if line.startswith('REMARK 3 R VALUE') and '(WORKING SET)' in line: line = [i for i in line[:-1].split() if i] r1 = line[-1] elif line.startswith('HEADER'): line = [i for i in line[:-1].split() if i] code = line[-1] self._Code(code) self._R1(r1)
class ForEachAtomPair(ForLoop): Input('Start', Atom, list=True) Output('Atom1', Atom) Output('Atom2', Atom) # def __init__(self, *args, **kwargs): # super(ForEachAtomPair, self).__init__(*args, **kwargs) def run(self): atoms = self._Start if self.fresh: self.x = 0 self.y = 1 self.end = len(atoms)-1 self.fresh = False self._Atom1(atoms[self.x]) self._Atom2(atoms[self.y]) self.y += 1 if self.y >= self.end: self.x += 1 self.y = self.x+1 if self.x >= self.end: self._Final(self._Start) self.done = True
class Int2Str(Node): Input('Int', int) Output('Str', str) def run(self): self._Str(str(self._Int))
class Range(Node): Input('EndValue', int) Output('ValueList', int, list=True) def run(self): self._ValueList(list(range(self._EndValue)))
class PDB2INS(CrystNode): Input('FileName', str) Input('Wavelength', float) Input('HKLF', int) Input('CELL', str) Input('SpaceGroup', str) Input('ANIS', bool) Input('MakeHKL', bool) Input('REDO', bool) Input('Z', int) Output('INS', str) Output('HKL', str) Output('PDB', str) def __init__(self, *args, **kwargs): super(PDB2INS, self).__init__(*args, **kwargs) self.stdout = '' def check(self): x = self.inputs['FileName'].isAvailable() return x def run(self): super(PDB2INS, self).run() opt = ('pdb2ins', self._FileName, '-i', '-o __pdb2ins__.ins', ' -w '+str(self._Wavelength) if self._Wavelength else '', ' -h '+str(self._HKLF) if self._HKLF else '', ' -c '+str(self._CELL) if self._CELL else '', ' -s '+str(self._SpaceGroup) if self._SpaceGroup else '', ' -a ' if self._ANIS else '-a', ' -b ' if self._MakeHKL else '-b', ' -r ' if self._REDO else '', ' -z ' + str(self._Z) if self._Z else '', (' -d '+ self._FileName+'.sf') if not '@' in self._FileName else '') opt = ' '.join(opt) print(opt) # opt = [o for o in ' '.join(opt).split(' ') if o] # print(opt) self.p = subprocess.Popen(opt, shell=True, stdout=subprocess.PIPE) self.stdout = '' while True: line = self.p.stdout.readline() if not line: break self.stdout += str(line)[1:] # print('ran') self._INS(open('__pdb2ins__.ins', 'r').read()) try: self._HKL(open('__pdb2ins__.hkl', 'r').read()) except IOError: try: self._HKL(open('{}.hkl'.format(self._FileName), 'r').read()) except IOError: self._HKL('') try: self._PDB(open('__pdb2ins__.pdb', 'r').read()) except IOError: self._PDB(open('{}.pdb'.format(self._FileName), 'r').read()) for file in os.listdir(): if file.startswith('__pdb2ins__'): os.remove(file) def report(self): r = super(PDB2INS, self).report() r['stdout'] = self.stdout r['template'] = 'ProgramTemplate' return r