def parse(self, opts, args): if opts.tag: self.tag = opts.tag if opts.magnetic_moment: self.magmoms = np.array( [float(m) for m in opts.magnetic_moment.split(',')]) self.gui = opts.gui self.write_summary = opts.write_summary self.write_to_file = opts.write_to_file self.use_lock_files = opts.use_lock_files self.interactive_python_session = opts.interactive_python_session self.contains = opts.contains self.modify = opts.modify if self.modify is not None: try: assert not isinstance(eval(self.modify), str), \ '--modify option received a string argument. Check quoting.' except SyntaxError: pass self.after = opts.after self.clean = opts.clean if opts.slice: self.slice = string2index(opts.slice)
def write(self, filename, rotations='', show_unit_cell=False, bbox=None, **kwargs): indices = range(self.nimages) p = filename.rfind('@') if p != -1: try: slice = string2index(filename[p + 1:]) except ValueError: pass else: indices = indices[slice] filename = filename[:p] if isinstance(indices, int): indices = [indices] images = [self.get_atoms(i) for i in indices] if len(filename) > 4 and filename[-4:] in ['.eps', '.png', '.pov']: write(filename, images, rotation=rotations, show_unit_cell=show_unit_cell, bbox=bbox, **kwargs) else: write(filename, images, **kwargs)
def run(opt, args): images = Images() if opt.aneb: opt.image_number = '-1' if len(args) > 0: from ase.io import string2index try: images.read(args, string2index(opt.image_number)) except IOError as e: if len(e.args) == 1: parser.error(e.args[0]) else: parser.error(e.args[1] + ': ' + e.filename) else: images.initialize([Atoms()]) if opt.interpolate: images.interpolate(opt.interpolate) if opt.aneb: images.aneb() if opt.repeat != '1': r = opt.repeat.split(',') if len(r) == 1: r = 3 * r images.repeat_images([int(c) for c in r]) if opt.radii_scale: images.set_radii(opt.radii_scale) if opt.output is not None: images.write(opt.output, rotations=opt.rotations, show_unit_cell=opt.show_unit_cell) opt.terminal = True if opt.terminal: if opt.graph is not None: data = images.graph(opt.graph) for line in data.T: for x in line: print(x, end=' ') print() else: from ase.gui.gui import GUI import ase.gui.gtkexcepthook ase gui = GUI(images, opt.rotations, opt.show_unit_cell, opt.bonds) gui.run(opt.graph)
def read(self, filenames, default_index=':', filetype=None): from ase.utils import basestring if isinstance(default_index, basestring): default_index = string2index(default_index) images = [] names = [] for filename in filenames: from ase.io.formats import parse_filename if '.json@' in filename or '.db@' in filename: # Ugh! How would one deal with this? # The parse_filename and string2index somehow conspire # to cause an error. See parse_filename # in ase.io.formats for this particular # special case. -askhl # # TODO Someone figure out how to see what header # a JSON file should have. imgs = read(filename, default_index, filetype) if hasattr(imgs, 'iterimages'): imgs = list(imgs.iterimages()) names += [filename] * len(imgs) images += imgs continue # Argh! if '@' in filename: actual_filename, index = parse_filename(filename, None) else: actual_filename, index = parse_filename( filename, default_index) imgs = read(filename, default_index, filetype) if hasattr(imgs, 'iterimages'): imgs = list(imgs.iterimages()) images.extend(imgs) # Name each file as filename@index: if isinstance(index, slice): start = index.start or 0 step = index.step or 1 else: start = index # index is just an integer assert len(imgs) == 1 step = 1 for i, img in enumerate(imgs): names.append('{}@{}'.format(actual_filename, start + i * step)) self.initialize(images, names)
def run(opt, args): images = Images() if opt.aneb: opt.image_number = '-1' if len(args) > 0: from ase.io import string2index try: images.read(args, string2index(opt.image_number)) except IOError, e: if len(e.args) == 1: parser.error(e.args[0]) else: parser.error(e.args[1] + ': ' + e.filename)
def read(self, filenames, default_index=':', filetype=None): from ase.utils import basestring if isinstance(default_index, basestring): default_index = string2index(default_index) images = [] names = [] for filename in filenames: from ase.io.formats import parse_filename if '@' in filename and 'postgres' not in filename or \ 'postgres' in filename and filename.count('@') == 2: actual_filename, index = parse_filename(filename, None) else: actual_filename, index = parse_filename( filename, default_index) # Read from stdin: if filename == '-': import sys from io import BytesIO buf = BytesIO(sys.stdin.buffer.read()) buf.seek(0) filename = buf filetype = 'traj' imgs = read(filename, index, filetype) if hasattr(imgs, 'iterimages'): imgs = list(imgs.iterimages()) images.extend(imgs) # Name each file as filename@index: if isinstance(index, slice): start = index.start or 0 step = index.step or 1 else: start = index step = 1 for i, img in enumerate(imgs): if isinstance(start, int): names.append('{}@{}'.format(actual_filename, start + i * step)) else: names.append('{}@{}'.format(actual_filename, start)) self.initialize(images, names)
def parse(self, opts, args): if opts.tag: self.tag = opts.tag if opts.magnetic_moment: self.magmoms = np.array([float(m) for m in opts.magnetic_moment.split(",")]) self.gui = opts.gui self.write_summary = opts.write_summary self.write_to_file = opts.write_to_file self.use_lock_files = opts.use_lock_files self.interactive_python_session = opts.interactive_python_session self.contains = opts.contains self.modify = opts.modify if opts.slice: self.slice = string2index(opts.slice)
def run(opt, args): images = Images() if opt.aneb: opt.image_number = '-1' if len(args) > 0: from ase.io import string2index images.read(args, string2index(opt.image_number)) else: images.initialize([Atoms()]) if opt.interpolate: images.interpolate(opt.interpolate) if opt.aneb: images.aneb() if opt.repeat != '1': r = opt.repeat.split(',') if len(r) == 1: r = 3 * r images.repeat_images([int(c) for c in r]) if opt.radii_scale: images.set_radii(opt.radii_scale) if opt.output is not None: images.write(opt.output, rotations=opt.rotations, show_unit_cell=opt.show_unit_cell) opt.terminal = True if opt.terminal: if opt.graph is not None: data = images.graph(opt.graph) for line in data.T: for x in line: print(x, end=' ') print() else: from ase.gui.gui import GUI import ase.gui.gtkexcepthook ase gui = GUI(images, opt.rotations, opt.show_unit_cell, opt.bonds) gui.run(opt.graph)
def write(self, filename, rotations='', show_unit_cell=False, bbox=None): indices = range(self.nimages) p = filename.rfind('@') if p != -1: try: slice = string2index(filename[p + 1:]) except ValueError: pass else: indices = indices[slice] filename = filename[:p] if isinstance(indices, int): indices = [indices] images = [self.get_atoms(i) for i in indices] write(filename, images, rotation=rotations, show_unit_cell=show_unit_cell, bbox=bbox)
def parse(self, opts, args): if opts.tag: self.tag = opts.tag if opts.magnetic_moment: self.magmoms = np.array( [float(m) for m in opts.magnetic_moment.split(',')]) self.gui = opts.gui self.write_summary = opts.write_summary self.write_to_file = opts.write_to_file self.use_lock_files = opts.use_lock_files self.interactive_python_session = opts.interactive_python_session self.contains = opts.contains self.modify = opts.modify if opts.slice: self.slice = string2index(opts.slice)
def run(opt, args): images = Images() if opt.aneb: opt.image_number = '-1' if len(args) > 0: from ase.io import string2index images.read(args, string2index(opt.image_number)) else: images.initialize([Atoms()]) if opt.interpolate: images.interpolate(opt.interpolate) if opt.aneb: images.aneb() if opt.repeat != '1': r = opt.repeat.split(',') if len(r) == 1: r = 3 * r images.repeat_images([int(c) for c in r]) if opt.output is not None: images.write(opt.output, rotations=opt.rotations, show_unit_cell=opt.show_unit_cell) opt.terminal = True if opt.terminal: if opt.graph is not None: data = images.graph(opt.graph) for line in data.T: for x in line: print x, print else: from ase.gui.gui import GUI import ase.gui.gtkexcepthook gui = GUI(images, opt.rotations, opt.show_unit_cell, opt.bonds) gui.run(opt.graph)
def write(self, filename, rotations='', bbox=None, **kwargs): # XXX We should show the unit cell whenever there is one indices = range(len(self)) p = filename.rfind('@') if p != -1: try: slice = string2index(filename[p + 1:]) except ValueError: pass else: indices = indices[slice] filename = filename[:p] if isinstance(indices, int): indices = [indices] images = [self.get_atoms(i) for i in indices] if len(filename) > 4 and filename[-4:] in ['.eps', '.png', '.pov']: write(filename, images, rotation=rotations, bbox=bbox, **kwargs) else: write(filename, images, **kwargs)
def run(args): from ase.gui.images import Images from ase.atoms import Atoms images = Images() if args.filenames: from ase.io import string2index images.read(args.filenames, string2index(args.image_number)) else: images.initialize([Atoms()]) if args.interpolate: images.interpolate(args.interpolate) if args.repeat != '1': r = args.repeat.split(',') if len(r) == 1: r = 3 * r images.repeat_images([int(c) for c in r]) if args.radii_scale: images.set_radii(args.radii_scale) if args.output is not None: images.write(args.output, rotations=args.rotations, show_unit_cell=args.show_unit_cell) args.terminal = True if args.terminal: if args.graph is not None: data = images.graph(args.graph) for line in data.T: for x in line: print(x, end=' ') print() else: from ase.gui.gui import GUI gui = GUI(images, args.rotations, args.show_unit_cell, args.bonds) gui.run(args.graph)
def save(self, dummy): "The user has pressed the SAVE button." filename = self.chooser.get_filename() if not filename or filename == "<<filename>>": oops("Please specify a file name") return # Check file type suffix = os.path.splitext(filename)[1][1:] if 'POSCAR' in filename or 'CONTCAR' in filename: suffix = 'POSCAR' if suffix == '': # No suffix chosen filt = self.chooser.get_filter().get_name() suffix = self.name_to_suffix[filt] if suffix is None: oops( "Specify file type by giving a suffix or selecting a file type." ) return else: filename = filename + '.' + suffix else: # Suffix given - check that it is not in conflict with selected file type. filt = self.chooser.get_filter().get_name() suffix2 = self.name_to_suffix[filt] if suffix2 is not None and suffix != suffix2: oops( "Your filename suffix conflicts with the file type you have selected." ) return if suffix not in self.name_to_suffix.values(): oops("Unknown file suffix " + suffix) return # We now have a consistent file name with an allowed suffix. # Find out which images we want to save. if self.radio_thisconf.get_active(): indices = [self.gui.frame] elif self.radio_allconf.get_active(): indices = range(self.gui.images.nimages) elif self.radio_someconf.get_active(): txt = self.whichconf.get_text() if not txt: oops("Please specify which images to save.") return try: slice = string2index(txt) except ValueError: oops("ERROR: Failed to parse image specification '%s'" % (txt, )) return indices = range(self.gui.images.nimages)[slice] if isinstance(indices, int): indices = [indices] else: raise RuntimeError( "No radio button selected - should not be possible!") # Now we are ready to write the file! extra = {} remove_hidden = False if self.is_graphics[suffix]: bbox = np.empty(4) size = np.array([self.gui.width, self.gui.height]) / self.gui.scale bbox[0:2] = np.dot(self.gui.center, self.gui.axes[:, :2]) - size / 2 bbox[2:] = bbox[:2] + size extra['rotation'] = self.gui.axes extra['show_unit_cell'] = self.gui.ui.get_widget( '/MenuBar/ViewMenu/ShowUnitCell').get_active() extra['bbox'] = bbox extra['colors'] = self.gui.get_colors( rgb=True)[self.gui.images.visible] remove_hidden = True if len(indices) == 1: # Saving a single configuration is always possible. write( filename, self.gui.images.get_atoms(indices[0], remove_hidden=remove_hidden), **extra) elif self.support_multi[suffix]: images = [ self.gui.images.get_atoms(i, remove_hidden=remove_hidden) for i in indices ] write(filename, images, **extra) else: # We want to write multiple images, but the file format does not support it. # The solution is to write multiple files, inserting a number in the file name # before the suffix. filename = filename.replace('%', '%%') # Preserve % in filenames. suffixpos = filename.rfind('.') filename = filename[:suffixpos] + '%05d' + filename[suffixpos:] for i, idx in enumerate(indices): write( filename % (i, ), self.gui.images.get_atoms(idx, remove_hidden=remove_hidden), **extra) oops("Wrote %d files" % (len(indices), ), (filename % (0, )) + ' .. ' + (filename % (len(indices) - 1, ))) self.destroy()
for field in self.fields]) sorting_array = field_data * self.scent[:, np.newaxis] sorting_array = sorting_array[self.hier] sorting_array = prec_round(sorting_array, self.tableformat.precision) field_data = field_data[:, np.lexsort(sorting_array)].transpose() if csv: rowformat = ','.join( ['{:h}' if field == 'el' else '{}' for field in self.fields]) else: rowformat = ''.join([self.tableformat.fmt[field] for field in self.fields]) body = [ self.tableformat.formatter( rowformat, *row) for row in field_data] return '\n'.join(body) default_index = string2index(':') def slice_split(filename): if '@' in filename: filename, index = parse_filename(filename, None) else: filename, index = parse_filename(filename, default_index) return filename, index
def save(self, dummy): "The user has pressed the SAVE button." filename = self.chooser.get_filename() if not filename or filename == "<<filename>>": oops("Please specify a file name") return # Check file type suffix = os.path.splitext(filename)[1][1:] if 'POSCAR' in filename or 'CONTCAR' in filename: suffix = 'POSCAR' if suffix == '': # No suffix chosen filt = self.chooser.get_filter().get_name() suffix = self.name_to_suffix[filt] if suffix is None: oops("Specify file type by giving a suffix or selecting a file type.") return else: filename = filename + '.' + suffix else: # Suffix given - check that it is not in conflict with selected file type. filt = self.chooser.get_filter().get_name() suffix2 = self.name_to_suffix[filt] if suffix2 is not None and suffix != suffix2: oops("Your filename suffix conflicts with the file type you have selected.") return if suffix not in self.name_to_suffix.values(): oops("Unknown file suffix "+suffix) return # We now have a consistent file name with an allowed suffix. # Find out which images we want to save. if self.radio_thisconf.get_active(): indices = [self.gui.frame] elif self.radio_allconf.get_active(): indices = range(self.gui.images.nimages) elif self.radio_someconf.get_active(): txt = self.whichconf.get_text() if not txt: oops("Please specify which images to save.") return try: slice = string2index(txt) except ValueError: oops("ERROR: Failed to parse image specification '%s'" % (txt,)) return indices = range(self.gui.images.nimages)[slice] if isinstance(indices, int): indices = [indices] else: raise RuntimeError("No radio button selected - should not be possible!") # Now we are ready to write the file! extra = {} remove_hidden = False if self.is_graphics[suffix]: bbox = np.empty(4) size = np.array([self.gui.width, self.gui.height]) / self.gui.scale bbox[0:2] = np.dot(self.gui.center, self.gui.axes[:, :2]) - size / 2 bbox[2:] = bbox[:2] + size extra['rotation'] = self.gui.axes extra['show_unit_cell'] = self.gui.ui.get_widget('/MenuBar/ViewMenu/ShowUnitCell').get_active() extra['bbox'] = bbox extra['colors'] = self.gui.get_colors(rgb=True)[self.gui.images.visible] remove_hidden = True if len(indices) == 1: # Saving a single configuration is always possible. write(filename, self.gui.images.get_atoms(indices[0], remove_hidden=remove_hidden), **extra) elif self.support_multi[suffix]: images = [self.gui.images.get_atoms(i, remove_hidden=remove_hidden) for i in indices] write(filename, images, **extra) else: # We want to write multiple images, but the file format does not support it. # The solution is to write multiple files, inserting a number in the file name # before the suffix. filename = filename.replace('%', '%%') # Preserve % in filenames. suffixpos = filename.rfind('.') filename = filename[:suffixpos] + '%05d' + filename[suffixpos:] for i, idx in enumerate(indices): write(filename % (i,), self.gui.images.get_atoms(idx, remove_hidden=remove_hidden), **extra) oops("Wrote %d files" % (len(indices),), (filename % (0,)) + ' .. ' + (filename % (len(indices)-1,))) self.destroy()