-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_chromadb_to_text.py
More file actions
318 lines (246 loc) Β· 11.7 KB
/
export_chromadb_to_text.py
File metadata and controls
318 lines (246 loc) Β· 11.7 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
"""
Export ChromaDB Data to Text File
=================================
This script connects to ChromaDB, retrieves all data from the float_embeddings collection,
and saves it to a comprehensive text file with proper formatting.
Author: FloatChat Data Pipeline
"""
import os
import json
from datetime import datetime
from pathlib import Path
from typing import Dict, Any, List
import sys
# Add project root to path
project_root = Path(__file__).parent
sys.path.append(str(project_root))
from dotenv import load_dotenv
from ingest.db_handler import ChromaDBHandler
# Load environment variables
env_file = Path(__file__).parent / '.env'
if env_file.exists():
load_dotenv(env_file)
def format_metadata(metadata: Dict[str, Any]) -> str:
"""Format metadata dictionary into readable text."""
formatted_lines = []
# Essential information first
if 'float_id' in metadata:
formatted_lines.append(f"Float ID: {metadata['float_id']}")
if 'source' in metadata:
formatted_lines.append(f"Source: {metadata['source']}")
if 'data_type' in metadata:
formatted_lines.append(f"Data Type: {metadata['data_type']}")
# Location information
location_fields = ['lat_min', 'lat_max', 'lon_min', 'lon_max']
location_data = {k: v for k, v in metadata.items() if k in location_fields}
if location_data:
formatted_lines.append("Location Range:")
for key, value in location_data.items():
formatted_lines.append(f" {key}: {value}")
# Measurement flags
measurement_flags = ['has_temperature', 'has_salinity', 'has_pressure']
measurements = {k: v for k, v in metadata.items() if k in measurement_flags}
if measurements:
formatted_lines.append("Available Measurements:")
for key, value in measurements.items():
measurement_type = key.replace('has_', '').title()
formatted_lines.append(f" {measurement_type}: {'Yes' if value else 'No'}")
# Other metadata
other_fields = [k for k in metadata.keys() if k not in
['float_id', 'source', 'data_type'] + location_fields + measurement_flags]
if other_fields:
formatted_lines.append("Additional Information:")
for key in other_fields:
value = metadata[key]
if isinstance(value, (dict, list)):
formatted_lines.append(f" {key}: {json.dumps(value, indent=4)}")
else:
formatted_lines.append(f" {key}: {value}")
return '\n'.join(formatted_lines)
def export_chromadb_to_text(output_filename: str = None) -> str:
"""
Export all ChromaDB data to a text file.
Args:
output_filename: Name of the output file (optional)
Returns:
Path to the created file
"""
# Generate filename if not provided
if not output_filename:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
output_filename = f"chromadb_export_{timestamp}.txt"
output_path = Path(__file__).parent / output_filename
try:
print("π Connecting to ChromaDB...")
# Connect to ChromaDB
chroma = ChromaDBHandler()
# Get the float_embeddings collection
collection = chroma.client.get_collection("float_embeddings")
print(f"β Connected to collection: float_embeddings")
# Get all documents with metadata
print("π₯ Retrieving all data from ChromaDB...")
results = collection.get(include=["documents", "metadatas"])
# Get IDs separately if needed
all_results = collection.get()
document_ids = all_results['ids'] if 'ids' in all_results else []
total_documents = len(results['documents'])
print(f"β Found {total_documents} documents")
if total_documents == 0:
print("β οΈ No data found in ChromaDB collection")
return None
# Write to text file
print(f"π Writing data to {output_path}...")
with open(output_path, 'w', encoding='utf-8') as f:
# Write header
f.write("=" * 100 + "\n")
f.write("CHROMADB DATA EXPORT - FLOAT EMBEDDINGS COLLECTION\n")
f.write("=" * 100 + "\n")
f.write(f"Export Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
f.write(f"Collection: float_embeddings\n")
f.write(f"Total Documents: {total_documents}\n")
f.write("=" * 100 + "\n\n")
# Write each document
for i, (document, metadata) in enumerate(zip(
results['documents'],
results['metadatas']
)):
# Get document ID if available
doc_id = document_ids[i] if i < len(document_ids) else f"doc_{i+1}"
# Document header
f.write(f"DOCUMENT #{i+1}\n")
f.write("-" * 80 + "\n")
f.write(f"Document ID: {doc_id}\n")
f.write("-" * 40 + "\n")
# Metadata section
f.write("METADATA:\n")
f.write(format_metadata(metadata))
f.write("\n" + "-" * 40 + "\n")
# Document content
f.write("DOCUMENT CONTENT:\n")
# Try to parse as JSON for pretty printing
try:
parsed_json = json.loads(document)
f.write(json.dumps(parsed_json, indent=2, ensure_ascii=False))
except (json.JSONDecodeError, TypeError):
# If not JSON, write as plain text
f.write(document)
f.write("\n\n" + "=" * 100 + "\n\n")
# Progress indicator
if (i + 1) % 10 == 0 or (i + 1) == total_documents:
print(f" Progress: {i+1}/{total_documents} documents written")
# Close ChromaDB connection
chroma.close()
print(f"β
Export completed successfully!")
print(f"π File saved: {output_path}")
print(f"π Total documents exported: {total_documents}")
# File size info
file_size = output_path.stat().st_size
if file_size < 1024:
size_str = f"{file_size} bytes"
elif file_size < 1024 * 1024:
size_str = f"{file_size / 1024:.1f} KB"
else:
size_str = f"{file_size / (1024 * 1024):.1f} MB"
print(f"π File size: {size_str}")
return str(output_path)
except Exception as e:
print(f"β Export failed: {e}")
raise
def export_summary_statistics(chroma_handler: ChromaDBHandler) -> Dict[str, Any]:
"""Generate summary statistics about the ChromaDB collection."""
try:
collection = chroma_handler.client.get_collection("float_embeddings")
results = collection.get(include=["metadatas"])
stats = {
"total_documents": len(results['metadatas']),
"sources": {},
"data_types": {},
"measurement_availability": {
"temperature": 0,
"salinity": 0,
"pressure": 0
},
"location_bounds": {
"lat_min": float('inf'),
"lat_max": float('-inf'),
"lon_min": float('inf'),
"lon_max": float('-inf')
}
}
for metadata in results['metadatas']:
# Count sources
source = metadata.get('source', 'unknown')
stats['sources'][source] = stats['sources'].get(source, 0) + 1
# Count data types
data_type = metadata.get('data_type', 'unknown')
stats['data_types'][data_type] = stats['data_types'].get(data_type, 0) + 1
# Count measurements
for measurement in ['temperature', 'salinity', 'pressure']:
if metadata.get(f'has_{measurement}', False):
stats['measurement_availability'][measurement] += 1
# Update location bounds
for bound in ['lat_min', 'lat_max', 'lon_min', 'lon_max']:
if bound in metadata and isinstance(metadata[bound], (int, float)):
value = float(metadata[bound])
if 'min' in bound:
stats['location_bounds'][bound] = min(stats['location_bounds'][bound], value)
else:
stats['location_bounds'][bound] = max(stats['location_bounds'][bound], value)
return stats
except Exception as e:
print(f"Error generating statistics: {e}")
return {}
def main():
"""Main function to run the export."""
try:
print("π ChromaDB Data Export Tool")
print("=" * 50)
# Ask user for filename
default_filename = f"chromadb_export_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
print(f"\nDefault filename: {default_filename}")
user_filename = input("Enter custom filename (or press Enter for default): ").strip()
filename = user_filename if user_filename else default_filename
# Ensure .txt extension
if not filename.endswith('.txt'):
filename += '.txt'
print(f"\nπ Export filename: {filename}")
# Perform export
exported_file = export_chromadb_to_text(filename)
if exported_file:
print(f"\nπ Export completed successfully!")
print(f"π File location: {exported_file}")
# Ask if user wants to view summary statistics
show_stats = input("\nWould you like to see summary statistics? (y/n): ").strip().lower()
if show_stats in ['y', 'yes']:
print("\nπ Generating summary statistics...")
chroma = ChromaDBHandler()
stats = export_summary_statistics(chroma)
chroma.close()
if stats:
print("\n" + "=" * 50)
print("SUMMARY STATISTICS")
print("=" * 50)
print(f"Total Documents: {stats['total_documents']}")
print(f"\nData Sources:")
for source, count in stats['sources'].items():
print(f" {source}: {count}")
print(f"\nData Types:")
for dtype, count in stats['data_types'].items():
print(f" {dtype}: {count}")
print(f"\nMeasurement Availability:")
for measurement, count in stats['measurement_availability'].items():
percentage = (count / stats['total_documents']) * 100
print(f" {measurement.title()}: {count} ({percentage:.1f}%)")
bounds = stats['location_bounds']
if bounds['lat_min'] != float('inf'):
print(f"\nLocation Coverage:")
print(f" Latitude: {bounds['lat_min']:.3f}Β° to {bounds['lat_max']:.3f}Β°")
print(f" Longitude: {bounds['lon_min']:.3f}Β° to {bounds['lon_max']:.3f}Β°")
else:
print("β Export failed or no data found")
except KeyboardInterrupt:
print("\nβ οΈ Export cancelled by user")
except Exception as e:
print(f"\nβ Export failed: {e}")
if __name__ == "__main__":
main()