Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

inspect-idgraph.py 3.6 KB

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

Press p or to see the previous file or, n or to see the next file

Comments

Loading...