-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsvs.py
386 lines (342 loc) · 14.1 KB
/
csvs.py
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import calendar
import gzip
from abc import ABC
from os.path import basename, join
from shutil import copyfileobj, move
from tempfile import TemporaryDirectory
from typing import Optional, Union, Iterator, IO, Tuple, Literal, TypeVar, Generic
from zipfile import ZipFile, BadZipFile
import pandas as pd
from click import argument, option
from utz import cached_property, Unset, err, singleton
from utz.gzip import DeterministicGzipFile, deterministic_gzip_open
from utz.ym import YM
from ctbk.has_root_cli import HasRootCLI, yms_arg
from ctbk.table import Table
from ctbk.task import Task
from ctbk.util.constants import BKT
from ctbk.util.df import DataFrame
from ctbk.util.read import Read
from ctbk.util.region import REGIONS, Region, region
from ctbk.zips import TripdataZips, TripdataZip
DIR = f'{BKT}/csvs'
NameType = Literal[1, 11, 2, 22, None]
T = TypeVar("T")
class ReadsTripdataZip(Task[T], ABC, Generic[T]):
def __init__(
self,
ym: YM,
region: Region,
name_type: NameType = None,
**kwargs,
):
if region not in REGIONS:
raise ValueError(f"Unrecognized region: {region}")
self.ym = ym
self.region = region
self.name_type = name_type
super().__init__(**kwargs)
@cached_property
def src(self) -> TripdataZip:
return TripdataZip(ym=self.ym, region=self.region, roots=self.roots)
DEFAULT_COMPRESSION_LEVEL = 9
READ_DTYPES = {
**{
c: str
for c in [
'start station id', 'end station id',
'Start Station ID', 'End Station ID',
'start_station_id', 'end_station_id',
]
},
'birth year': 'Int16',
'Birth Year': 'Int16',
'gender': 'Int8',
'Gender': 'Int8',
}
READ_KWARGS = dict(
dtype=READ_DTYPES,
na_values=r'\N',
float_precision='round_trip',
)
class TripdataCsv(ReadsTripdataZip[DataFrame], Table):
DIR = DIR
NAMES = [ 'csv', 'c', ]
@property
def region_str(self) -> str:
return 'JC-' if self.region == 'JC' else ''
@property
def name(self):
return f'{self.region_str}{self.ym}'
@property
def basename(self) -> str:
return f'{self.name}-citibike-tripdata.csv.gz'
@cached_property
def url(self) -> str:
return f'{self.dir}/{self.basename}'
@classmethod
def open_path(cls, path: str, mode: str, compression_level: int = DEFAULT_COMPRESSION_LEVEL):
if path.endswith('.gz'):
if mode.startswith("r"):
return gzip.open(path, mode)
elif mode.startswith("w"):
return deterministic_gzip_open(
path=path,
mode=mode,
compression_level=compression_level,
)
else:
raise ValueError(f"Unsupported mode: {mode}")
else:
return open(path, mode)
@classmethod
def sort_and_write(cls, name, in_path, out_fd):
df = pd.read_csv(in_path, **READ_KWARGS)
df.index.name = "lineno"
df = df.reset_index()
path_cols = TripdataCsv.infer_sort_cols(df)
err(f"{name}: inferred default columns: {', '.join(path_cols)}")
df = df.sort_values(path_cols)
linenos = df.lineno.tolist()
with (
cls.open_path(in_path, 'rb') as input_file,
out_fd as output_file
):
header, *lines = list(input_file)
output_file.write(header)
nonempty_lines = []
empty_line_idxs = []
for idx, line in enumerate(lines):
if line == b'\n':
empty_line_idxs.append(idx)
else:
nonempty_lines.append(line)
lines = nonempty_lines
if empty_line_idxs:
err(f"{name}: removed {len(empty_line_idxs)} empty lines: {', '.join(map(str, empty_line_idxs))}")
if len(lines) != len(df):
raise ValueError(f"{name}: {len(lines)} lines != {len(df)} DF size")
for lineno in linenos:
output_file.write(lines[lineno])
def extract_csv_from_zip(self):
with TemporaryDirectory() as tmpdir:
tmp_path = join(tmpdir, f'{self.name}.csv')
with open(tmp_path, 'wb') as o:
header = None
for fdno, i in enumerate(self.zip_csv_fds()):
line = next(i)
if fdno == 0:
header = line
o.write(line)
else:
header2 = line
if header != header2:
raise RuntimeError(f"Header mismatch in {self.src.url} (CSV idx {fdno}): {header} != {header2}")
o.write(b'\n')
copyfileobj(i, o)
with self.fd('wb') as raw_o:
self.sort_and_write(
self.name,
tmp_path,
DeterministicGzipFile(
fileobj=raw_o,
mode='wb',
compresslevel=DEFAULT_COMPRESSION_LEVEL
)
)
def _create(self, read: Union[None, Read] = Unset) -> DataFrame | None:
read = self.read if read is Unset else read
if read is None:
self.extract_csv_from_zip()
else:
return self.checkpoint(read=read)
def zip_csv_fds(self) -> Iterator[IO]:
"""Return a read fd for the single CSV in the source .zip."""
src = self.src
ym = self.ym
zip_yym = int(self.src.yym)
with src.fd('rb') as z_in:
z = ZipFile(z_in)
names = z.namelist()
err(f'{src.url}: zip names: {names}')
if zip_yym < 2024:
dir0 = f'{zip_yym}-citibike-tripdata'
if zip_yym >= 2020:
# 2020-2023 have per-month Zips nested inside the year Zip
inner_zip_name = f'{dir0}/{ym}-citibike-tripdata.zip'
with z.open(inner_zip_name, 'r') as inner_zip_fd:
inner_zip = ZipFile(inner_zip_fd)
csvs = list(sorted([ f for f in inner_zip.namelist() if f.endswith('.csv') and not f.startswith('_') ]))
err(f"{ym}: loaded CSVs from annual inner zip: {csvs}")
for csv in csvs:
yield inner_zip.open(csv, 'r')
return
else:
month = ym.m
month_name = calendar.month_name[month]
dir1 = f'{month}_{month_name}'
# "Type 1" name patterns:
# ```
# $ uzl $z18 | ew csv | rv MACOS | snk2 -t_ | snk3 -st_ | snk2 -st/ | last
# …
# 2018-citibike-tripdata/3_March/201803-citibike-tripdata_1.csv
# 2018-citibike-tripdata/4_April/201804-citibike-tripdata_1.csv
# 2018-citibike-tripdata/4_April/201804-citibike-tripdata_2.csv
# …
# ```
# In 2017, there's an extra ".csv" before the "_<N>" suffix:
# ```
# $ uzl $z17 | ew csv | rv MACOS | snk3 -st_ | snk2 -st/ | last
# …
# 2017-citibike-tripdata/3_March/201703-citibike-tripdata.csv_1.csv
# 2017-citibike-tripdata/4_April/201704-citibike-tripdata.csv_1.csv
# 2017-citibike-tripdata/4_April/201704-citibike-tripdata.csv_2.csv
# …
# ```
prefix1 = f'{dir0}/{dir1}/{ym}-citibike-tripdata{".csv" if ym.y == 2017 else ""}_'
csvs1 = list(sorted([ f for f in names if f.startswith(prefix1) and f.endswith('.csv') ]))
# "Type 2" name patterns:
# ```
# 2013-citibike-tripdata/201306-citibike-tripdata.csv
# 2018-citibike-tripdata/201801-citibike-tripdata.csv
# ```
name2 = f'{dir0}/{ym}-citibike-tripdata.csv'
csvs2 = list(sorted([ f for f in names if f == name2 ]))
if not csvs1 and not csvs2:
raise RuntimeError(f"Failed to find CSVs in {src.url} with name patterns: {prefix1}*.csv, {name2}")
pattern = self.name_type
if pattern is None or pattern == 1:
csvs = csvs1 if csvs1 else csvs2
elif pattern == 2:
csvs = csvs2 if csvs2 else csvs1
elif pattern == 11:
if csvs2 and not csvs1:
raise RuntimeError(f'Expected "type 1" CSV name patterns ({prefix1}*.csv), but only found {csvs2}')
csvs = csvs1
elif pattern == 22:
if csvs1 and not csvs2:
raise RuntimeError(f'Expected "type 2" CSV name pattern ({name2}), but only found {csvs1}')
csvs = csvs2
err(f"{ym}: loaded CSVs from annual zip: {csvs}")
else:
csvs = list(sorted([ f for f in names if f.endswith('.csv') and not f.startswith('_') ]))
if len(csvs) > 1:
err(f"Found {len(csvs)} CSVs in {src.url}: {csvs}")
if not csvs:
# 202409-citibike-tripdata.zip contains 5 `.csv.zip`s
csv_zip_names = [
f
for f in names
if f.endswith('.csv.zip')
and not f.startswith('_')
and not basename(f).startswith('_')
]
for csv_zip_name in csv_zip_names:
with z.open(csv_zip_name, 'r') as csv_zip_fd:
try:
csv_zip = ZipFile(csv_zip_fd)
except BadZipFile:
raise ValueError(f"Failed to open nested zip file {csv_zip_name} inside {src.url}")
csv_zip_names = csv_zip.namelist()
csvs = list(sorted([ f for f in csv_zip_names if f.endswith('.csv') and not f.startswith('_') ]))
for csv in csvs:
yield csv_zip.open(csv, 'r')
else:
for name in csvs:
yield z.open(name, 'r')
def meta(self):
if self.exists():
return pd.read_csv(self.url, **READ_KWARGS, nrows=0)
else:
with next(self.zip_csv_fds()) as i:
return pd.read_csv(i, **READ_KWARGS, nrows=0)
def _df(self) -> DataFrame:
self._create(read=None)
return pd.read_csv(self.url, **READ_KWARGS)
@classmethod
def infer_sort_cols(cls, df) -> list[str]:
"""Infer default columns to sort by.
NY 201306-201610: "starttime", "stoptime", "bikeid"
NJ 201509-201704: "Start Time", "Stop Time", "Bike ID"
NY NJ 201610-201704: "Start Time", "Stop Time", "Bike ID"
NY NJ 201704-202102: "starttime", "stoptime", "bikeid"
NY NJ 202102-202411: "started_at", "ended_at", "ride_id"
"""
start_time_col = singleton([ c for c in [ 'Start Time', 'starttime', 'started_at' ] if c in df.columns ])
stop_time_col = singleton([ c for c in [ 'Stop Time', 'stoptime', 'ended_at' ] if c in df.columns ])
id_col = singleton([ c for c in [ 'Bike ID', 'bikeid', 'ride_id' ] if c in df.columns ])
path_cols = [ start_time_col, stop_time_col, id_col ]
return path_cols
@property
def checkpoint_kwargs(self):
return dict(fmt='csv', read_kwargs=dict(dtype=str), write_kwargs=dict(index=False))
def _read(self) -> DataFrame:
return pd.read_csv(self.url, **READ_KWARGS)
class TripdataCsvs(HasRootCLI):
DIR = DIR
CHILD_CLS = TripdataCsv
def __init__(
self,
yms: list[YM],
regions: Optional[list[str]] = None,
name_type: NameType = None,
**kwargs,
):
self.src = TripdataZips(yms=yms, regions=regions, roots=kwargs.get('roots'))
self.yms = yms
self.regions = regions or REGIONS
self.name_type = name_type
super().__init__(**kwargs)
@cached_property
def children(self) -> list[TripdataCsv]:
return [
TripdataCsv(ym=u.ym, region=u.region, name_type=self.name_type, **self.kwargs)
for u in self.src.children
]
@property
def m2r2csv(self) -> dict[YM, dict[Region, TripdataCsv]]:
m2r2u = self.src.m2r2u
return {
ym: {
region: TripdataCsv(ym=ym, region=region, **self.kwargs)
for region in r2u
}
for ym, r2u in m2r2u.items()
}
def m2df(self):
return {
m: pd.concat([
csv.df()
for r, csv in r2csv.items()
])
for m, r2csv in self.m2r2csv.items()
}
cli = TripdataCsvs.cli(
help=f"Extract CSVs from \"tripdata\" .zip files. Writes to <root>/{DIR}.",
cmd_decos=[
yms_arg,
region,
option('-t', '--name-type', type=int, help='1: prefer type 1, 2: prefer type 2, 11: require type 1, 22: require type 2'),
],
)
@cli.command('sort')
@option('-n', '--dry-run', is_flag=True, help="Don't write files, just print what would be done")
@option('-z', '--compression-level', type=int, default=DEFAULT_COMPRESSION_LEVEL, help="Gzip compression level")
@argument('paths', nargs=-1)
def sort(
dry_run: bool,
compression_level: int,
paths: Tuple[str, ...],
):
"""Sort one or more `.csv{,.gz}`'s in-place, remove empty lines"""
for path in paths:
if dry_run:
continue
with TemporaryDirectory() as tmpdir:
tmp_path = join(tmpdir, basename(path))
TripdataCsv.sort_and_write(
basename(path),
path,
TripdataCsv.open_path(tmp_path, 'wb', compression_level=compression_level),
)
move(tmp_path, path)