1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
- """
- Inspect a book cluster.
- Usage:
- inspect-idgraph.py [options] --stats
- inspect-idgraph.py [options] --records CLUSTER
- inspect-idgraph.py [options] --graph CLUSTER
- inspect-idgraph.py [options] --full-graph
- Options:
- -o FILE
- Write output to FILE
- -f, --format FMT
- Output in format FMT.
- CLUSTER
- The cluster number to inspect.
- """
- import sys
- import re
- import json
- from xml.etree import ElementTree as etree
- from textwrap import dedent as d
- from docopt import docopt
- import pandas as pd
- from bookdata import tracking, db, script_log
- from bookdata.graph import GraphLoader
- def stats(dbc, out, opts):
- "Compute statistics of the clustering"
- with dbc.cursor() as cur:
- _log.info('getting aggregate stats')
- cur.execute('SELECT COUNT(*), MAX(isbns) FROM cluster_stats')
- n_clusters, m_isbns = cur.fetchone()
- print(f'Clusters: {n_clusters}', file=out)
- print(f'Largest has {m_isbns} ISBNs', file=out)
- _log.info('computing top stats')
- print('Top clusters by size:', file=out)
- top = pd.read_sql('SELECT * FROM cluster_stats ORDER BY isbns DESC LIMIT 10', dbc)
- print(top.fillna(0), file=out)
- def records(dbc, out, opts):
- "Dump ISBN records from a cluster to a CSV file"
- cluster = opts['CLUSTER']
- bc_recs = []
- _log.info('inspecting cluster %s', cluster)
- _log.info('fetching LOC records')
- bc_recs.append(pd.read_sql(f'''
- SELECT isbn, 'LOC' AS source, rec_id AS record, NULL AS work, title
- FROM locmds.book_rec_isbn
- JOIN isbn_id USING (isbn_id)
- JOIN isbn_cluster USING (isbn_id)
- LEFT JOIN locmds.book_title USING (rec_id)
- WHERE cluster = {cluster}
- ''', dbc))
- _log.info('fetching OL records')
- bc_recs.append(pd.read_sql(f'''
- SELECT isbn, 'OL' AS source,
- edition_id AS record, work_id AS work,
- title
- FROM ol.isbn_link
- JOIN isbn_id USING (isbn_id)
- JOIN isbn_cluster USING (isbn_id)
- LEFT JOIN ol.edition_title USING (edition_id)
- WHERE cluster = {cluster}
- ''', dbc))
- _log.info('fetching GR records')
- bc_recs.append(pd.read_sql(f'''
- SELECT isbn, 'GR' AS source,
- gr_book_id AS record, gr_work_id AS work,
- work_title
- FROM gr.book_isbn
- JOIN isbn_id USING (isbn_id)
- JOIN isbn_cluster USING (isbn_id)
- JOIN gr.book_ids USING (gr_book_id)
- LEFT JOIN gr.work_title USING (gr_work_id)
- WHERE cluster = {cluster}
- ''', dbc))
- bc_recs = pd.concat(bc_recs, ignore_index=True)
- bc_recs.sort_values('isbn', inplace=True)
- _log.info('fetched %d records', len(bc_recs))
- bc_recs.to_csv(out, index=False)
- def graph(opts):
- cluster = opts['CLUSTER']
- _log.info('exporting graph for cluster %s', cluster)
- gl = GraphLoader()
- with db.engine().connect() as cxn:
- gl.set_cluster(cluster, cxn)
- g = gl.load_graph(cxn, True)
- ofn = opts['-o']
- _log.info('saving graph to %s', ofn)
- g.save(ofn)
- def full_graph(opts):
- gl = GraphLoader()
- with db.engine().connect() as cxn:
- g = gl.load_minimal_graph(cxn)
- ofn = opts['-o']
- _log.info('saving graph to %s', ofn)
- g.save(ofn)
- _log = script_log(__name__)
- opts = docopt(__doc__)
- if opts['--full-graph']:
- full_graph(opts)
- elif opts['--graph']:
- graph(opts)
- else:
- if opts['-o']:
- out = open(opts['-o'], 'w', encoding='utf8')
- else:
- out = sys.stdout
- with db.connect() as dbc:
- if opts['--stats']:
- stats(dbc, out, opts)
- elif opts['--records']:
- records(dbc, out, opts)
|