Passed
Push — master ( 2f6156...9bb7bb )
by Daniel
02:58 queued 12s
created

amd.cli   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 66
dl 0
loc 88
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B main() 0 76 6
1
"""The command line interface for average-minimum-distance.
2
"""
3
4
import argparse
5
from .compare import compare
6
7
8
def main():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
9
10
    parser = argparse.ArgumentParser(description='Compare crystals by PDD or AMD from the command line.')
11
12
    parser.add_argument('path_or_refcodes', type=str, nargs='+',
13
        help='path to a file or folder, or collection of refcodes (csd-python-api only).')
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 16 spaces).
Loading history...
14
    parser.add_argument('--output', '-o', type=str, default='output', 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
15
        help='name of the output file.')
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 16 spaces).
Loading history...
16
    parser.add_argument('--format', '-f', type=str, default='csv', 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
17
        help='format of the output file, default csv.')
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 16 spaces).
Loading history...
18
19
    # args of amd.compare
20
    parser.add_argument('--by', '-b', type=str, default='AMD', choices=['AMD', 'PDD'],
21
        help='whether to use AMD or PDD to compare crystals.')
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 16 spaces).
Loading history...
22
    parser.add_argument('--k', '-k', type=int, default=100, help='k value to use for AMD/PDD.')
23
24
    # reading args
25
    parser.add_argument('--reader', '-r', type=str, default='ase', 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
26
        choices=['ase', 'ccdc', 'pycodcif'],
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 16 spaces).
Loading history...
27
        help='backend package used for parsing files, default ase.')
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 16 spaces).
Loading history...
28
    parser.add_argument('--remove_hydrogens', default=False, action='store_true',
29
        help='remove Hydrogen atoms from the crystals.')
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 16 spaces).
Loading history...
30
    parser.add_argument('--disorder', type=str, default='skip', 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
31
        choices=['skip', 'ordered_sites', 'all_sites'],
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 16 spaces).
Loading history...
32
        help='control how disordered structures are handled.')
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 16 spaces).
Loading history...
33
    parser.add_argument('--heaviest_component', default=False, action='store_true',
34
        help='remove all but the heaviest molecule in the asymmetric unit, intended for removing solvents (csd-python-api only).')
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 16 spaces).
Loading history...
35
    parser.add_argument('--molecular_centres', default=False, action='store_true',
36
        help='uses the centres of molecules for comparisons instead of atoms (csd-python-api only).')
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 16 spaces).
Loading history...
37
    parser.add_argument('--supress_warnings', default=False, action='store_true',
38
        help='do not show warnings encountered during reading.')
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 16 spaces).
Loading history...
39
    parser.add_argument('--families', default=False, action='store_true',
40
        help='interpret strings given as refcode families (csd-python-api only).')
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 16 spaces).
Loading history...
41
42
    # pdd args
43
    parser.add_argument('--collapse_tol', type=float, default=1e-4,
44
        help='tolerance for collapsing rows of PDDs.')
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 16 spaces).
Loading history...
45
46
    # compare args
47
    parser.add_argument('--metric', type=str, default='chebyshev',
48
        help='metric used to compare AMDs/rows of PDDs.')
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 16 spaces).
Loading history...
49
    parser.add_argument('--n_jobs', type=int, default=1,
50
        help='number of cores to use for multiprocessing.')
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 16 spaces).
Loading history...
51
    parser.add_argument('--verbose', type=int, default=0,
52
        help='tolerance for collapsing rows of PDDs.')
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 16 spaces).
Loading history...
53
    parser.add_argument('--low_memory', default=False, action='store_true',
54
        help='use an alternative algorithm with lower memory usage.')
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 16 spaces).
Loading history...
55
56
    args = parser.parse_args()
57
    kwargs = vars(args)
58
    path_or_refcodes = kwargs.pop('path_or_refcodes')
59
    outpath = kwargs.pop('output', 'output')
60
    ext = kwargs.pop('format', 'csv')
61
    kwargs['show_warnings'] = not kwargs['supress_warnings']
62
    kwargs.pop('supress_warnings', None)
63
64
    crystals = path_or_refcodes[0]
65
    crystals_ = None
66
    if len(path_or_refcodes) > 2:
0 ignored issues
show
Unused Code introduced by
Unnecessary "elif" after "raise"
Loading history...
67
        raise ValueError('amd.compare: one or two collections of crystals are allowed for comparison.')
68
    elif len(path_or_refcodes) == 2:
69
        crystals_ = path_or_refcodes[1]
70
71
    df = compare(crystals, crystals_, **kwargs)
72
    if kwargs['verbose']:
73
        print(df)
74
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
75
    if not outpath.endswith('.' + ext):
76
        outpath += '.' + ext
77
78
    try:
79
        output_func = getattr(df, 'to_' + ext)
80
        output_func(outpath)
81
    except AttributeError:
82
        print(f'Cannot output format {ext}, using csv instead.')
83
        df.to_csv(outpath + '.csv')
84
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
85
86
if __name__ == '__main__':
87
    main()
0 ignored issues
show
Coding Style introduced by
Final newline missing
Loading history...