def test_passthrough(self): src = """ def test(): x = __pydron_new_cell__('x') x.cell_contents = 42 class X(object): def foo__U0(x, self): print x.cell_contents foo = __pydron_wrap_closure__(foo__U0, (x__P1,)) __pydron_members__ = 123 """ expected = """ def test(): x = __pydron_new_cell__('x') x.cell_contents = 42 def class_X__U0(x__P1): def foo__U0(x, self): print x.cell_contents foo = __pydron_wrap_closure__(foo__U0, (x__P1,)) __pydron_members__ = 123 return __pydron_members__ X = __pydron_read_global__('type')('X', (object,), class_X__U0(x)) """ utils.compare(src, expected, declass.DeClass)
def _rename_file(self): """Batch renames an image sequence of a single file. If self.input_dir is the same as self.output_dir the original file gets renamed, otherwise the file gets renamed and moved to the new location.""" _, ext = os.path.splitext(self.input_fname) out_root = self.output_root if "%%" in out_root: out_root = out_root.replace("%%", "%") destination_path = os.path.join(self.output_dir, out_root + ext) if compare(self.input_dir, self.output_dir): # rename files only try: shutil.move(self.input_path, destination_path) except OSError as e: raise e else: # move and rename files try: shutil.copy(self.input_path, destination_path) except OSError as e: raise e rc, msg = is_file(destination_path) if not rc: e = "Unable to move and rename file from {}. " if compare(self.input_dir, self.output_dir): e = "Unable to rename file from {}. " e += msg raise RuntimeError(e.format(self.input_dir))
def test_rhf_vs_uhf_screening(): """Checks difference between the number of shell quartets screened with Density screening in RHF vs UHF. Difference should be 0, mathematically. Default threshhold of 1.0E-12 is used""" mol = psi4.geometry(""" 0 1 O -1.551007 -0.114520 0.000000 H -1.934259 0.762503 0.000000 H -0.599677 0.040712 0.000000 O 1.350625 0.111469 0.000000 H 1.680398 -0.373741 -0.758561 H 1.680398 -0.373741 0.758561 symmetry c1 no_reorient no_com """) # run rhf calculation psi4.set_options({ "scf_type": "direct", "screening" : 'density', "df_scf_guess" : False, "integral_package": 'libint2', "ints_tolerance" : 1e-12, "reference" : "rhf", "save_jk": True, "bench" : 1 }) rhf_energy, rhf_wfn = psi4.energy('hf/DZ', return_wfn=True) # run uhf calculation psi4.set_options({ "scf_type": "direct", "screening" : 'density', "df_scf_guess" : False, "integral_package": 'libint2', "ints_tolerance" : 1e-12, "reference" : "uhf", "save_jk": True, "bench" : 1 }) uhf_energy, uhf_wfn = psi4.energy('hf/DZ', return_wfn=True) # prep for comparing results to expected values rhf_computed_shells = rhf_wfn.jk().computed_shells_per_iter() uhf_computed_shells = uhf_wfn.jk().computed_shells_per_iter() computed_shells_expected = [13171, 19618, 19665, 19657, 19661, 19661, 19663, 19663, 19663] # compare iteration counts of runs with computed shell quartet array lengths # iteration_+1 is used to account for computed_shells arrays including SAD guess results assert(len(computed_shells_expected) == rhf_wfn.iteration_+1) assert(len(computed_shells_expected) == uhf_wfn.iteration_+1) # actually compare results with expected values assert compare(computed_shells_expected, rhf_computed_shells, 'Schwarz Computed Shells Count, Cutoff 1.0e-12') assert compare(computed_shells_expected, uhf_computed_shells, 'Density Computed Shells Count, Cutoff 1.0e-12')
def testImportNested(self): src = """ import x.y """ expected = """ x = __import__('x.y', globals(), None, None) """ utils.compare(src, expected, deimport.DeImport)
def testModImport(self): src = """ import x """ expected = """ x = __import__('x', globals(), None, None) """ utils.compare(src, expected, deimport.DeImport)
def testImportAlias(self): src = """ import x as y """ expected = """ y = __import__('x', globals(), None, None) """ utils.compare(src, expected, deimport.DeImport)
def testImportFromPureRelative(self): src = """ from . import y """ expected = """ module__U0 = __import__(None, globals(), None, ('y',), 1) y = module__U0.y """ utils.compare(src, expected, deimport.DeImport)
def testImportFromRelative2(self): src = """ from ..x import y """ expected = """ module__U0 = __import__('x', globals(), None, ('y',), 2) y = module__U0.y """ utils.compare(src, expected, deimport.DeImport)
def testImportFromNested(self): src = """ from a.b.c import y """ expected = """ module__U0 = __import__('a.b.c', globals(), None, ('y',)) y = module__U0.y """ utils.compare(src, expected, deimport.DeImport)
def testImportMultiple(self): src = """ import x, y """ expected = """ x = __import__('x', globals(), None, None) y = __import__('y', globals(), None, None) """ utils.compare(src, expected, deimport.DeImport)
def testImportFromAs(self): src = """ from x import y as z """ expected = """ module__U0 = __import__('x', globals(), None, ('y',)) z = module__U0.y """ utils.compare(src, expected, deimport.DeImport)
def step_statictics(simu, network, plot, inputs, outputs): entire_FoN = inputs + network['FoN'].stateHiddenNeurons + network[ 'FoN'].stateOutputNeurons entire_FoN2 = inputs + network['FoN2'].stateHiddenNeurons + network[ 'FoN2'].stateOutputNeurons lin = simu.examples.ninputs lhi = lin + lin // 4 lout = lhi + simu.examples.noutputs #rms simu.rms('FoN', inputs, outputs) simu.rms('SoN', network['FoN'].stateHiddenNeurons, entire_FoN) plot['SoN_rms_input'] += network['SoN'].calc_RMS_range( network['FoN'].stateHiddenNeurons, entire_FoN, 0, lin) / lout * (lin - 0) plot['SoN_rms_hidden'] += network['SoN'].calc_RMS_range( network['FoN'].stateHiddenNeurons, entire_FoN, lin, lhi) / lout * (lhi - lin) plot['SoN_rms_output'] += network['SoN'].calc_RMS_range( network['FoN'].stateHiddenNeurons, entire_FoN, lhi, lout) / lout * (lout - lhi) simu.rms('FoN2', inputs, outputs) simu.rms('SoN2', network['FoN2'].stateHiddenNeurons, entire_FoN2) plot['SoN2_rms_input'] += network['SoN2'].calc_RMS_range( network['FoN2'].stateHiddenNeurons, entire_FoN2, 0, lin) / lout * (lin - 0) plot['SoN2_rms_hidden'] += network['SoN2'].calc_RMS_range( network['FoN2'].stateHiddenNeurons, entire_FoN2, lin, lhi) / lout * (lhi - lin) plot['SoN2_rms_output'] += network['SoN2'].calc_RMS_range( network['FoN2'].stateHiddenNeurons, entire_FoN2, lhi, lout) / lout * (lout - lhi) #err simu.err('FoN', outputs) plot['SoN_err_input'] += 1 - compare( inputs, network['SoN'].stateOutputNeurons[0:lin]) if (not compare_f(network['FoN'].stateHiddenNeurons, network['SoN'].stateOutputNeurons[lin:lhi], 0.3)): plot['SoN_err_hidden'] += 1 if (index_max(network['SoN'].stateOutputNeurons[lhi:lout]) != index_max(network['FoN'].stateOutputNeurons)): plot['SoN_err_output'] += 1 simu.err('FoN2', outputs) plot['SoN2_err_input'] += 1 - compare( inputs, network['SoN2'].stateOutputNeurons[0:lin]) if (not compare_f(network['FoN2'].stateHiddenNeurons, network['SoN2'].stateOutputNeurons[lin:lhi], 0.3)): plot['SoN2_err_hidden'] += 1 if (index_max(network['SoN'].stateOutputNeurons[lhi:lout]) != index_max(network['FoN2'].stateOutputNeurons)): plot['SoN2_err_output'] += 1
def test_read_implicit(self): src = """ def test(): print x """ expected = """ def test(): print __pydron_read_global__('x') """ utils.compare(src, expected, deglobal.DeGlobal)
def test_parameter(self): src = """ def test(x): pass """ expected = """ def test(x): pass """ utils.compare(src, expected, deunbound.DeUnbound)
def testImportFromMultiple(self): src = """ from x import y, z """ expected = """ module__U0 = __import__('x', globals(), None, ('y', 'z')) y = module__U0.y z = module__U0.z """ utils.compare(src, expected, deimport.DeImport)
def test_False(self): src = """ def test(): return None """ expected = """ def test(): return None """ utils.compare(src, expected, deglobal.DeGlobal)
def testImportFromStar(self): src = """ from x import * """ expected = """ module__U0 = __import__('x', globals(), None, ('*',)) for module_element__U0 in getattr(module__U0, '__all__', dir(module__U0)): globals()[module_element__U0] = getattr(module__U0, module_element__U0) """ utils.compare(src, expected, deimport.DeImport)
def test_builtin(self): src = """ def test(): globals() """ expected = """ def test(): __pydron_read_global__('globals')() """ utils.compare(src, expected, deglobal.DeGlobal)
def test_compare(self): """ Tests the compare funciton """ a, b = "test123", "test123" compare_result_true = compare(a, b) self.assertTrue(compare_result_true) a, b = "test123", "test456" compare_result_false = compare(a, b) self.assertFalse(compare_result_false)
def test_compare(self): """ Tests the compare funciton """ a, b = 'test123', 'test123' compare_result_true = compare(a, b) self.assertTrue(compare_result_true) a, b = 'test123', 'test456' compare_result_false = compare(a, b) self.assertFalse(compare_result_false)
def step_statictics(simu, network, plot, inputs, outputs): entire_FoN = inputs + network["FoN"].stateHiddenNeurons + network["FoN"].stateOutputNeurons entire_FoN2 = inputs + network["FoN2"].stateHiddenNeurons + network["FoN2"].stateOutputNeurons lin = simu.examples.ninputs lhi = lin + lin // 4 lout = lhi + simu.examples.noutputs # rms simu.rms("FoN", inputs, outputs) simu.rms("SoN", network["FoN"].stateHiddenNeurons, entire_FoN) plot["SoN_rms_input"] += ( network["SoN"].calc_RMS_range(network["FoN"].stateHiddenNeurons, entire_FoN, 0, lin) / lout * (lin - 0) ) plot["SoN_rms_hidden"] += ( network["SoN"].calc_RMS_range(network["FoN"].stateHiddenNeurons, entire_FoN, lin, lhi) / lout * (lhi - lin) ) plot["SoN_rms_output"] += ( network["SoN"].calc_RMS_range(network["FoN"].stateHiddenNeurons, entire_FoN, lhi, lout) / lout * (lout - lhi) ) simu.rms("FoN2", inputs, outputs) simu.rms("SoN2", network["FoN2"].stateHiddenNeurons, entire_FoN2) plot["SoN2_rms_input"] += ( network["SoN2"].calc_RMS_range(network["FoN2"].stateHiddenNeurons, entire_FoN2, 0, lin) / lout * (lin - 0) ) plot["SoN2_rms_hidden"] += ( network["SoN2"].calc_RMS_range(network["FoN2"].stateHiddenNeurons, entire_FoN2, lin, lhi) / lout * (lhi - lin) ) plot["SoN2_rms_output"] += ( network["SoN2"].calc_RMS_range(network["FoN2"].stateHiddenNeurons, entire_FoN2, lhi, lout) / lout * (lout - lhi) ) # err simu.err("FoN", outputs) plot["SoN_err_input"] += 1 - compare(inputs, network["SoN"].stateOutputNeurons[0:lin]) if not compare_f(network["FoN"].stateHiddenNeurons, network["SoN"].stateOutputNeurons[lin:lhi], 0.3): plot["SoN_err_hidden"] += 1 if index_max(network["SoN"].stateOutputNeurons[lhi:lout]) != index_max(network["FoN"].stateOutputNeurons): plot["SoN_err_output"] += 1 simu.err("FoN2", outputs) plot["SoN2_err_input"] += 1 - compare(inputs, network["SoN2"].stateOutputNeurons[0:lin]) if not compare_f(network["FoN2"].stateHiddenNeurons, network["SoN2"].stateOutputNeurons[lin:lhi], 0.3): plot["SoN2_err_hidden"] += 1 if index_max(network["SoN"].stateOutputNeurons[lhi:lout]) != index_max(network["FoN2"].stateOutputNeurons): plot["SoN2_err_output"] += 1
def test_delete_non_global(self): src = """ def test(): del x, y """ expected = """ def test(): del x del y """ utils.compare(src, expected, deglobal.DeGlobal)
def test_class(self): src = """ class test(object): x = 1 """ expected = """ class test(__pydron_unbound_check__(object)): x = __pydron_unbound__ x = 1 """ utils.compare(src, expected, deunbound.DeUnbound)
def test_None(self): src = """ def test(): x = None """ expected = """ def test(): x = __pydron_unbound__ x = None """ utils.compare(src, expected, deunbound.DeUnbound)
def test_assign_explicit(self): src = """ def test(): global x x = 1 """ expected = """ def test(): __pydron_assign_global__('x', 1) """ utils.compare(src, expected, deglobal.DeGlobal)
def test_delete(self): src = """ def test(): global x del x """ expected = """ def test(): __pydron_delete_global__('x') """ utils.compare(src, expected, deglobal.DeGlobal)
def filter_normalization(obj, lothreshold, hithreshold, config=None): """ This function takes an object with normalization integration information and a threshold and creates a mask file containing the pixel IDs that do not make it above the threshold. @param obj: The object containing the normalization information @type obj: C{SOM.SOM} @param lothreshold: The value below which a pixel will be masked. @type lothreshold: C{float} @param hithreshold: The value above which a pixel will be masked. @type hithreshold: C{float} @param config: The object holding the DR configuration @type config: L{hlr_utils.Configure} @raise TypeError: The incoming object is not a C{SOM}. """ import hlr_utils o_descr = hlr_utils.get_descr(obj) if o_descr != "SOM": raise TypeError("Only SOMs are allowed in this function!") if config is None: # Make mask file name from object information instname = obj.attr_list.inst.get_name() runnum = obj.attr_list["run_number"] outfile = "%s_%s_mask.dat" % (instname, str(runnum)) else: # Make mask file name from configuration information outfile = hlr_utils.ext_replace(config.output, config.ext_replacement, "dat") outfile = hlr_utils.add_tag(outfile, "mask") ofile = open(outfile, "w") import SOM import utils len_obj = hlr_utils.get_length(obj) for i in xrange(len_obj): norm = hlr_utils.get_value(obj, i, o_descr) if utils.compare(norm, lothreshold) <= 0 or \ utils.compare(norm, hithreshold) >= 0: map_so = hlr_utils.get_map_so(obj, None, i) pix_id = SOM.NeXusId.fromString(str(map_so.id)).toJoinedStr() print >> ofile, pix_id ofile.close()
def test_assignment(self): src = """ def test(): x = 1 """ expected = """ def test(): x = __pydron_unbound__ x = 1 """ utils.compare(src, expected, deunbound.DeUnbound)
def test_forward_pass(net_arguments): net, pytorch_net, layers = net_arguments input_data = np.array( [[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], [[-1, -2, 0, 1], [-2, -3, 1, 2], [3, 4, 2, -1], [-2, -3, -4, 9]]]], dtype=np.float64) outputs = net.forward(input_data) pytorch_outputs = pytorch_net(torch.from_numpy(input_data)) compare(outputs, nn.Softmax(dim=1)(pytorch_outputs))
def custom_sort(c1, c2): compare_qt_restricoes = compare(c1.qt_restricoes, c2.qt_restricoes) compare_aptidao_dist = compare(c1.aptidao_dist, c2.aptidao_dist) compare_aptidao_cars = compare(c1.aptidao_cars, c2.aptidao_cars) if compare_qt_restricoes == 0: if compare_aptidao_dist == 0: return compare_aptidao_cars else: return compare_aptidao_dist else: return compare_qt_restricoes
def test_builtin(self): src = """ def test(): __pydron_var__ = 1 print __pydron_var__ """ expected = """ def test(): __pydron_var__ = 1 print __pydron_var__ """ utils.compare(src, expected, deunbound.DeUnbound)
def test_training_sgd(net_arguments, mnist): mnist_train, _ = mnist iterations = 100 net, pytorch_net, layers = net_arguments optimizer = optim.SGD(net.pytorch_parameters, lr=0.001, momentum=0.9) pytorch_optimizer = optim.SGD(pytorch_net.parameters(), lr=0.001, momentum=0.9) criterion = nn.CrossEntropyLoss() for i, data in enumerate(mnist_train, 0): images, labels = data def loss_grad(layer, b): gradients = [] for j in range(10): result = layer.result.data[j, b] if j == labels[b]: result -= 1 gradients.append(result) return gradients images = images.double() net.forward(images.numpy()) net.backward(loss_grad, optimizer) pytorch_optimizer.zero_grad() pytorch_outputs = pytorch_net(images) pytorch_loss = criterion(pytorch_outputs, labels) pytorch_loss.backward() pytorch_optimizer.step() if i == iterations - 1: break pytorch_layers = [ pytorch_net.conv1, pytorch_net.conv2, pytorch_net.fc1, pytorch_net.fc2, pytorch_net.fc3 ] devito_layers = [layers[0], layers[2], layers[5], layers[6], layers[7]] for i in range(len(pytorch_layers)): pytorch_layer = pytorch_layers[i] devito_layer = devito_layers[i] compare(devito_layer.kernel.data, pytorch_layer.weight) compare(devito_layer.bias.data, pytorch_layer.bias)
def filter_normalization(obj, lothreshold, hithreshold, config=None): """ This function takes an object with normalization integration information and a threshold and creates a mask file containing the pixel IDs that do not make it above the threshold. @param obj: The object containing the normalization information @type obj: C{SOM.SOM} @param lothreshold: The value below which a pixel will be masked. @type lothreshold: C{float} @param hithreshold: The value above which a pixel will be masked. @type hithreshold: C{float} @param config: The object holding the DR configuration @type config: L{hlr_utils.Configure} @raise TypeError: The incoming object is not a C{SOM}. """ import hlr_utils o_descr = hlr_utils.get_descr(obj) if o_descr != "SOM": raise TypeError("Only SOMs are allowed in this function!") if config is None: # Make mask file name from object information instname = obj.attr_list.inst.get_name() runnum = obj.attr_list["run_number"] outfile = "%s_%s_mask.dat" % (instname, str(runnum)) else: # Make mask file name from configuration information outfile = hlr_utils.ext_replace(config.output, config.ext_replacement, "dat") outfile = hlr_utils.add_tag(outfile, "mask") ofile = open(outfile, "w") import SOM import utils len_obj = hlr_utils.get_length(obj) for i in xrange(len_obj): norm = hlr_utils.get_value(obj, i, o_descr) if utils.compare(norm, lothreshold) <= 0 or utils.compare(norm, hithreshold) >= 0: map_so = hlr_utils.get_map_so(obj, None, i) pix_id = SOM.NeXusId.fromString(str(map_so.id)).toJoinedStr() print >> ofile, pix_id ofile.close()
def test_use(self): src = """ def test(): x = 1 print x """ expected = """ def test(): x = __pydron_unbound__ x = 1 print __pydron_unbound_check__(x) """ utils.compare(src, expected, deunbound.DeUnbound)
def test_delete(self): src = """ def test(): x = 1 del x """ expected = """ def test(): x = __pydron_unbound__ x = 1 x = __pydron_unbound__ """ utils.compare(src, expected, deunbound.DeUnbound)
def test_delete_mix(self): src = """ def test(): global y del x, y, z """ expected = """ def test(): del x __pydron_delete_global__('y') del z """ utils.compare(src, expected, deglobal.DeGlobal)
def test_assign_mixed(self): src = """ def test(): global x x = 1 print x """ expected = """ def test(): __pydron_assign_global__('x', 1) print __pydron_read_global__('x') """ utils.compare(src, expected, deglobal.DeGlobal)
def test_ClassDef(self): src = """ def test(): global x class x(): pass """ expected = """ def test(): class x__U0(): pass __pydron_assign_global__('x', x__U0) """ utils.compare(src, expected, deglobal.DeGlobal)