def main (): client, db = mongo.connect() parser = argparse.ArgumentParser(description='UTILITIES') parser.add_argument('molecule', help='the name of the molecule/molecues', nargs='?') parser.set_defaults(which='main') subparser = parser.add_subparsers(help='TEST SET COMMANDS') plotter = subparser.add_parser('plot', help='PLOTTING OPERATIONS') plotter.add_argument('-d', '--dataset', required=True, help='Dataset to plot') plotter.add_argument('-s', '--savefig', help='Location to save the figure') plotter.set_defaults(which='plotting') converter = subparser.add_parser('convert', help='CONVERSION OPERATIONS') converter.add_argument('-s', '--save', help='Name the com file') converter.set_defaults(which='convert') spectra = subparser.add_parser('spectra', help='SPECTRA GRAB') spectra.add_argument('-s', '--savefig', help='Location to save the figure') spectra.add_argument('-d', '--dataset', help='dataset to examine, otherwise all', default=None) spectra.set_defaults(which='spectra') args = parser.parse_args() mol = molecule.Molecule(os.getcwd()).pull_record(db, {'g_safename':args.molecule}) if args.which == 'plotting': if args.savefig: loc = args.savefig else: loc = None main_title = mol.g_safename + ' ' + args.dataset shift_sets = tensor_analysis.molecule_tensor_analysis(mol, args.dataset) exp, calc, names = tensor_analysis.extract_data(shift_sets) fig = plotting.Figure(dimensions=(12,5), cols=1, main_title=main_title) plotting.plot_tensor_scatter(calc, exp, fig, loc, legend=False) fig.show_figure() # plotting.plot_tensors(tensors, title=args.dataset, loc=loc) elif args.which == 'convert': params = molecule.input_gaussian_params() molecule.convert_to_com(mol, **params) mol.cleanup() if args.save: if not args.save.endswith('.com'): args.save += '.com' old = os.path.join(os.getcwd(), mol.g_safename, mol.g_safename+'.com') new = os.path.join(os.path.dirname(old), args.save) os.rename(old, new) elif args.which == 'spectra': shift_list = [] for x in mol.get_spectra(nuclei='13C'): if hasattr(x, 'dataset'): name = "(C) " + x.dataset else: name = "(E) " + str(x.assignment_method) shifts = sorted(x.shifts, key=lambda y: y[0]) shifts.insert(0, name) shift_list.append(shifts) print '\n' for sl in sublist(shift_list, 5): for line in zip(*sl): # print shifts print ''.join(['{:<30}'.format(x) for x in line]) print '\n' exp_spectra = mol.get_spectra(computed=False, nuclei='13C') if args.dataset: comp_spectra = mol.get_spectra(computed=True, dataset=args.dataset, assignment_method='Computed') else: comp_spectra = mol.get_spectra(computed=True, assignment_method='Computed') if comp_spectra: print '{:<30}{:<25}{:<20}{:<20}{:<20}'.format('Experimental', 'Computed', 'slope', 'intercept', 'R') for exp_spec in exp_spectra: for comp_spec in comp_spectra: stats = tensor_analysis.regres_stats(comp_spec, exp_spec) print '{:<30}{:<25}{:<20}{:<20}{:<20}'.format(exp_spec.assignment_method, comp_spec.dataset, round(stats[0],4), round(stats[1],4), round(stats[2],4)) if args.savefig: print 'Saving figure to {0}'.format(saveto) t = tensor_analysis.molecule_tensor_analysis(mol, comp_spec.dataset) plotting.plot_tensors(t, title=mol.g_safename, loc=saveto)
def main(): client, db = mongo.connect() parser = argparse.ArgumentParser(description='PLOTTING') parser.add_argument('-d', '--dataset', nargs='+', required=True, default=[], help='The dataset(s) to draw from') parser.add_argument('-m', '--molecules', type = argparse.FileType('r'), default = []) parser.add_argument('-s', '--savefig', help='Location to save the figure') parser.add_argument('-t', '--title', required=True, help='The title of the figure - enclose in ""') parser.add_argument('-p', '--plot_title', action='store_true', help='Will try to create plot titles from name of dataset') parser.add_argument('-l', '--legend', action='store_true', help='Includes legends on plot') parser.add_argument('--size', nargs=2, default=[14,8], type=float, help='The size of the plot to draw') parser.add_argument('--cols', default=4, type=float, help='The number of columns to display') parser.set_defaults(which='main') subparser = parser.add_subparsers(help='Plot types') scatter = subparser.add_parser('scatter', help="Plot a scatter graph") scatter.add_argument('-e', '--errors', action='store_true', help='Includes error plots') scatter.set_defaults(which='scatter') error = subparser.add_parser('error', help="Plot an error graph") error.set_defaults(which='error') hybrid = subparser.add_parser('hybrid', help="Plot a hybrid error graph") hybrid.set_defaults(which='hybrid') charge = subparser.add_parser('charge', help="Plot a charge error graph") charge.set_defaults(which='charge') charge3D = subparser.add_parser('charge_3D', help="Plot a 3D charge error graph") charge3D.set_defaults(which='charge_3D') args = parser.parse_args() dataset_names = glob_datasets(db.shifts.distinct('spectra.dataset'), args.dataset) figure = plotting.Figure(dimensions=args.size, main_title=args.title, cols=args.cols) if isinstance(args.molecules, file): lines = args.molecules.readlines() args.molecules = [x.rstrip() for x in lines] for i,name in enumerate(dataset_names): if args.plot_title: axis_title = name.split('/')[-1] functional, basis = axis_title.split('_') basis = '6-311+G(2d,p)'if basis == '631+G' else '6-31G(d,p)' axis_title = functional + ' // ' + basis else: axis_title = name #axis_title = '' if args.molecules: molecules_of_interest = args.molecules else: molecules_of_interest = [] stream = tensor_analysis.TensorStream.build(db, name, molecules_of_interest) exp, calc = stream.exp, stream.calc scaled_tensors = stream.scale_stream() if args.which == 'scatter': plotting.plot_tensor_scatter(exp, calc, figure, plot_title=axis_title, legend=args.legend) if args.errors: plotting.plot_scaled_shift_errors(scaled_tensors, figure, plot_title=axis_title, legend=args.legend) if args.which == 'error': plotting.plot_scaled_shift_errors(scaled_tensors, figure, plot_title=axis_title, legend=args.legend) if args.which == 'hybrid': figure = plotting.plot_scaled_tensors_by_hybridisation(stream.hybridisations, scaled_tensors, plot_title=args.title) if args.which == 'charge': figure = plotting.plot_charge_chart(stream.hybridisations, stream.charges, scaled_tensors, plot_title=args.title) if args.which == 'charge_3D': print args.which figure = plotting.plot_3D_scatter(stream.hybridisations, stream.charges, scaled_tensors, plot_title=args.title) if args.savefig: figure.save_figure(args.savefig) else: figure.show_figure()