Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -138,6 +138,34 @@ def create_sample_data() -> pd.DataFrame:
|
|
138 |
|
139 |
return df
|
140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
def analyze_eis_optimized(
|
142 |
df: pd.DataFrame,
|
143 |
circuit_model: str = "auto",
|
@@ -159,9 +187,30 @@ def analyze_eis_optimized(
|
|
159 |
return {"error": "Insufficient memory available"}, None, None
|
160 |
|
161 |
try:
|
162 |
-
#
|
163 |
-
|
164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
|
166 |
# Use optimized parameters for HF
|
167 |
params = HF_OPTIMIZED_PARAMS.copy() if use_hf_params else {}
|
@@ -267,7 +316,9 @@ def analyze_eis_optimized(
|
|
267 |
"fit_parameters": fitted_params if fitted_params else {},
|
268 |
"fit_error": float(fit_error) if fit_error else None,
|
269 |
"chi_squared": float(chi_squared) if chi_squared else None,
|
270 |
-
"memory_usage_mb": get_memory_usage()
|
|
|
|
|
271 |
}
|
272 |
|
273 |
if progress_callback:
|
@@ -363,10 +414,13 @@ def create_interface():
|
|
363 |
|
364 |
with gr.Row():
|
365 |
gr.Markdown("""
|
366 |
-
**
|
367 |
-
|
368 |
-
|
369 |
-
|
|
|
|
|
|
|
370 |
|
371 |
Leave empty to use sample data.
|
372 |
""")
|
@@ -460,9 +514,24 @@ def create_interface():
|
|
460 |
return df.head(10), f"Memory: {get_memory_usage():.1f} MB"
|
461 |
try:
|
462 |
df = pd.read_csv(file.name)
|
463 |
-
|
464 |
-
|
465 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
466 |
|
467 |
def clear_all():
|
468 |
gc.collect()
|
|
|
138 |
|
139 |
return df
|
140 |
|
141 |
+
def detect_column_names(df: pd.DataFrame) -> Dict[str, str]:
|
142 |
+
"""Auto-detect column names for EIS data"""
|
143 |
+
columns = df.columns.tolist()
|
144 |
+
mapping = {}
|
145 |
+
|
146 |
+
# Frequency column detection
|
147 |
+
freq_candidates = ['frequency', 'freq', 'f', 'Hz', 'Frequency']
|
148 |
+
for col in columns:
|
149 |
+
if any(candidate.lower() in col.lower() for candidate in freq_candidates):
|
150 |
+
mapping['frequency'] = col
|
151 |
+
break
|
152 |
+
|
153 |
+
# Real impedance column detection
|
154 |
+
real_candidates = ['z_real', 'real', 'zreal', 'z\'', 'realImpedance', 'real_impedance', 'Re(Z)', 'Re_Z']
|
155 |
+
for col in columns:
|
156 |
+
if any(candidate.lower() in col.lower().replace('(', '').replace(')', '') for candidate in real_candidates):
|
157 |
+
mapping['z_real'] = col
|
158 |
+
break
|
159 |
+
|
160 |
+
# Imaginary impedance column detection
|
161 |
+
imag_candidates = ['z_imag', 'imag', 'zimag', 'z\'\'', 'imagImpedance', 'imag_impedance', 'Im(Z)', 'Im_Z', '-z\'\'']
|
162 |
+
for col in columns:
|
163 |
+
if any(candidate.lower() in col.lower().replace('(', '').replace(')', '') for candidate in imag_candidates):
|
164 |
+
mapping['z_imag'] = col
|
165 |
+
break
|
166 |
+
|
167 |
+
return mapping
|
168 |
+
|
169 |
def analyze_eis_optimized(
|
170 |
df: pd.DataFrame,
|
171 |
circuit_model: str = "auto",
|
|
|
187 |
return {"error": "Insufficient memory available"}, None, None
|
188 |
|
189 |
try:
|
190 |
+
# Auto-detect column names
|
191 |
+
column_mapping = detect_column_names(df)
|
192 |
+
|
193 |
+
if 'frequency' not in column_mapping:
|
194 |
+
return {"error": "Could not find frequency column. Expected names: frequency, freq, f, Hz"}, None, None
|
195 |
+
if 'z_real' not in column_mapping:
|
196 |
+
return {"error": "Could not find real impedance column. Expected names: z_real, real, realImpedance, Re(Z)"}, None, None
|
197 |
+
if 'z_imag' not in column_mapping:
|
198 |
+
return {"error": "Could not find imaginary impedance column. Expected names: z_imag, imag, imagImpedance, Im(Z)"}, None, None
|
199 |
+
|
200 |
+
# Prepare impedance data using detected column names
|
201 |
+
freq = df[column_mapping['frequency']].values
|
202 |
+
z_real = df[column_mapping['z_real']].values
|
203 |
+
z_imag = df[column_mapping['z_imag']].values
|
204 |
+
|
205 |
+
# Handle imaginary part sign convention
|
206 |
+
# EIS convention: Z = Z' - jZ'' (imaginary part should be negative for typical circuits)
|
207 |
+
# If most imaginary values are positive, we might need to flip the sign
|
208 |
+
if np.mean(z_imag) > 0:
|
209 |
+
z_imag = -z_imag # Flip sign to follow EIS convention
|
210 |
+
if progress_callback:
|
211 |
+
progress_callback(0.15, "Adjusted imaginary impedance sign...")
|
212 |
+
|
213 |
+
Z = z_real + 1j * z_imag
|
214 |
|
215 |
# Use optimized parameters for HF
|
216 |
params = HF_OPTIMIZED_PARAMS.copy() if use_hf_params else {}
|
|
|
316 |
"fit_parameters": fitted_params if fitted_params else {},
|
317 |
"fit_error": float(fit_error) if fit_error else None,
|
318 |
"chi_squared": float(chi_squared) if chi_squared else None,
|
319 |
+
"memory_usage_mb": get_memory_usage(),
|
320 |
+
"column_mapping": column_mapping,
|
321 |
+
"data_points": len(freq)
|
322 |
}
|
323 |
|
324 |
if progress_callback:
|
|
|
414 |
|
415 |
with gr.Row():
|
416 |
gr.Markdown("""
|
417 |
+
**Supported CSV Formats (auto-detected):**
|
418 |
+
|
419 |
+
**Frequency column**: `frequency`, `freq`, `f`, `Hz`
|
420 |
+
**Real impedance**: `z_real`, `real`, `realImpedance`, `Re(Z)`
|
421 |
+
**Imaginary impedance**: `z_imag`, `imag`, `imagImpedance`, `Im(Z)`
|
422 |
+
|
423 |
+
✅ **Your file format is automatically detected!**
|
424 |
|
425 |
Leave empty to use sample data.
|
426 |
""")
|
|
|
514 |
return df.head(10), f"Memory: {get_memory_usage():.1f} MB"
|
515 |
try:
|
516 |
df = pd.read_csv(file.name)
|
517 |
+
# Detect column mapping
|
518 |
+
mapping = detect_column_names(df)
|
519 |
+
missing = []
|
520 |
+
if 'frequency' not in mapping:
|
521 |
+
missing.append("frequency")
|
522 |
+
if 'z_real' not in mapping:
|
523 |
+
missing.append("real impedance")
|
524 |
+
if 'z_imag' not in mapping:
|
525 |
+
missing.append("imaginary impedance")
|
526 |
+
|
527 |
+
if missing:
|
528 |
+
status = f"Memory: {get_memory_usage():.1f} MB | ⚠️ Missing: {', '.join(missing)}"
|
529 |
+
else:
|
530 |
+
status = f"Memory: {get_memory_usage():.1f} MB | ✅ All columns detected"
|
531 |
+
|
532 |
+
return df.head(10), status
|
533 |
+
except Exception as e:
|
534 |
+
return None, f"Memory: {get_memory_usage():.1f} MB | ❌ Error: {str(e)}"
|
535 |
|
536 |
def clear_all():
|
537 |
gc.collect()
|