Update README.md
Browse files
README.md
CHANGED
|
@@ -79,7 +79,7 @@ import pandas as pd
|
|
| 79 |
from datetime import datetime
|
| 80 |
import gc
|
| 81 |
|
| 82 |
-
def
|
| 83 |
"""
|
| 84 |
Load only the necessary CSV files from a Hugging Face dataset repository.
|
| 85 |
|
|
@@ -113,6 +113,8 @@ def load_csvs_from_huggingface(start_date, end_date, columns_to_keep=None):
|
|
| 113 |
"patent_number": "Int64",
|
| 114 |
}
|
| 115 |
|
|
|
|
|
|
|
| 116 |
dataset_years = [str(year) for year in range(1978, 2006)]
|
| 117 |
|
| 118 |
start_date_int = int(datetime.strptime(start_date, "%Y-%m-%d").strftime("%Y%m%d"))
|
|
@@ -126,20 +128,20 @@ def load_csvs_from_huggingface(start_date, end_date, columns_to_keep=None):
|
|
| 126 |
raise ValueError(f"No matching CSV files found for {start_date} to {end_date}")
|
| 127 |
|
| 128 |
df_list = []
|
| 129 |
-
|
| 130 |
for year in matching_years:
|
| 131 |
filepath = f"data/years/{year}/clefip2011_en_classification_{year}_validated.csv"
|
| 132 |
|
| 133 |
try:
|
| 134 |
# 1. Load the dataset (This stays on disk as an Arrow memory-map, NOT in RAM)
|
| 135 |
dataset = load_dataset(
|
| 136 |
-
huggingface_dataset_name,
|
| 137 |
-
data_files=filepath,
|
| 138 |
split="train",
|
| 139 |
sep=";",
|
| 140 |
on_bad_lines="skip"
|
| 141 |
)
|
| 142 |
-
|
| 143 |
# Select Columns Before Chunking
|
| 144 |
if columns_to_keep is not None:
|
| 145 |
# Safety check: Only select columns that actually exist in this specific file
|
|
@@ -149,14 +151,14 @@ def load_csvs_from_huggingface(start_date, end_date, columns_to_keep=None):
|
|
| 149 |
|
| 150 |
# 2. Tell HuggingFace to output Pandas dataframes when sliced
|
| 151 |
dataset = dataset.with_format("pandas")
|
| 152 |
-
|
| 153 |
-
# 3. CHUNKING: Process exactly
|
| 154 |
chunk_size = 10000
|
| 155 |
for i in range(0, len(dataset), chunk_size):
|
| 156 |
-
# Only these
|
| 157 |
df_chunk = dataset[i : i + chunk_size]
|
| 158 |
df_chunk.columns = df_chunk.columns.str.strip()
|
| 159 |
-
|
| 160 |
# Filter this specific chunk
|
| 161 |
if "date" in df_chunk.columns:
|
| 162 |
temp_dates = pd.to_numeric(df_chunk["date"], errors="coerce")
|
|
@@ -164,13 +166,22 @@ def load_csvs_from_huggingface(start_date, end_date, columns_to_keep=None):
|
|
| 164 |
df_filtered = df_chunk[mask].copy()
|
| 165 |
else:
|
| 166 |
df_filtered = df_chunk.copy()
|
| 167 |
-
|
| 168 |
# Apply types and append
|
| 169 |
if not df_filtered.empty:
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
df_filtered = df_filtered.astype(valid_column_types)
|
| 172 |
df_list.append(df_filtered)
|
| 173 |
-
|
| 174 |
# Clear chunk from memory immediately
|
| 175 |
del df_chunk, df_filtered
|
| 176 |
gc.collect()
|
|
@@ -182,17 +193,18 @@ def load_csvs_from_huggingface(start_date, end_date, columns_to_keep=None):
|
|
| 182 |
except Exception as e:
|
| 183 |
print(f"Error processing {filepath}: {e}")
|
| 184 |
|
| 185 |
-
|
| 186 |
if not df_list:
|
| 187 |
return pd.DataFrame()
|
| 188 |
-
|
| 189 |
# Combine chunks
|
| 190 |
final_df = pd.concat(df_list, ignore_index=True)
|
| 191 |
-
|
|
|
|
|
|
|
| 192 |
# Destroy the list
|
| 193 |
del df_list
|
| 194 |
gc.collect()
|
| 195 |
-
|
| 196 |
return final_df
|
| 197 |
|
| 198 |
|
|
@@ -205,7 +217,7 @@ Load All Columns
|
|
| 205 |
start_date = "1985-03-01"
|
| 206 |
end_date = "1985-04-30"
|
| 207 |
|
| 208 |
-
df =
|
| 209 |
|
| 210 |
|
| 211 |
```
|
|
@@ -223,7 +235,7 @@ columns_to_keep = [
|
|
| 223 |
start_date = "1985-03-01"
|
| 224 |
end_date = "1985-04-30"
|
| 225 |
|
| 226 |
-
df =
|
| 227 |
|
| 228 |
```
|
| 229 |
|
|
|
|
| 79 |
from datetime import datetime
|
| 80 |
import gc
|
| 81 |
|
| 82 |
+
def load_temporal_optimized(start_date, end_date, columns_to_keep=None):
|
| 83 |
"""
|
| 84 |
Load only the necessary CSV files from a Hugging Face dataset repository.
|
| 85 |
|
|
|
|
| 113 |
"patent_number": "Int64",
|
| 114 |
}
|
| 115 |
|
| 116 |
+
category_cols = {"country", "kind", "lang", "status"}
|
| 117 |
+
|
| 118 |
dataset_years = [str(year) for year in range(1978, 2006)]
|
| 119 |
|
| 120 |
start_date_int = int(datetime.strptime(start_date, "%Y-%m-%d").strftime("%Y%m%d"))
|
|
|
|
| 128 |
raise ValueError(f"No matching CSV files found for {start_date} to {end_date}")
|
| 129 |
|
| 130 |
df_list = []
|
| 131 |
+
|
| 132 |
for year in matching_years:
|
| 133 |
filepath = f"data/years/{year}/clefip2011_en_classification_{year}_validated.csv"
|
| 134 |
|
| 135 |
try:
|
| 136 |
# 1. Load the dataset (This stays on disk as an Arrow memory-map, NOT in RAM)
|
| 137 |
dataset = load_dataset(
|
| 138 |
+
huggingface_dataset_name,
|
| 139 |
+
data_files=filepath,
|
| 140 |
split="train",
|
| 141 |
sep=";",
|
| 142 |
on_bad_lines="skip"
|
| 143 |
)
|
| 144 |
+
|
| 145 |
# Select Columns Before Chunking
|
| 146 |
if columns_to_keep is not None:
|
| 147 |
# Safety check: Only select columns that actually exist in this specific file
|
|
|
|
| 151 |
|
| 152 |
# 2. Tell HuggingFace to output Pandas dataframes when sliced
|
| 153 |
dataset = dataset.with_format("pandas")
|
| 154 |
+
|
| 155 |
+
# 3. CHUNKING: Process exactly 40,000 rows at a time
|
| 156 |
chunk_size = 10000
|
| 157 |
for i in range(0, len(dataset), chunk_size):
|
| 158 |
+
# Only these 40,000 rows are loaded into RAM
|
| 159 |
df_chunk = dataset[i : i + chunk_size]
|
| 160 |
df_chunk.columns = df_chunk.columns.str.strip()
|
| 161 |
+
|
| 162 |
# Filter this specific chunk
|
| 163 |
if "date" in df_chunk.columns:
|
| 164 |
temp_dates = pd.to_numeric(df_chunk["date"], errors="coerce")
|
|
|
|
| 166 |
df_filtered = df_chunk[mask].copy()
|
| 167 |
else:
|
| 168 |
df_filtered = df_chunk.copy()
|
| 169 |
+
|
| 170 |
# Apply types and append
|
| 171 |
if not df_filtered.empty:
|
| 172 |
+
# Step 1: Cast category columns via string[pyarrow] first to handle None/mixed types
|
| 173 |
+
for col in category_cols:
|
| 174 |
+
if col in df_filtered.columns:
|
| 175 |
+
df_filtered[col] = pd.Categorical(df_filtered[col])
|
| 176 |
+
|
| 177 |
+
# Step 2: Apply remaining column types, skipping category cols already handled
|
| 178 |
+
valid_column_types = {
|
| 179 |
+
col: dtype for col, dtype in column_types.items()
|
| 180 |
+
if col in df_filtered.columns and col not in category_cols
|
| 181 |
+
}
|
| 182 |
df_filtered = df_filtered.astype(valid_column_types)
|
| 183 |
df_list.append(df_filtered)
|
| 184 |
+
|
| 185 |
# Clear chunk from memory immediately
|
| 186 |
del df_chunk, df_filtered
|
| 187 |
gc.collect()
|
|
|
|
| 193 |
except Exception as e:
|
| 194 |
print(f"Error processing {filepath}: {e}")
|
| 195 |
|
|
|
|
| 196 |
if not df_list:
|
| 197 |
return pd.DataFrame()
|
| 198 |
+
|
| 199 |
# Combine chunks
|
| 200 |
final_df = pd.concat(df_list, ignore_index=True)
|
| 201 |
+
for col in category_cols:
|
| 202 |
+
if col in final_df.columns:
|
| 203 |
+
final_df[col] = pd.Categorical(final_df[col])
|
| 204 |
# Destroy the list
|
| 205 |
del df_list
|
| 206 |
gc.collect()
|
| 207 |
+
|
| 208 |
return final_df
|
| 209 |
|
| 210 |
|
|
|
|
| 217 |
start_date = "1985-03-01"
|
| 218 |
end_date = "1985-04-30"
|
| 219 |
|
| 220 |
+
df = load_temporal_optimized(start_date, end_date)
|
| 221 |
|
| 222 |
|
| 223 |
```
|
|
|
|
| 235 |
start_date = "1985-03-01"
|
| 236 |
end_date = "1985-04-30"
|
| 237 |
|
| 238 |
+
df = load_temporal_optimized(start_date, end_date, columns_to_keep)
|
| 239 |
|
| 240 |
```
|
| 241 |
|