amylonidis commited on
Commit
4088d9b
·
verified ·
1 Parent(s): 10aa90c

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +59 -30
README.md CHANGED
@@ -92,61 +92,90 @@ def load_csvs_from_huggingface(start_date, end_date):
92
  huggingface_dataset_name = "amylonidis/PatClass2011"
93
 
94
  column_types = {
95
- "ucid": "string",
96
  "country": "category",
97
- "doc_number": "int64",
98
  "kind": "category",
99
  "lang": "category",
100
- "date": "int32",
101
- "application_date": "int32",
102
- "date_produced": "int32",
103
  "status": "category",
104
- "main_code": "string",
105
- "further_codes": "string",
106
- "ipcr_codes": "string",
107
- "ecla_codes": "string",
108
- "title": "string",
109
- "abstract": "string",
110
- "description": "string",
111
- "claims": "string",
112
- "applicants": "string",
113
- "inventors": "string",
 
114
  }
115
 
116
- dataset_years = ['1978', '1979', '1980', '1981', '1982', '1983', '1984', '1985', '1986',
117
- '1987', '1988', '1989', '1990', '1991', '1992', '1993', '1994', '1995',
118
- '1996','1997', '1998', '1999', '2000', '2001', '2002','2003', '2004', '2005']
119
 
120
  start_date_int = int(datetime.strptime(start_date, "%Y-%m-%d").strftime("%Y%m%d"))
121
  end_date_int = int(datetime.strptime(end_date, "%Y-%m-%d").strftime("%Y%m%d"))
122
 
123
  start_year, end_year = str(start_date_int)[:4], str(end_date_int)[:4]
124
  given_years = [str(year) for year in range(int(start_year), int(end_year) + 1)]
125
- matching_years = [f for f in dataset_years for year in given_years if f==year]
126
 
127
  if not matching_years:
128
  raise ValueError(f"No matching CSV files found for {start_date} to {end_date}")
129
 
130
  df_list = []
 
131
  for year in matching_years:
132
  filepath = f"data/years/{year}/clefip2011_en_classification_{year}_validated.csv"
133
 
134
  try:
135
- dataset = load_dataset(huggingface_dataset_name, data_files=filepath, split="train")
136
- df = dataset.to_pandas().astype(column_types)
137
- mask = (df["date"] >= start_date_int) & (df["date"] <= end_date_int)
138
- df_filtered = df[mask].copy()
139
-
140
-
141
- if not df_filtered.empty:
142
- df_list.append(df_filtered)
143
-
144
- del df, dataset, df_filtered, mask
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  gc.collect()
146
 
147
  except Exception as e:
148
  print(f"Error processing {filepath}: {e}")
149
 
 
150
  return pd.concat(df_list, ignore_index=True) if df_list else pd.DataFrame()
151
 
152
 
@@ -179,7 +208,7 @@ This will load the dataset into a `DatasetDict` object, please make sure you hav
179
 
180
  You can also use the following Google Colab notebooks to explore the Analytics that were performed to the dataset.
181
 
182
- - [Date Analytics](https://colab.research.google.com/drive/1N2w5F1koWmZOyQaf0ZTB3gighPTXtUzD?usp=sharing)
183
  - [Applicant - Inventor Name Analytics](https://colab.research.google.com/drive/1y1nEGrl40IjsUrFKgVsmYyuHyve40mfk?usp=sharing)
184
  - [Main Codes Analytics](https://colab.research.google.com/drive/1fhAgrSAsO5Q2TzFlFywI6Bl0D2w-cfoL?usp=sharing)
185
  - [Section Title Analytics](https://colab.research.google.com/drive/126zqwzdt2nZDF5N7mdl4n8XadCnwav12?usp=sharing)
 
92
  huggingface_dataset_name = "amylonidis/PatClass2011"
93
 
94
  column_types = {
95
+ "ucid": "string[pyarrow]",
96
  "country": "category",
 
97
  "kind": "category",
98
  "lang": "category",
99
+ "date": "Int32",
100
+ "application_date": "Int32",
101
+ "date_produced": "Int32",
102
  "status": "category",
103
+ "main_code": "string[pyarrow]",
104
+ "further_codes": "string[pyarrow]",
105
+ "ipcr_codes": "string[pyarrow]",
106
+ "ecla_codes": "string[pyarrow]",
107
+ "title": "string[pyarrow]",
108
+ "abstract": "string[pyarrow]",
109
+ "description": "string[pyarrow]",
110
+ "claims": "string[pyarrow]",
111
+ "applicants": "string[pyarrow]",
112
+ "inventors": "string[pyarrow]",
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"))
119
  end_date_int = int(datetime.strptime(end_date, "%Y-%m-%d").strftime("%Y%m%d"))
120
 
121
  start_year, end_year = str(start_date_int)[:4], str(end_date_int)[:4]
122
  given_years = [str(year) for year in range(int(start_year), int(end_year) + 1)]
123
+ matching_years = [f for f in dataset_years if f in given_years]
124
 
125
  if not matching_years:
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
+ # 2. Tell HuggingFace to output Pandas dataframes when sliced
144
+ dataset = dataset.with_format("pandas")
145
+
146
+ # 3. CHUNKING: Process exactly 10,000 rows at a time
147
+ chunk_size = 10000
148
+ for i in range(0, len(dataset), chunk_size):
149
+ # Only these 10,000 rows are loaded into RAM
150
+ df_chunk = dataset[i : i + chunk_size]
151
+ df_chunk.columns = df_chunk.columns.str.strip()
152
+
153
+ # Filter this specific chunk
154
+ if "date" in df_chunk.columns:
155
+ temp_dates = pd.to_numeric(df_chunk["date"], errors="coerce")
156
+ mask = (temp_dates >= start_date_int) & (temp_dates <= end_date_int)
157
+ df_filtered = df_chunk[mask].copy()
158
+ else:
159
+ df_filtered = df_chunk.copy()
160
+
161
+ # Apply types and append
162
+ if not df_filtered.empty:
163
+ valid_column_types = {col: dtype for col, dtype in column_types.items() if col in df_filtered.columns}
164
+ df_filtered = df_filtered.astype(valid_column_types)
165
+ df_list.append(df_filtered)
166
+
167
+ # Clear chunk from memory immediately
168
+ del df_chunk, df_filtered
169
+ gc.collect()
170
+
171
+ # Clear the dataset object from memory before the next year
172
+ del dataset
173
  gc.collect()
174
 
175
  except Exception as e:
176
  print(f"Error processing {filepath}: {e}")
177
 
178
+ # Combine all the tiny, filtered chunks at the very end
179
  return pd.concat(df_list, ignore_index=True) if df_list else pd.DataFrame()
180
 
181
 
 
208
 
209
  You can also use the following Google Colab notebooks to explore the Analytics that were performed to the dataset.
210
 
211
+ - [Date Analytics](https://colab.research.google.com/drive/17KXRabtBLPiPmgVIl2N30MoRqzOqlX-N?usp=sharing)
212
  - [Applicant - Inventor Name Analytics](https://colab.research.google.com/drive/1y1nEGrl40IjsUrFKgVsmYyuHyve40mfk?usp=sharing)
213
  - [Main Codes Analytics](https://colab.research.google.com/drive/1fhAgrSAsO5Q2TzFlFywI6Bl0D2w-cfoL?usp=sharing)
214
  - [Section Title Analytics](https://colab.research.google.com/drive/126zqwzdt2nZDF5N7mdl4n8XadCnwav12?usp=sharing)