Ejemplo n.º 1
0
def main():
    from argparse import ArgumentParser, RawDescriptionHelpFormatter
    headerhelp = \
'''
MTZ2NPY converts an mtz-file to numpy readable npy file.
'''
    parser = ArgumentParser(formatter_class=RawDescriptionHelpFormatter,
                            description=headerhelp)
    parser.add_argument('inpath', help='The input MTZ file.')
    parser.add_argument('outpath', help='The output NPY file.')
    parser.add_argument('-z',
                        '--gzipped',
                        action='store_true',
                        help='Compress the output.')
    parser.add_argument('-f',
                        '--overwrite',
                        action='store_true',
                        help='Overwirte existing files.')

    args = parser.parse_args()

    from pymtz import read_mtz_file

    print("Reading ", args.inpath, end='...', flush=True)
    dataset = read_mtz_file(args.inpath)
    print("done")
    print("Writing ", args.outpath, end='...', flush=True)
    dataset.savenpy(args.outpath,
                    fOverwrite=args.overwrite,
                    fCompressed=args.gzipped)
    print("done")
Ejemplo n.º 2
0
def rotate_freeflag(args):
    mtz = read_mtz_file(args.mtzin)
    avlabels = mtz.GetLabels()
    if args.free not in avlabels:
        print('Column '+args.free+' not found. Check input mtz-file and test set label selection.')
        print('Available columns:')
        print(' '.join(avlabels))
        return False
    sys.stdout.write('Rotating the free flag column by %d... ' % (args.free_shift))
    mtz.RotateFreeFlag(args.free, args.free_shift)
    sys.stdout.write('done\n')
    utils.mtzsave(mtz, args)
Ejemplo n.º 3
0
def rcompute(args):
    mtz = read_mtz_file(args.mtzin)
    if not args.dhigh:
        args.dhigh = mtz.GetHighD()
    avlabels = mtz.GetLabels()
    if args.free not in avlabels:
        print('Column '+args.free+' not found. Check input mtz-file and test set label selection.')
        print('Available columns:')
        print(' '.join(avlabels))
    if args.fobs not in avlabels:
        print('Column '+args.fobs+' not found. Check input mtz-file and Fobs label selection.')
        print('Available columns:')
        print(' '.join(avlabels))
    if args.fcalc not in avlabels:
        print('Column '+args.fcalc+' not found. Check input mtz-file and Fcalc label selection.')
        print('Available columns:')
        print(' '.join(avlabels))
    else:
        d = array(mtz.GetResolutionColumn())
        fp, fc, sigf = mtz.GetReflectionColumns([args.fobs, args.fcalc, args.sigfobs])
        test_index = mtz.GetTestIndex(args.free, True)
        work_index = mtz.GetWorkIndex(args.free, True)
        ind = (d >= args.dhigh) * isfinite(fp)
        test_index = test_index[ind]
        work_index = work_index[ind]
        fp, fc, sigf = fp[ind], fc[ind], sigf[ind]
        fpfc = fp-fc
        sys.stdout.write('%9s %9s %9s %9s\n' % ('R','RWORK','RFREE','REXP'))
        sys.stdout.write('%9.3f '  % (100*abs(fpfc).sum()/fp.sum()))
        sys.stdout.write('%9.3f '  % (100*abs(fpfc[work_index]).sum()/fp[work_index].sum()))
        sys.stdout.write('%9.3f ' % (100*abs(fpfc[test_index]).sum()/fp[test_index].sum()))
        sys.stdout.write('%9.3f\n '  % (79.8*sigf.sum()/fp.sum()))
        dR=(abs(fpfc)*fp.sum()-abs(fpfc).sum()*fp)/((fp.sum())**2-fp*fp.sum())
        indr = argsort(dR)
        Rc = 100*cumsum(abs(fpfc[indr]))/cumsum(fp[indr])
        Rce = 79.8*cumsum(sigf[indr])/cumsum(fp[indr])
        plot(Rc-Rce,'r')
        grid()
        show()
Ejemplo n.º 4
0
def scale_columns(args):
    mtz = read_mtz_file(args.mtzin)
    for label in args.cols.split(','):
        mtz.ScaleColumn(label, args.scale)
    utils.mtzsave(mtz, args)
Ejemplo n.º 5
0
def info(args):
    mtz = read_mtz_file(args.mtzin)
    print("Columns: "+','.join(mtz.GetLabels()))
    print("Resolution range: ",mtz.GetLowD(),mtz.GetHighD())
Ejemplo n.º 6
0
def filter_columns(args):
    mtz = read_mtz_file(args.mtzin)
    for label in set(mtz.GetLabels()).difference(args.cols.split(','), ['H','K','L']):
        mtz.DeleteColumn(label)
    utils.mtzsave(mtz, args)
Ejemplo n.º 7
0
def testset(args):
    mtz = read_mtz_file(args.mtzin)
    sys.stdout.write('Generating the free flag column (%.1f%%)... ' % (100*args.free_fraction))
    mtz.GenerateFreeFlag(args.free, args.free_fraction)
    sys.stdout.write('done\n')
    utils.mtzsave(mtz, args)
Ejemplo n.º 8
0
def main():
    from argparse import ArgumentParser, RawDescriptionHelpFormatter
    headerhelp = \
'''
'''
    parser = ArgumentParser(formatter_class=RawDescriptionHelpFormatter,
                            description=headerhelp)
    parser.add_argument('inpath', help='The input mTZ file.')
    parser.add_argument('-n',
                        '--nobs-per-shell',
                        type=int,
                        default=1000,
                        help='Number of observations per shaell target.')

    args = parser.parse_args()

    from pymtz import read_mtz_file
    from scipy import random, corrcoef, array, nonzero, zeros, ones
    from scipy.stats import spearmanr
    from matplotlib.pyplot import figure, show

    print("Reading ", args.inpath, end='...', flush=True)
    dataset = read_mtz_file(args.inpath)
    print("done")
    Nmeas = dataset.GetReflectionNumber()
    print("Found %d measurements" % Nmeas)
    ashes = dataset.GetHKLhash()
    uniqhkl = set(ashes)
    Nunq = len(uniqhkl)
    Nshells = int(Nunq / args.nobs_per_shell)
    Ihl, shl = array(dataset.reflections).T[nonzero([
        (t in ['I', 'SIGI']) for t in dataset.GetLabels()
    ])[0]]
    whl = 1 / shl**2
    hkldict = dict.fromkeys(uniqhkl)
    for (i, ash) in enumerate(ashes):
        if hkldict[ash]:
            hkldict[ash].append(i)
        else:
            hkldict[ash] = [i]
    print("Found %d unique reflections" % (Nunq))
    print("Will subdivide data into %d resolution shells" % Nshells)
    edges = dataset.GetResolutionShells(Nshells)
    shell_column = dataset.GetShellColumn(edges)
    d12, cc12, sp12, p12, Ish = [], [], [], [], []
    for i in range(Nshells):
        Ish1, Ish2, shkl = [], [], []
        shind = (shell_column == i)
        for ash in set(ashes[shind]):
            mcity = len(hkldict[ash])
            if mcity > 1:
                Nhalf = int(mcity / 2)
                wr = list(ones(Nhalf)) + list(zeros(Nhalf))
                if len(wr) < mcity:
                    wr.append(random.random_integers(0, 1))
                wr = random.permutation(wr)
                Ish1.append(
                    sum(wr * Ihl[hkldict[ash]] * whl[hkldict[ash]]) /
                    sum(wr * whl[hkldict[ash]]))
                Ish2.append(
                    sum((1 - wr) * Ihl[hkldict[ash]] * whl[hkldict[ash]]) /
                    sum((1 - wr) * whl[hkldict[ash]]))
                shkl.append(dataset.hash2hkl(ash))
        cc12.append(corrcoef(Ish1, Ish2)[0][1])
        sp12t, p12t = spearmanr(Ish1, Ish2)
        sp12.append(sp12t)
        p12.append(p12t)
        Ish.append([Ish1, Ish2, shkl])
        d12.append(dataset.GetResolutionColumn()[shind].mean())
        print("%4d %7.2f %7.2f %7.2f %8d %5.3f %5.3f %6.3g" %
              (i + 1, edges[i], edges[i + 1], d12[-1], sum(shind), cc12[-1],
               sp12t, p12t))
    Ish_fig = figure(FigureClass=KDWindow)
    Ish_fig.set_data([Ish, array(d12), cc12, sp12])
    Ish_fig.plot()
    Ish_fig.canvas.mpl_connect('key_press_event', Ish_fig.onkeypress)
    show()