dev sync
Browse files- app.py +80 -16
- process_asc_files_in_multi_p.py +0 -3
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import traceback
|
|
|
|
| 3 |
if os.name == 'posix' and os.uname().sysname == "Darwin" and os.path.exists("/opt/homebrew/lib"):
|
| 4 |
# Fix for macOS https://github.com/Kozea/CairoSVG/issues/354#issuecomment-1072905204
|
| 5 |
from ctypes.macholib import dyld
|
|
@@ -434,6 +435,62 @@ def save_to_zips(folder, pattern, savename, delete_after_zip=False, required_str
|
|
| 434 |
st.session_state["logger"].info(f"Done zipping for pattern {pattern}")
|
| 435 |
|
| 436 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 437 |
def make_json_serializable(obj, _stats=None):
|
| 438 |
"""
|
| 439 |
Recursively convert non-JSON-serializable objects to serializable types.
|
|
@@ -542,6 +599,7 @@ def call_subprocess(script_path, data):
|
|
| 542 |
json_data_in = json.dumps(serializable_data)
|
| 543 |
|
| 544 |
result = subprocess.run(["python", script_path], input=json_data_in, capture_output=True, text=True)
|
|
|
|
| 545 |
st.session_state["logger"].info(f"Got result from call_subprocess with return code {result.returncode}")
|
| 546 |
if result.stdout and "error" not in result.stdout[:9]:
|
| 547 |
result_data = json.loads(result.stdout)
|
|
@@ -958,14 +1016,17 @@ def process_all_asc_files(
|
|
| 958 |
all_sentence_dfs_concat = all_sentence_dfs_concat.drop(columns="subject_trialID")
|
| 959 |
all_sentence_dfs_concat.to_csv(RESULTS_FOLDER / f"{savestring}_comb_sentences.csv")
|
| 960 |
|
| 961 |
-
|
| 962 |
-
|
| 963 |
-
|
| 964 |
-
|
| 965 |
-
|
| 966 |
-
|
| 967 |
-
|
| 968 |
-
|
|
|
|
|
|
|
|
|
|
| 969 |
else:
|
| 970 |
trials_summary = None
|
| 971 |
subjects_summary = None
|
|
@@ -1443,14 +1504,17 @@ def process_all_csv_files(
|
|
| 1443 |
all_sentence_dfs_concat = all_sentence_dfs_concat.drop(columns="subject_trialID")
|
| 1444 |
all_sentence_dfs_concat.to_csv(RESULTS_FOLDER / f"{savestring}_comb_sentences.csv")
|
| 1445 |
|
| 1446 |
-
|
| 1447 |
-
|
| 1448 |
-
|
| 1449 |
-
|
| 1450 |
-
|
| 1451 |
-
|
| 1452 |
-
|
| 1453 |
-
|
|
|
|
|
|
|
|
|
|
| 1454 |
|
| 1455 |
return (
|
| 1456 |
list_of_trial_lists,
|
|
|
|
| 1 |
import os
|
| 2 |
import traceback
|
| 3 |
+
from datetime import datetime
|
| 4 |
if os.name == 'posix' and os.uname().sysname == "Darwin" and os.path.exists("/opt/homebrew/lib"):
|
| 5 |
# Fix for macOS https://github.com/Kozea/CairoSVG/issues/354#issuecomment-1072905204
|
| 6 |
from ctypes.macholib import dyld
|
|
|
|
| 435 |
st.session_state["logger"].info(f"Done zipping for pattern {pattern}")
|
| 436 |
|
| 437 |
|
| 438 |
+
def create_master_results_zipfile(
|
| 439 |
+
folder,
|
| 440 |
+
individual_zips,
|
| 441 |
+
master_zipname,
|
| 442 |
+
file_type_prefix: str = "asc"
|
| 443 |
+
):
|
| 444 |
+
"""
|
| 445 |
+
Create a master zipfile containing both individual file zips and combined results in organized subfolders.
|
| 446 |
+
|
| 447 |
+
Args:
|
| 448 |
+
folder: The results folder path
|
| 449 |
+
individual_zips: List of individual zipfile paths to include
|
| 450 |
+
master_zipname: Name for the master zipfile (e.g., 'all_asc_results_YYYYMMDD.zip')
|
| 451 |
+
file_type_prefix: Either 'asc' or 'csv' for logging purposes
|
| 452 |
+
|
| 453 |
+
Returns:
|
| 454 |
+
Path to the created master zipfile
|
| 455 |
+
"""
|
| 456 |
+
master_zip_path = folder.joinpath(master_zipname)
|
| 457 |
+
|
| 458 |
+
st.session_state["logger"].info(f"Creating master {file_type_prefix} results zipfile: {master_zipname}")
|
| 459 |
+
|
| 460 |
+
with zipfile.ZipFile(master_zip_path, mode="w") as master_archive:
|
| 461 |
+
# Add individual file zips to /individual/ subfolder
|
| 462 |
+
for individual_zip in individual_zips:
|
| 463 |
+
if os.path.exists(individual_zip):
|
| 464 |
+
individual_zip_path = pl.Path(individual_zip)
|
| 465 |
+
arcname = f"individual/{individual_zip_path.name}"
|
| 466 |
+
master_archive.write(individual_zip_path, arcname=arcname)
|
| 467 |
+
st.session_state["logger"].info(f"Added {individual_zip_path.name} to master zip at {arcname}")
|
| 468 |
+
|
| 469 |
+
# Add combined result files to /combined/ subfolder
|
| 470 |
+
combined_patterns = [
|
| 471 |
+
"*_comb_fixations.csv",
|
| 472 |
+
"*_comb_saccades.csv",
|
| 473 |
+
"*_comb_chars.csv",
|
| 474 |
+
"*_comb_words.csv",
|
| 475 |
+
"*_comb_sentences.csv",
|
| 476 |
+
"*_comb_metadata.json",
|
| 477 |
+
"*_trials_summary.csv",
|
| 478 |
+
"*_subjects_summary.csv",
|
| 479 |
+
]
|
| 480 |
+
|
| 481 |
+
added_combined_files = set()
|
| 482 |
+
for pattern in combined_patterns:
|
| 483 |
+
for file_path in folder.glob(pattern):
|
| 484 |
+
if file_path.name not in added_combined_files:
|
| 485 |
+
arcname = f"combined/{file_path.name}"
|
| 486 |
+
master_archive.write(file_path, arcname=arcname)
|
| 487 |
+
added_combined_files.add(file_path.name)
|
| 488 |
+
st.session_state["logger"].info(f"Added {file_path.name} to master zip at {arcname}")
|
| 489 |
+
|
| 490 |
+
st.session_state["logger"].info(f"Master {file_type_prefix} results zipfile created: {master_zip_path}")
|
| 491 |
+
return str(master_zip_path)
|
| 492 |
+
|
| 493 |
+
|
| 494 |
def make_json_serializable(obj, _stats=None):
|
| 495 |
"""
|
| 496 |
Recursively convert non-JSON-serializable objects to serializable types.
|
|
|
|
| 599 |
json_data_in = json.dumps(serializable_data)
|
| 600 |
|
| 601 |
result = subprocess.run(["python", script_path], input=json_data_in, capture_output=True, text=True)
|
| 602 |
+
ic(result.stderr)
|
| 603 |
st.session_state["logger"].info(f"Got result from call_subprocess with return code {result.returncode}")
|
| 604 |
if result.stdout and "error" not in result.stdout[:9]:
|
| 605 |
result_data = json.loads(result.stdout)
|
|
|
|
| 1016 |
all_sentence_dfs_concat = all_sentence_dfs_concat.drop(columns="subject_trialID")
|
| 1017 |
all_sentence_dfs_concat.to_csv(RESULTS_FOLDER / f"{savestring}_comb_sentences.csv")
|
| 1018 |
|
| 1019 |
+
# Create master zipfile with organized subfolders
|
| 1020 |
+
date_str = datetime.now().strftime("%Y%m%d")
|
| 1021 |
+
master_zip_name = f"all_asc_results_{date_str}.zip"
|
| 1022 |
+
master_zip_path = create_master_results_zipfile(
|
| 1023 |
+
RESULTS_FOLDER,
|
| 1024 |
+
zipfiles_with_results,
|
| 1025 |
+
master_zip_name,
|
| 1026 |
+
file_type_prefix="asc"
|
| 1027 |
+
)
|
| 1028 |
+
# Add master zip to the list for user download
|
| 1029 |
+
zipfiles_with_results.append(master_zip_path)
|
| 1030 |
else:
|
| 1031 |
trials_summary = None
|
| 1032 |
subjects_summary = None
|
|
|
|
| 1504 |
all_sentence_dfs_concat = all_sentence_dfs_concat.drop(columns="subject_trialID")
|
| 1505 |
all_sentence_dfs_concat.to_csv(RESULTS_FOLDER / f"{savestring}_comb_sentences.csv")
|
| 1506 |
|
| 1507 |
+
# Create master zipfile with organized subfolders
|
| 1508 |
+
date_str = datetime.now().strftime("%Y%m%d")
|
| 1509 |
+
master_zip_name = f"all_customfile_results_{date_str}.zip"
|
| 1510 |
+
master_zip_path = create_master_results_zipfile(
|
| 1511 |
+
RESULTS_FOLDER,
|
| 1512 |
+
zipfiles_with_results,
|
| 1513 |
+
master_zip_name,
|
| 1514 |
+
file_type_prefix="csv"
|
| 1515 |
+
)
|
| 1516 |
+
# Add master zip to the list for user download
|
| 1517 |
+
zipfiles_with_results.append(master_zip_path)
|
| 1518 |
|
| 1519 |
return (
|
| 1520 |
list_of_trial_lists,
|
process_asc_files_in_multi_p.py
CHANGED
|
@@ -1,11 +1,8 @@
|
|
| 1 |
-
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, as_completed
|
| 2 |
import json
|
| 3 |
import logging
|
| 4 |
-
from sys import platform as _platform
|
| 5 |
from functools import partial
|
| 6 |
import multiprocessing
|
| 7 |
import os
|
| 8 |
-
from tqdm.auto import tqdm
|
| 9 |
from multi_proc_funcs import DIST_MODELS_FOLDER, process_trial_choice, set_up_models
|
| 10 |
import sys
|
| 11 |
import pandas as pd
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
import logging
|
|
|
|
| 3 |
from functools import partial
|
| 4 |
import multiprocessing
|
| 5 |
import os
|
|
|
|
| 6 |
from multi_proc_funcs import DIST_MODELS_FOLDER, process_trial_choice, set_up_models
|
| 7 |
import sys
|
| 8 |
import pandas as pd
|