Python numpy.savetxt() Examples

The following are 30 code examples for showing how to use numpy.savetxt(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module numpy , or try the search function .

Example 1
Project: mlearn   Author: materialsvirtuallab   File: gap.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def write_param(self, xml_filename='gap.xml'):
        """
        Write xml file to perform lammps calculation.

        Args:
            xml_filename (str): Filename to store xml formatted parameters.
        """
        if not self.param:
            raise RuntimeError("The xml and parameters should be provided.")
        tree = self.param.get('xml')
        root = tree.getroot()
        gpcoordinates = list(root.iter('gpCoordinates'))[0]
        param_filename = "{}.soapparam".format(self.name)
        gpcoordinates.set('sparseX_filename', param_filename)
        np.savetxt(param_filename, self.param.get('param'), fmt='%.20e')
        tree.write(xml_filename)
        pair_coeff = self.pair_coeff.format(xml_filename,
                                            '\"Potential xml_label={}\"'.
                                            format(self.param.get('potential_label')),
                                            self.specie.Z)
        ff_settings = [self.pair_style, pair_coeff]
        return ff_settings 
Example 2
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: utils.py    License: Apache License 2.0 6 votes vote down vote up
def pred_test(testing_data, exe, param_list=None, save_path=""):
    ret = numpy.zeros((testing_data.shape[0], 2))
    if param_list is None:
        for i in range(testing_data.shape[0]):
            exe.arg_dict['data'][:] = testing_data[i, 0]
            exe.forward(is_train=False)
            ret[i, 0] = exe.outputs[0].asnumpy()
            ret[i, 1] = numpy.exp(exe.outputs[1].asnumpy())
        numpy.savetxt(save_path, ret)
    else:
        for i in range(testing_data.shape[0]):
            pred = numpy.zeros((len(param_list),))
            for j in range(len(param_list)):
                exe.copy_params_from(param_list[j])
                exe.arg_dict['data'][:] = testing_data[i, 0]
                exe.forward(is_train=False)
                pred[j] = exe.outputs[0].asnumpy()
            ret[i, 0] = pred.mean()
            ret[i, 1] = pred.std()**2
        numpy.savetxt(save_path, ret)
    mse = numpy.square(ret[:, 0] - testing_data[:, 0] **3).mean()
    return mse, ret 
Example 3
Project: pruning_yolov3   Author: zbyuan   File: utils.py    License: GNU General Public License v3.0 6 votes vote down vote up
def print_mutation(hyp, results, bucket=''):
    # Print mutation results to evolve.txt (for use with train.py --evolve)
    a = '%10s' * len(hyp) % tuple(hyp.keys())  # hyperparam keys
    b = '%10.3g' * len(hyp) % tuple(hyp.values())  # hyperparam values
    c = '%10.3g' * len(results) % results  # results (P, R, mAP, F1, test_loss)
    print('\n%s\n%s\nEvolved fitness: %s\n' % (a, b, c))

    if bucket:
        os.system('gsutil cp gs://%s/evolve.txt .' % bucket)  # download evolve.txt

    with open('evolve.txt', 'a') as f:  # append result
        f.write(c + b + '\n')
    x = np.unique(np.loadtxt('evolve.txt', ndmin=2), axis=0)  # load unique rows
    np.savetxt('evolve.txt', x[np.argsort(-fitness(x))], '%10.3g')  # save sort by fitness

    if bucket:
        os.system('gsutil cp evolve.txt gs://%s' % bucket)  # upload evolve.txt 
Example 4
Project: ArtGAN   Author: cs-chan   File: ingest_flower102.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def run(self):
        """
        resize images then write manifest files to disk.
        """
        self.write_label()
        self.collectdata()

        records = [(fname, tgt)
                   for fname, tgt in self.trainpairlist.items()]
        np.savetxt(self.manifests['train'], records, fmt='%s,%s')

        records = [(fname, tgt)
                   for fname, tgt in self.valpairlist.items()]
        np.savetxt(self.manifests['val'], records, fmt='%s,%s')

        records = [(fname, tgt)
                   for fname, tgt in self.testpairlist.items()]
        np.savetxt(self.manifests['test'], records, fmt='%s,%s') 
Example 5
Project: transferlearning   Author: jindongwang   File: main.py    License: MIT License 6 votes vote down vote up
def extract_feature(model, dataloader, save_path, load_from_disk=True, model_path=''):
    if load_from_disk:
        model = models.Network(base_net=args.model_name,
                               n_class=args.num_class)
        model.load_state_dict(torch.load(model_path))
        model = model.to(DEVICE)
    model.eval()
    correct = 0
    fea_all = torch.zeros(1,1+model.base_network.output_num()).to(DEVICE)
    with torch.no_grad():
        for inputs, labels in dataloader:
            inputs, labels = inputs.to(DEVICE), labels.to(DEVICE)
            feas = model.get_features(inputs)
            labels = labels.view(labels.size(0), 1).float()
            x = torch.cat((feas, labels), dim=1)
            fea_all = torch.cat((fea_all, x), dim=0)
            outputs = model(inputs)
            preds = torch.max(outputs, 1)[1]
            correct += torch.sum(preds == labels.data.long())
        test_acc = correct.double() / len(dataloader.dataset)
    fea_numpy = fea_all.cpu().numpy()
    np.savetxt(save_path, fea_numpy[1:], fmt='%.6f', delimiter=',')
    print('Test acc: %f' % test_acc)

# You may want to classify with 1nn after getting features 
Example 6
Project: pointnet-registration-framework   Author: vinits5   File: generate_rotations.py    License: MIT License 6 votes vote down vote up
def main(args):
    # dataset
    testset = get_datasets(args)
    batch_size = len(testset)

    amp = args.deg * math.pi / 180.0
    w = torch.randn(batch_size, 3)
    w = w / w.norm(p=2, dim=1, keepdim=True) * amp
    t = torch.rand(batch_size, 3) * args.max_trans

    if args.format == 'wv':
        # the output: twist vectors.
        R = ptlk.so3.exp(w) # (N, 3) --> (N, 3, 3)
        G = torch.zeros(batch_size, 4, 4)
        G[:, 3, 3] = 1
        G[:, 0:3, 0:3] = R
        G[:, 0:3, 3] = t

        x = ptlk.se3.log(G) # --> (N, 6)
    else:
        # rotation-vector and translation-vector
        x = torch.cat((w, t), dim=1)

    numpy.savetxt(args.outfile, x, delimiter=',') 
Example 7
Project: pymoo   Author: msu-coinlab   File: generate.py    License: Apache License 2.0 6 votes vote down vote up
def generate_test_data():
    for str_problem in ["osy"]:
        problem = get_problem(str_problem)

        X = []

        # define a callback function that prints the X and F value of the best individual
        def my_callback(algorithm):
            pop = algorithm.pop
            _X = pop.get("X")[np.random.permutation(len(pop))[:10]]
            X.append(_X)

        minimize(problem,
                 method='nsga2',
                 method_args={'pop_size': 100},
                 termination=('n_gen', 100),
                 callback=my_callback,
                 pf=problem.pareto_front(),
                 disp=True,
                 seed=1)

        np.savetxt("%s.x" % str_problem, np.concatenate(X, axis=0), delimiter=",") 
Example 8
def get_predict_labels():
    inputs = tf.placeholder("float", [None, 64, 64, 1])
    is_training = tf.placeholder("bool")
    prediction, _ = googlenet(inputs, is_training)
    predict_labels = tf.argmax(prediction, 1)
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    data = sio.loadmat("../data/dataset.mat")
    testdata = data["test"] / 127.5 - 1.0
    testlabel = data["testlabels"]
    saver.restore(sess, "../save_para/.\\model.ckpt")
    nums_test = testlabel.shape[1]
    PREDICT_LABELS = np.zeros([nums_test])
    for i in range(nums_test // BATCH_SIZE):
        PREDICT_LABELS[i * BATCH_SIZE:i * BATCH_SIZE + BATCH_SIZE] = sess.run(predict_labels, feed_dict={inputs: testdata[i * BATCH_SIZE:i * BATCH_SIZE + BATCH_SIZE], is_training: False})
    PREDICT_LABELS[(nums_test // BATCH_SIZE - 1) * BATCH_SIZE + BATCH_SIZE:] = sess.run(predict_labels, feed_dict={inputs: testdata[(nums_test // BATCH_SIZE - 1) * BATCH_SIZE + BATCH_SIZE:], is_training: False})
    np.savetxt("../data/predict_labels.txt", PREDICT_LABELS) 
Example 9
Project: pyscf   Author: pyscf   File: test_0162_bse_h2o_spin2_uhf_cis.py    License: Apache License 2.0 6 votes vote down vote up
def test_0162_bse_h2o_spin2_uhf_cis(self):
    """ This  """
    mol = gto.M(verbose=1,atom='O 0 0 0; H 0 0.489 1.074; H 0 0.489 -1.074',basis='cc-pvdz',spin=2)

    gto_mf = scf.UHF(mol)
    gto_mf.kernel()
    gto_td = tddft.TDDFT(gto_mf)
    gto_td.nstates = 190
    gto_td.kernel()

    omegas = np.arange(0.0, 2.0, 0.01) + 1j*0.03
    p_ave = -polariz_freq_osc_strength(gto_td.e, gto_td.oscillator_strength(), omegas).imag
    data = np.array([omegas.real*HARTREE2EV, p_ave])
    np.savetxt('test_0162_bse_h2o_spin2_uhf_cis_pyscf.txt', data.T, fmt=['%f','%f'])
    #data_ref = np.loadtxt('test_0162_bse_h2o_spin2_uhf_cis_pyscf.txt-ref').T
    #self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3))
    
    nao_td  = bse_iter(mf=gto_mf, gto=mol, verbosity=0, xc_code='CIS')

    polariz = -nao_td.comp_polariz_inter_ave(omegas).imag
    data = np.array([omegas.real*HARTREE2EV, polariz])
    np.savetxt('test_0162_bse_h2o_spin2_uhf_cis_nao.txt', data.T, fmt=['%f','%f'])
    #data_ref = np.loadtxt('test_0162_bse_h2o_spin2_uhf_cis_nao.txt-ref').T
    #self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3), \
    #  msg="{}".format(abs(data_ref-data).sum()/data.size)) 
Example 10
Project: pyscf   Author: pyscf   File: test_0082_bse_gw_gto_be.py    License: Apache License 2.0 6 votes vote down vote up
def test_bse_gto_vs_nao_inter_0082(self):
    """ Interacting case """
    #dm1 = gto_mf.make_rdm1()
    #o1 = gto_mf.get_ovlp()
    #print(__name__, 'dm1*o1', (dm1*o1).sum())
    nao_td = bse_iter(mf=gto_mf, gto=mol, verbosity=0, xc_code='GW', perform_gw=True)
    
    #dm2 = nao_td.make_rdm1()
    #o2 = nao_td.get_ovlp()
    #n = nao_td.norbs
    #print(__name__, 'dm2*o2', (dm2.reshape((n,n))*o2).sum())
    
    omegas = np.linspace(0.0,2.0,450)+1j*0.04
    p_iter = -nao_td.comp_polariz_inter_ave(omegas).imag
    data = np.array([omegas.real*27.2114, p_iter])
    np.savetxt('be.bse_iter.omega.inter.ave.txt', data.T, fmt=['%f','%f']) 
Example 11
Project: pyscf   Author: pyscf   File: test_0083_gw_bse_h2_ae.py    License: Apache License 2.0 6 votes vote down vote up
def test_gw(self):
    """ This is GW """
    mol = gto.M( verbose = 1, atom = '''H 0 0 0;  H 0.17 0.7 0.587''', basis = 'cc-pvdz',)
    #mol = gto.M( verbose = 0, atom = '''H 0.0 0.0 -0.3707;  H 0.0 0.0 0.3707''', basis = 'cc-pvdz',)
    gto_mf = scf.RHF(mol)
    gto_mf.kernel()
    #print('gto_mf.mo_energy:', gto_mf.mo_energy)
    b = bse_iter(mf=gto_mf, gto=mol, perform_gw=True, xc_code='GW', verbosity=0, nvrt=4)
    #self.assertAlmostEqual(b.mo_energy[0], -0.5967647)
    #self.assertAlmostEqual(b.mo_energy[1], 0.19072719)
    omegas = np.linspace(0.0,2.0,450)+1j*0.04
    p_iter = -b.comp_polariz_inter_ave(omegas).imag
    data = np.array([omegas.real*27.2114, p_iter])
    np.savetxt('h2_gw_bse_iter.omega.inter.ave.txt', data.T)
    data_ref = np.loadtxt('h2_gw_bse_iter.omega.inter.ave.txt-ref').T
    #print(__name__, abs(data_ref-data).sum()/data.size)
    self.assertTrue(np.allclose(data_ref, data, 5))

    p_iter = -b.comp_polariz_nonin_ave(omegas).imag
    data = np.array([omegas.real*27.2114, p_iter])
    np.savetxt('h2_gw_bse_iter.omega.nonin.ave.txt', data.T) 
Example 12
Project: pyscf   Author: pyscf   File: test_0040_bse_rpa_nao.py    License: Apache License 2.0 6 votes vote down vote up
def test_0040_bse_rpa_nao(self):
    """ Compute polarization with RPA via 2-point non-local potentials (BSE solver)  """
    from timeit import default_timer as timer

    dname = os.path.dirname(os.path.abspath(__file__))
    bse = bse_iter(label='water', cd=dname, xc_code='RPA', verbosity=0)
    omegas = np.linspace(0.0,2.0,150)+1j*0.01
    #print(__name__, omegas.shape)
    
    pxx = np.zeros(len(omegas))
    for iw,omega in enumerate(omegas):
      for ixyz in range(1):
        dip = bse.dip_ab[ixyz]
        vab = bse.apply_l(dip, omega)
        pxx[iw] = pxx[iw] - (vab.imag*dip.reshape(-1)).sum()
        
    data = np.array([omegas.real*27.2114, pxx])
    np.savetxt('water.bse_iter_rpa.omega.inter.pxx.txt', data.T, fmt=['%f','%f'])
    data_ref = np.loadtxt(dname+'/water.bse_iter_rpa.omega.inter.pxx.txt-ref')
    #print('    bse.l0_ncalls ', bse.l0_ncalls)
    self.assertTrue(np.allclose(data_ref,data.T, rtol=1.0, atol=1e-05)) 
Example 13
Project: pyscf   Author: pyscf   File: test_0091_tddft_x_zip_na20.py    License: Apache License 2.0 6 votes vote down vote up
def test_x_zip_feature_na20_chain(self):
    """ This a test for compression of the eigenvectos at higher energies """
    dname = dirname(abspath(__file__))
    siesd = dname+'/sodium_20'
    x = td_c(label='siesta', cd=siesd,x_zip=True, x_zip_emax=0.25,x_zip_eps=0.05,jcutoff=7,xc_code='RPA',nr=128, fermi_energy=-0.0913346431431985)
    
    eps = 0.005
    ww = np.arange(0.0, 0.5, eps/2.0)+1j*eps
    data = np.array([ww.real*27.2114, -x.comp_polariz_inter_ave(ww).imag])
    fname = 'na20_chain.tddft_iter_rpa.omega.inter.ave.x_zip.txt'
    np.savetxt(fname, data.T, fmt=['%f','%f'])
    #print(__file__, fname)
    data_ref = np.loadtxt(dname+'/'+fname+'-ref')
    #print('    x.rf0_ncalls ', x.rf0_ncalls)
    #print(' x.matvec_ncalls ', x.matvec_ncalls)
    self.assertTrue(np.allclose(data_ref,data.T, rtol=1.0e-1, atol=1e-06)) 
Example 14
Project: pyscf   Author: pyscf   File: test_0161_bse_h2b_spin1_uhf_cis.py    License: Apache License 2.0 6 votes vote down vote up
def test_161_bse_h2b_spin1_uhf_cis(self):
    """ This  """
    mol = gto.M(verbose=1,atom='B 0 0 0; H 0 0.489 1.074; H 0 0.489 -1.074',basis='cc-pvdz',spin=1)

    gto_mf = scf.UHF(mol)
    gto_mf.kernel()
    gto_td = tddft.TDHF(gto_mf)
    gto_td.nstates = 150
    gto_td.kernel()

    omegas = np.arange(0.0, 2.0, 0.01) + 1j*0.03
    p_ave = -polariz_freq_osc_strength(gto_td.e, gto_td.oscillator_strength(), omegas).imag
    data = np.array([omegas.real*HARTREE2EV, p_ave])
    np.savetxt('test_0161_bse_h2b_spin1_uhf_cis_pyscf.txt', data.T, fmt=['%f','%f'])
    #data_ref = np.loadtxt('test_0159_bse_h2b_uhf_cis_pyscf.txt-ref').T
    #self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3))
    
    nao_td  = bse_iter(mf=gto_mf, gto=mol, verbosity=0, xc_code='CIS')

    polariz = -nao_td.comp_polariz_inter_ave(omegas).imag
    data = np.array([omegas.real*HARTREE2EV, polariz])
    np.savetxt('test_0161_bse_h2b_spin1_uhf_cis_nao.txt', data.T, fmt=['%f','%f'])
    #data_ref = np.loadtxt('test_0161_bse_h2b_spin1_uhf_cis_nao.txt-ref').T
    #self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3)) 
Example 15
Project: pyscf   Author: pyscf   File: test_0157_bse_h2o_rhf_cis.py    License: Apache License 2.0 6 votes vote down vote up
def test_157_bse_h2o_rhf_cis(self):
    """ This  """
    mol = gto.M(verbose=1,atom='O 0 0 0; H 0 0.489 1.074; H 0 0.489 -1.074',basis='cc-pvdz')

    gto_mf = scf.RHF(mol)
    gto_mf.kernel()
    gto_td = tddft.TDDFT(gto_mf)
    gto_td.nstates = 190
    gto_td.kernel()

    omegas = np.arange(0.0, 2.0, 0.01) + 1j*0.03
    p_ave = -polariz_freq_osc_strength(gto_td.e, gto_td.oscillator_strength(), omegas).imag
    data = np.array([omegas.real*HARTREE2EV, p_ave])
    np.savetxt('test_0157_bse_h2o_rhf_cis_pyscf.txt', data.T, fmt=['%f','%f'])
    data_ref = np.loadtxt('test_0157_bse_h2o_rhf_cis_pyscf.txt-ref').T
    self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3))
    
    nao_td  = bse_iter(mf=gto_mf, gto=mol, verbosity=0, xc_code='CIS')

    polariz = -nao_td.comp_polariz_inter_ave(omegas).imag
    data = np.array([omegas.real*HARTREE2EV, polariz])
    np.savetxt('test_0157_bse_h2o_rhf_cis_nao.txt', data.T, fmt=['%f','%f'])
    data_ref = np.loadtxt('test_0157_bse_h2o_rhf_cis_nao.txt-ref').T
    self.assertTrue(np.allclose(data_ref, data, atol=5e-5, rtol=5e-2), 
      msg="{}".format(abs(data_ref-data).sum()/data.size ) ) 
Example 16
Project: pyscf   Author: pyscf   File: test_0035_bse_nonin_nao.py    License: Apache License 2.0 6 votes vote down vote up
def test_bse_iter_nonin(self):
    """ Compute polarization with LDA TDDFT  """
    from timeit import default_timer as timer
    
    dname = os.path.dirname(os.path.abspath(__file__))
    bse = bse_iter(label='water', cd=dname, iter_broadening=1e-2, xc_code='RPA', verbosity=0)
    omegas = np.linspace(0.0,2.0,500)+1j*bse.eps
    
    pxx = np.zeros(len(omegas))
    for iw,omega in enumerate(omegas):
      for ixyz in range(1):
        vab = bse.apply_l0(bse.dip_ab[ixyz], omega)
        pxx[iw] = pxx[iw] - (vab.imag*bse.dip_ab[ixyz].reshape(-1)).sum()
        
    data = np.array([omegas.real*27.2114, pxx])
    np.savetxt('water.bse_iter_rpa.omega.nonin.pxx.txt', data.T, fmt=['%f','%f'])
    data_ref = np.loadtxt(dname+'/water.bse_iter_rpa.omega.nonin.pxx.txt-ref')
    #print('    bse.l0_ncalls ', bse.l0_ncalls)
    self.assertTrue(np.allclose(data_ref,data.T, rtol=1.0, atol=1e-05)) 
Example 17
Project: pyscf   Author: pyscf   File: test_0152_bse_h2b_uks_pz.py    License: Apache License 2.0 6 votes vote down vote up
def test_152_bse_h2b_uks_pz(self):
    """ This  """
    mol = gto.M(verbose=1,atom='B 0 0 0; H 0 0.489 1.074; H 0 0.489 -1.074',basis='cc-pvdz',spin=3)

    gto_mf = scf.UKS(mol)
    gto_mf.kernel()
    gto_td = tddft.TDDFT(gto_mf)
    gto_td.nstates = 190
    gto_td.kernel()

    omegas = np.arange(0.0, 2.0, 0.01) + 1j*0.03
    p_ave = -polariz_freq_osc_strength(gto_td.e, gto_td.oscillator_strength(), omegas).imag
    data = np.array([omegas.real*HARTREE2EV, p_ave])
    np.savetxt('test_0152_bse_h2b_uks_pz_pyscf.txt', data.T, fmt=['%f','%f'])
    data_ref = np.loadtxt('test_0152_bse_h2b_uks_pz_pyscf.txt-ref').T
    self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3))
    
    nao_td  = bse_iter(mf=gto_mf, gto=mol, verbosity=0)

    polariz = -nao_td.comp_polariz_inter_ave(omegas).imag
    data = np.array([omegas.real*HARTREE2EV, polariz])
    np.savetxt('test_0152_bse_h2b_uks_pz_nao.txt', data.T, fmt=['%f','%f'])
    data_ref = np.loadtxt('test_0152_bse_h2b_uks_pz_nao.txt-ref').T
    self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3)) 
Example 18
Project: pyscf   Author: pyscf   File: test_0139_h2o_uks_nonin_pb.py    License: Apache License 2.0 6 votes vote down vote up
def test_139_h2o_uks_nonin_pb(self):
    """ This  """
    mol = gto.M(verbose=1,atom='O 0 0 0; H 0 0.489 1.074; H 0 0.489 -1.074',basis='cc-pvdz')
    gto_mf = dft.UKS(mol)
    gto_mf.kernel()
    nao_mf = bse_iter(gto=mol, mf=gto_mf)
    comega = np.arange(0.0, 2.0, 0.01) + 1j*0.03
    pnonin = -nao_mf.polariz_nonin_ave_matelem(comega).imag
    data = np.array([comega.real*HARTREE2EV, pnonin])
    np.savetxt('test_139_h2o_uks_nonin_matelem.txt', data.T, fmt=['%f','%f'])
    #data_ref = np.loadtxt('test_139_h2o_uks_nonin_matelem.txt-ref').T
    #self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3))

    pnonin = -nao_mf.comp_polariz_nonin_ave(comega).imag
    data = np.array([comega.real*HARTREE2EV, pnonin])
    np.savetxt('test_139_h2o_uks_nonin_nao.txt', data.T, fmt=['%f','%f'])
    #data_ref = np.loadtxt('test_139_h2o_uks_nonin_nao.txt-ref').T
    #self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3)) 
Example 19
Project: pyscf   Author: pyscf   File: test_0146_bse_h2o_rhf_rpa.py    License: Apache License 2.0 6 votes vote down vote up
def test_0146_bse_h2o_rhf_rpa(self):
    """ Interacting case """
    mol=gto.M(verbose=0,atom='O 0 0 0;H 0 0.489 1.074;H 0 0.489 -1.074',basis='cc-pvdz',)
    gto_hf = scf.RKS(mol)
    gto_hf.xc = 'hf'
    gto_hf.kernel()
    gto_td = tddft.dRPA(gto_hf)
    gto_td.nstates = 95
    gto_td.kernel()

    omegas = np.arange(0.0, 2.0, 0.01) + 1j*0.03
    #p_ave = -polariz_inter_ave(gto_hf, mol, gto_td, omegas).imag
    p_ave = -polariz_freq_osc_strength(gto_td.e, gto_td.oscillator_strength(), omegas).imag
    data = np.array([omegas.real*HARTREE2EV, p_ave])
    np.savetxt('test_0146_bse_h2o_rhf_rpa_pyscf.txt', data.T, fmt=['%f','%f'])
    data_ref = np.loadtxt('test_0146_bse_h2o_rhf_rpa_pyscf.txt-ref').T
    self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3))
    
    nao_td  = bse_iter(mf=gto_hf, gto=mol, verbosity=0, xc_code='RPA')

    p_iter = -nao_td.comp_polariz_inter_ave(omegas).imag
    data = np.array([omegas.real*HARTREE2EV, p_iter])
    np.savetxt('test_0146_bse_h2o_rhf_rpa_nao.txt', data.T, fmt=['%f','%f'])
    data_ref = np.loadtxt('test_0146_bse_h2o_rhf_rpa_nao.txt-ref').T
    self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3)) 
Example 20
Project: pyscf   Author: pyscf   File: test_0096_h2_ae_qchem_irf.py    License: Apache License 2.0 6 votes vote down vote up
def test_qchem_irf(self):
    """ Test """
    gto_hf = dft.RKS(mol)
    gto_hf.kernel()
    nocc = mol.nelectron//2
    nmo = gto_hf.mo_energy.size
    nvir = nmo-nocc    
    gto_td = tddft.dRPA(gto_hf)
    gto_td.nstates = min(100, nocc*nvir)
    gto_td.kernel()
    
    gto_gw = GW(gto_hf, gto_td)
    gto_gw.kernel()
    
    nao_td  = tddft_iter(mf=gto_hf, gto=mol, xc_code='RPA')
    eps = 0.02
    omegas = np.arange(0.0,2.0,eps/2.0)+1j*eps
    p_iter = -nao_td.comp_polariz_inter_ave(omegas).imag
    data = np.array([omegas.real*27.2114, p_iter])
    np.savetxt('NH.tddft_iter_rpa.omega.inter.pav.txt', data.T, fmt=['%f','%f'])
    
    np.set_printoptions(linewidth=180)
    #qrf = qchem_inter_rf(mf=gto_hf, gto=mol, pb_algorithm='fp', verbosity=1) 
Example 21
Project: pyscf   Author: pyscf   File: test_0150_bse_h2o_uhf_rpa.py    License: Apache License 2.0 6 votes vote down vote up
def test_0150_bse_h2o_uhf_rpa(self):
    """ Interacting case """
    mol=gto.M(verbose=0,atom='O 0 0 0;H 0 0.489 1.074;H 0 0.489 -1.074',basis='cc-pvdz',)
    gto_mf = scf.UKS(mol)
    gto_mf.xc = 'hf'
    gto_mf.kernel()
    gto_td = tddft.dRPA(gto_mf)
    gto_td.nstates = 95
    gto_td.kernel()

    omegas = np.arange(0.0, 2.0, 0.01) + 1j*0.03
    p_ave = -polariz_freq_osc_strength(gto_td.e, gto_td.oscillator_strength(), omegas).imag
    data = np.array([omegas.real*HARTREE2EV, p_ave])
    np.savetxt('test_0150_bse_h2o_uhf_rpa_pyscf.txt', data.T, fmt=['%f','%f'])
    data_ref = np.loadtxt('test_0150_bse_h2o_uhf_rpa_pyscf.txt-ref').T
    self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3))
    
    nao_td  = bse_iter(mf=gto_mf, gto=mol, verbosity=0, xc_code='RPA')

    polariz = -nao_td.comp_polariz_inter_ave(omegas).imag
    data = np.array([omegas.real*HARTREE2EV, polariz])
    np.savetxt('test_0150_bse_h2o_uhf_rpa_nao.txt', data.T, fmt=['%f','%f'])
    data_ref = np.loadtxt('test_0150_bse_h2o_uhf_rpa_nao.txt-ref').T
    self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3)) 
Example 22
Project: pyscf   Author: pyscf   File: test_0151_bse_h2b_uhf_rpa.py    License: Apache License 2.0 6 votes vote down vote up
def test_151_bse_h2b_uhf_rpa(self):
    """ This  """
    mol = gto.M(verbose=1,atom='B 0 0 0; H 0 0.489 1.074; H 0 0.489 -1.074',basis='cc-pvdz',spin=3)

    gto_mf = scf.UKS(mol)
    gto_mf.xc = 'hf'
    gto_mf.kernel()
    gto_td = tddft.dRPA(gto_mf)
    gto_td.nstates = 190
    gto_td.kernel()

    omegas = np.arange(0.0, 2.0, 0.01) + 1j*0.03
    p_ave = -polariz_freq_osc_strength(gto_td.e, gto_td.oscillator_strength(), omegas).imag
    data = np.array([omegas.real*HARTREE2EV, p_ave])
    np.savetxt('test_0151_bse_h2b_uhf_rpa_pyscf.txt', data.T, fmt=['%f','%f'])
    data_ref = np.loadtxt('test_0151_bse_h2b_uhf_rpa_pyscf.txt-ref').T
    self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3))
    
    nao_td  = bse_iter(mf=gto_mf, gto=mol, verbosity=0, xc_code='RPA')

    polariz = -nao_td.comp_polariz_inter_ave(omegas).imag
    data = np.array([omegas.real*HARTREE2EV, polariz])
    np.savetxt('test_0151_bse_h2b_uhf_rpa_nao.txt', data.T, fmt=['%f','%f'])
    data_ref = np.loadtxt('test_0151_bse_h2b_uhf_rpa_nao.txt-ref').T
    self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3)) 
Example 23
Project: pyscf   Author: pyscf   File: test_0013_gpaw_overlap_nao.py    License: Apache License 2.0 6 votes vote down vote up
def test_sv_after_gpaw(self):
    """ init ao_log with radial orbitals from GPAW """
    if calc is None: return

    self.assertTrue(hasattr(calc, 'setups'))
    sv = mf(gpaw=calc, gen_pb=False)
    self.assertEqual(sv.ao_log.nr, 1024)
    over = sv.overlap_coo().toarray()
    error = sv.hsx.check_overlaps(over)
    self.assertLess(error, 1e-4)
    #print("overlap error: ", error/over.size)

    #print("Pyscf    gpaw")
    #for py, gp in zip(over[:, 0], sv.wfsx.overlaps[:, 0]):
    #    print("{0:.5f}    {1:.5f}".format(py, gp))
    #np.savetxt("Pyscf.overlaps", over)
    #np.savetxt("gpaw.overlaps", sv.wfsx.overlaps,) 
Example 24
Project: TensorFlow-TransX   Author: thunlp   File: transR.py    License: MIT License 5 votes vote down vote up
def __init__(self):
		lib.setInPath("./data/FB15K/")
		test_lib.setInPath("./data/FB15K/")
		lib.setBernFlag(0)
		self.learning_rate = 0.0001
		self.testFlag = False
		self.loadFromData = False
		self.L1_flag = True
		self.hidden_sizeE = 100
		self.hidden_sizeR = 100
		self.nbatches = 100
		self.entity = 0
		self.relation = 0
		self.trainTimes = 1000
		self.margin = 1.0
		'''
		In the original paper, TransR is trained with the pre-trained embeddings as parameter initialization.
		If you do not want to train with the pre-trained embeddings, you can use the following code instead of the default version.
		You need to use np.savetxt() to store the pre-trained embeddings into the corresponding file and input the file's name.
		e.g.
			self.ent_init = "ent_embeddings.txt"
			self.rel_init = "rel_embeddings.txt"
		where entity and relation embeddings are stored into the "ent_embeddings.txt" and "rel_embeddings.txt" respectively.
		'''
		self.rel_init = None
		self.ent_init = None 
Example 25
def sample_normalize(self, k_samples=1000, overwrite=False):
        """ Estimate the mean and std of the features from the training set
        Params:
            k_samples (int): Use this number of samples for estimation
        """
        log = LogUtil().getlogger()
        log.info("Calculating mean and std from samples")
        # if k_samples is negative then it goes through total dataset
        if k_samples < 0:
            audio_paths = self.audio_paths

        # using sample
        else:
            k_samples = min(k_samples, len(self.train_audio_paths))
            samples = self.rng.sample(self.train_audio_paths, k_samples)
            audio_paths = samples
        manager = Manager()
        return_dict = manager.dict()
        jobs = []
        for threadIndex in range(cpu_count()):
            proc = Process(target=self.preprocess_sample_normalize, args=(threadIndex, audio_paths, overwrite, return_dict))
            jobs.append(proc)
            proc.start()
        for proc in jobs:
            proc.join()

        feat = np.sum(np.vstack([item['feat'] for item in return_dict.values()]), axis=0)
        count = sum([item['count'] for item in return_dict.values()])
        feat_squared = np.sum(np.vstack([item['feat_squared'] for item in return_dict.values()]), axis=0)

        self.feats_mean = feat / float(count)
        self.feats_std = np.sqrt(feat_squared / float(count) - np.square(self.feats_mean))
        np.savetxt(
            generate_file_path(self.save_dir, self.model_name, 'feats_mean'), self.feats_mean)
        np.savetxt(
            generate_file_path(self.save_dir, self.model_name, 'feats_std'), self.feats_std)
        log.info("End calculating mean and std from samples") 
Example 26
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: stt_utils.py    License: Apache License 2.0 5 votes vote down vote up
def spectrogram_from_file(filename, step=10, window=20, max_freq=None,
                          eps=1e-14, overwrite=False, save_feature_as_csvfile=False):
    """ Calculate the log of linear spectrogram from FFT energy
    Params:
        filename (str): Path to the audio file
        step (int): Step size in milliseconds between windows
        window (int): FFT window size in milliseconds
        max_freq (int): Only FFT bins corresponding to frequencies between
            [0, max_freq] are returned
        eps (float): Small value to ensure numerical stability (for ln(x))
    """

    csvfilename = filename.replace(".wav", ".csv")
    if (os.path.isfile(csvfilename) is False) or overwrite:
        with soundfile.SoundFile(filename) as sound_file:
            audio = sound_file.read(dtype='float32')
            sample_rate = sound_file.samplerate
            if audio.ndim >= 2:
                audio = np.mean(audio, 1)
            if max_freq is None:
                max_freq = sample_rate / 2
            if max_freq > sample_rate / 2:
                raise ValueError("max_freq must not be greater than half of "
                                 " sample rate")
            if step > window:
                raise ValueError("step size must not be greater than window size")
            hop_length = int(0.001 * step * sample_rate)
            fft_length = int(0.001 * window * sample_rate)

            pxx, freqs = spectrogram(
                audio, fft_length=fft_length, sample_rate=sample_rate,
                hop_length=hop_length)

            ind = np.where(freqs <= max_freq)[0][-1] + 1
            res = np.transpose(np.log(pxx[:ind, :] + eps))
            if save_feature_as_csvfile:
                np.savetxt(csvfilename, res)
            return res
    else:
        return np.loadtxt(csvfilename) 
Example 27
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: Train.py    License: Apache License 2.0 5 votes vote down vote up
def encode_csv(label_csv, systole_csv, diastole_csv):
    systole_encode, diastole_encode = encode_label(np.loadtxt(label_csv, delimiter=","))
    np.savetxt(systole_csv, systole_encode, delimiter=",", fmt="%g")
    np.savetxt(diastole_csv, diastole_encode, delimiter=",", fmt="%g")

# Write encoded label into the target csv
# We use CSV so that not all data need to sit into memory
# You can also use inmemory numpy array if your machine is large enough 
Example 28
Project: ArtGAN   Author: cs-chan   File: ingest_stl10.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def write_label(self, ):
        for i, l in enumerate(self.labels):
            sdir = os.path.join(self.outlabeldir, str(i) + '.txt')
            np.savetxt(sdir, [l], '%d') 
Example 29
Project: ArtGAN   Author: cs-chan   File: ingest_stl10.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def run(self):
        """
        resize images then write manifest files to disk.
        """
        self.write_label()
        self.collectdata()

        records = [(fname, tgt)
                   for fname, tgt in self.trainpairlist.items()]
        np.savetxt(self.manifests['train'], records, fmt='%s,%s')

        records = [(fname, tgt)
                   for fname, tgt in self.valpairlist.items()]
        np.savetxt(self.manifests['val'], records, fmt='%s,%s') 
Example 30
Project: ArtGAN   Author: cs-chan   File: ingest_stl10.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def write_label(self, ):
        sdir = os.path.join(self.out_dir, 'labels', '11.txt')
        np.savetxt(sdir, [11], '%d')