Spaces:
Running
Running
File size: 56,201 Bytes
d9f3d42 121ad92 dc9b4d4 27762e4 2725f44 d9f3d42 5d7739c dc9b4d4 32b338c 121ad92 108d59f d9f3d42 f351db0 27762e4 0c0f7f8 14cf64d b3d2bc9 14cf64d 0c0f7f8 d9f3d42 121ad92 d9f3d42 b3d2bc9 121ad92 2725f44 edbaaaf b3d2bc9 edbaaaf b3d2bc9 9d73992 b3d2bc9 edbaaaf b3d2bc9 27762e4 b3d2bc9 27762e4 b3d2bc9 694763e edbaaaf b3d2bc9 edbaaaf b3d2bc9 9d73992 b3d2bc9 edbaaaf 121ad92 b3d2bc9 edbaaaf b3d2bc9 9d73992 b3d2bc9 edbaaaf 121ad92 44f8493 869b08d 27762e4 44f8493 27762e4 44f8493 27762e4 44f8493 27762e4 44f8493 27762e4 dc9b4d4 27762e4 44f8493 27762e4 44f8493 121ad92 27762e4 5d7739c 27762e4 121ad92 27762e4 121ad92 27762e4 869b08d 27762e4 869b08d 27762e4 869b08d 27762e4 869b08d 27762e4 121ad92 0c0f7f8 6e901da edbaaaf 27762e4 edbaaaf 6e901da b3d2bc9 27762e4 b3d2bc9 f351db0 27762e4 f351db0 27762e4 2725f44 27762e4 6e901da f351db0 6e901da dc9b4d4 6e901da f351db0 6e901da f351db0 27762e4 2725f44 27762e4 6e901da f351db0 27762e4 2725f44 27762e4 6e901da f351db0 27762e4 2725f44 27762e4 6e901da f351db0 6e901da 27762e4 2725f44 27762e4 6e901da 27762e4 6e901da 27762e4 2725f44 27762e4 6e901da 27762e4 2725f44 27762e4 6e901da 27762e4 6e901da 27762e4 6e901da dc9b4d4 edbaaaf 27762e4 edbaaaf b3d2bc9 27762e4 b3d2bc9 9d73992 27762e4 9d73992 b3d2bc9 694763e d9f3d42 694763e d9f3d42 6e901da 27762e4 2725f44 27762e4 6e901da 44f8493 6e901da 27762e4 6e901da 27762e4 2725f44 27762e4 6e901da 27762e4 6e901da dc9b4d4 edbaaaf 27762e4 edbaaaf b3d2bc9 f00a555 b3d2bc9 27762e4 b3d2bc9 9d73992 27762e4 9d73992 b3d2bc9 6e901da f351db0 27762e4 f351db0 dc9b4d4 f351db0 dc9b4d4 f351db0 dc9b4d4 f351db0 dc9b4d4 f351db0 dc9b4d4 f00a555 f351db0 f00a555 dc9b4d4 f00a555 27762e4 f00a555 f351db0 f00a555 dc9b4d4 f00a555 27762e4 f00a555 6e901da 27762e4 2725f44 27762e4 2725f44 27762e4 694763e 27762e4 2725f44 27762e4 2725f44 27762e4 2725f44 27762e4 2725f44 dc9b4d4 27762e4 b3d2bc9 f00a555 b3d2bc9 27762e4 9d73992 27762e4 9d73992 27762e4 9d73992 b3d2bc9 27762e4 f351db0 f00a555 f351db0 27762e4 f00a555 f351db0 27762e4 f00a555 27762e4 b3d2bc9 0c0f7f8 dc9b4d4 27762e4 b3d2bc9 27762e4 b3d2bc9 9d73992 27762e4 9d73992 b3d2bc9 694763e 44f8493 694763e 0c0f7f8 44f8493 0c0f7f8 694763e 0c0f7f8 8dfd384 694763e 0c0f7f8 8dfd384 0c0f7f8 694763e 0c0f7f8 8dfd384 0c0f7f8 694763e 44f8493 8dfd384 44f8493 694763e 44f8493 694763e 44f8493 694763e 44f8493 8dfd384 44f8493 8dfd384 44f8493 8dfd384 44f8493 694763e 44f8493 694763e 44f8493 869b08d 0c0f7f8 27762e4 0c0f7f8 44f8493 0c0f7f8 44f8493 869b08d 27762e4 edbaaaf 0c0f7f8 27762e4 0c0f7f8 edbaaaf 27762e4 edbaaaf 0c0f7f8 27762e4 0c0f7f8 edbaaaf 27762e4 edbaaaf 0c0f7f8 27762e4 0c0f7f8 edbaaaf 27762e4 edbaaaf 0c0f7f8 b3d2bc9 27762e4 b3d2bc9 27762e4 b3d2bc9 0c0f7f8 b3d2bc9 0c0f7f8 869b08d 694763e 0c0f7f8 869b08d 0c0f7f8 869b08d 44f8493 869b08d 8dfd384 0c0f7f8 869b08d 0c0f7f8 468fff2 108d59f 27762e4 108d59f 27762e4 108d59f 13e9399 108d59f 27762e4 108d59f 27762e4 108d59f 32b338c 108d59f 32b338c 468fff2 32b338c 468fff2 32b338c 108d59f 27762e4 108d59f 27762e4 108d59f 27762e4 32b338c 108d59f 13e9399 108d59f 0c0f7f8 5d7739c 27762e4 b3d2bc9 27762e4 b3d2bc9 9d73992 27762e4 9d73992 b3d2bc9 edbaaaf d9f3d42 27762e4 edbaaaf 2725f44 108d59f 27762e4 108d59f edbaaaf 2725f44 b3d2bc9 694763e 8dfd384 694763e 8dfd384 14cf64d 9d73992 14cf64d | 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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 | # Standard Imports
import os
import stat
import copy
import logging
import numpy as np
import warnings
import tempfile
import traceback
import requests
from bokeh.models import Tooltip
# HoloViz Imports
import panel as pn
# Stingray Imports
from stingray.events import EventList
from stingray import Lightcurve
# Dashboard Classes and State Management Imports
from utils.state_manager import state_manager
from utils.app_context import AppContext
from utils.error_handler import ErrorHandler
from utils.error_recovery import ErrorRecoveryPanel, show_file_error, show_validation_error, show_success
from utils.DashboardClasses import (
MainHeader,
MainArea,
OutputBox,
WarningBox,
HelpBox,
WarningHandler,
PlotsContainer,
)
# Strings Imports
# Path to the topmost directory for loaded data
loaded_data_path = os.path.join(os.getcwd(), "files", "loaded-data")
# Create the loaded-data directory if it doesn't exist
os.makedirs(loaded_data_path, exist_ok=True)
def create_warning_handler():
"""
Create an instance of WarningHandler and redirect warnings to this custom handler.
Returns:
WarningHandler: An instance of WarningHandler to handle warnings.
Side effects:
Overrides the default warning handler with a custom one.
Example:
>>> warning_handler = create_warning_handler()
>>> warning_handler.warn("Test warning", category=RuntimeWarning)
"""
warning_handler = WarningHandler()
warnings.showwarning = warning_handler.warn
return warning_handler
""" Header Section """
def create_loadingdata_header(context: AppContext):
"""
Create the header for the data loading section.
Args:
context (AppContext): The application context containing containers and state.
Returns:
MainHeader: An instance of MainHeader with the specified heading.
Example:
>>> header = create_loadingdata_header(context)
>>> header.heading.value
'Data Ingestion'
"""
home_heading_input = pn.widgets.TextInput(name="Heading", value="Data Ingestion")
return MainHeader(heading=home_heading_input)
""" Output Box Section """
def create_loadingdata_output_box(content):
"""
Create an output box to display messages.
Args:
content (str): The content to be displayed in the output box.
Returns:
OutputBox: An instance of OutputBox with the specified content.
Example:
>>> output_box = create_loadingdata_output_box("File loaded successfully.")
>>> output_box.output_content
'File loaded successfully.'
"""
return OutputBox(output_content=content)
""" Warning Box Section """
def create_loadingdata_warning_box(content):
"""
Create a warning box to display warnings.
Args:
content (str): The content to be displayed in the warning box.
Returns:
WarningBox: An instance of WarningBox with the specified content.
Example:
>>> warning_box = create_loadingdata_warning_box("Invalid file format.")
>>> warning_box.warning_content
'Invalid file format.'
"""
return WarningBox(warning_content=content)
def read_event_data(
event,
file_selector,
filename_input,
format_input,
format_checkbox,
rmf_file_dropper,
additional_columns_input,
use_lazy_loading,
use_preview_mode,
preview_duration_input,
context: AppContext,
warning_handler,
):
"""
Load event data from selected files with extended EventList.read functionality,
supporting FileDropper for RMF files and additional columns.
Args:
event: The event object triggering the function.
file_selector: The file selector widget.
filename_input: Text input for filenames.
format_input: Text input for file formats.
format_checkbox: Checkbox for default format.
rmf_file_dropper: File dropper for RMF files.
additional_columns_input: Text input for additional columns.
context (AppContext): The application context containing containers and state.
warning_handler: The handler for warnings.
"""
# Validation for required inputs
if not file_selector.value:
context.update_container('output_box',
create_loadingdata_output_box(
"No file selected. Please select a file to upload."
)
)
return
try:
# Parse file paths
file_paths = file_selector.value
filenames = (
[name.strip() for name in filename_input.value.split(",")]
if filename_input.value
else []
)
except Exception as e:
user_msg, tech_msg = ErrorHandler.handle_error(
e,
context="Parsing file paths and names",
file_count=len(file_selector.value) if file_selector.value else 0
)
# Create retry callback
def retry_load():
load_event_lists_from_file(
event, file_selector, filename_input, format_input,
format_checkbox, rmf_file_dropper, additional_columns_input,
context, warning_handler
)
# Show error panel with retry option
error_panel = ErrorRecoveryPanel.create_error_panel(
error_message=user_msg,
error_type="error",
retry_callback=retry_load,
help_text="Check that file paths and filenames are correctly formatted (comma-separated if multiple)",
technical_details=tech_msg
)
context.update_container('warning_box', error_panel)
return
try:
# Parse file formats
formats = (
[fmt.strip() for fmt in format_input.value.split(",")]
if format_input.value
else []
)
# Use default format if checkbox is checked
if format_checkbox.value:
formats = ["ogip" for _ in range(len(file_paths))]
except Exception as e:
user_msg, tech_msg = ErrorHandler.handle_error(
e,
context="Parsing file formats",
format_input=format_input.value if format_input.value else "None"
)
# Create retry callback
def retry_load():
load_event_lists_from_file(
event, file_selector, filename_input, format_input,
format_checkbox, rmf_file_dropper, additional_columns_input,
context, warning_handler
)
# Show error panel with retry option
error_panel = ErrorRecoveryPanel.create_error_panel(
error_message=user_msg,
error_type="error",
retry_callback=retry_load,
help_text="Supported formats: ogip, hea, fits (comma-separated if multiple files)",
technical_details=tech_msg
)
context.update_container('warning_box', error_panel)
return
try:
# Retrieve the RMF file from FileDropper (if any)
if rmf_file_dropper.value:
rmf_file = list(rmf_file_dropper.value.values())[0]
# Save the file data to a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix=".rmf") as tmp_file:
tmp_file.write(rmf_file)
tmp_file_path = tmp_file.name
except Exception as e:
user_msg, tech_msg = ErrorHandler.handle_error(
e,
context="Processing RMF file",
has_rmf=bool(rmf_file_dropper.value)
)
# Create clear callback to reset RMF file
def clear_rmf():
rmf_file_dropper.value = None
context.update_container('warning_box',
pn.pane.Markdown("*RMF file cleared. Ready to try again.*")
)
# Show error panel with clear option
error_panel = ErrorRecoveryPanel.create_error_panel(
error_message=user_msg,
error_type="error",
clear_callback=clear_rmf,
help_text="Make sure the RMF file is valid and in the correct format (.rmf extension)",
technical_details=tech_msg
)
context.update_container('warning_box', error_panel)
return
try:
# Parse additional columns
additional_columns = (
[col.strip() for col in additional_columns_input.value.split(",")]
if additional_columns_input.value
else None
)
except Exception as e:
user_msg, tech_msg = ErrorHandler.handle_error(
e,
context="Parsing additional columns",
columns_input=additional_columns_input.value if additional_columns_input.value else "None"
)
# Create retry callback
def retry_load():
load_event_lists_from_file(
event, file_selector, filename_input, format_input,
format_checkbox, rmf_file_dropper, additional_columns_input,
context, warning_handler
)
# Create clear callback
def clear_columns():
additional_columns_input.value = ""
context.update_container('warning_box',
pn.pane.Markdown("*Additional columns cleared. Ready to try again.*")
)
# Show error panel with retry and clear options
error_panel = ErrorRecoveryPanel.create_error_panel(
error_message=user_msg,
error_type="error",
retry_callback=retry_load,
clear_callback=clear_columns,
help_text="Provide column names as comma-separated values (e.g., 'PI, ENERGY')",
technical_details=tech_msg
)
context.update_container('warning_box', error_panel)
return
# Use data service to load files
loaded_files = []
for file_path, file_name, file_format in zip(file_paths, filenames, formats):
# Choose loading method based on mode selection
if use_preview_mode.value:
# Use preview mode for extremely large files
result = context.services.data.load_event_list_preview(
file_path=file_path,
name=file_name,
preview_duration=preview_duration_input.value,
rmf_file=tmp_file_path if rmf_file_dropper.value else None,
additional_columns=additional_columns
)
elif use_lazy_loading.value:
# Use lazy loading method (now supports RMF and additional columns!)
result = context.services.data.load_event_list_lazy(
file_path=file_path,
name=file_name,
safety_margin=0.5,
rmf_file=tmp_file_path if rmf_file_dropper.value else None,
additional_columns=additional_columns
)
else:
# Use standard loading method
result = context.services.data.load_event_list(
file_path=file_path,
name=file_name,
fmt=file_format,
rmf_file=tmp_file_path if rmf_file_dropper.value else None,
additional_columns=additional_columns
)
if result["success"]:
# Add loading method info to message
method_info = result.get("metadata", {}).get("method", "standard")
message = result["message"]
if method_info == "standard_risky":
message += " ⚠️ (Loaded despite memory risk)"
loaded_files.append(message)
else:
# If loading failed, show error panel with retry
def retry_load():
read_event_data(
event, file_selector, filename_input, format_input,
format_checkbox, rmf_file_dropper, additional_columns_input,
use_lazy_loading, context, warning_handler
)
error_panel = ErrorRecoveryPanel.create_error_panel(
error_message=result['message'],
error_type="error",
retry_callback=retry_load,
help_text="Check the file format and try again, or select different files",
technical_details=result.get('error', 'No technical details available')
)
context.update_container('output_box', error_panel)
return
# Show success panel
success_message = f"Successfully loaded {len(loaded_files)} file(s)"
details = "<br>".join([f"• {msg}" for msg in loaded_files])
success_panel = ErrorRecoveryPanel.create_success_panel(
success_message=success_message,
details=details
)
context.update_container('output_box', success_panel)
# Show warnings if any
if warning_handler.warnings:
context.update_container('warning_box',
create_loadingdata_warning_box("\n".join(warning_handler.warnings))
)
else:
context.update_container('warning_box', create_loadingdata_warning_box("No warnings."))
# Clear the warnings after displaying them
warning_handler.warnings.clear()
def save_loaded_files(
event,
filename_input,
format_input,
format_checkbox,
context: AppContext,
warning_handler,
):
"""
Save loaded event data to specified file formats.
Args:
event: The event object triggering the function.
filename_input (TextInput): The input widget for filenames.
format_input (TextInput): The input widget for formats.
format_checkbox (Checkbox): The checkbox for default format.
context (AppContext): The application context containing containers and state.
warning_handler (WarningHandler): The handler for warnings.
Side effects:
- Saves files to disk in the specified formats.
- Updates the output and warning containers with messages.
Restrictions:
- Requires that the number of filenames and formats matches the number of loaded files unless default format is used.
Example:
>>> save_loaded_files(event, filename_input, format_input, format_checkbox, context, warning_handler)
>>> os.path.exists('/path/to/saved/file.hdf5')
True # Assuming the file was saved successfully
"""
# Get all event data from state manager
all_event_data = context.state.get_event_data()
if not all_event_data:
context.update_container('output_box',
create_loadingdata_output_box("No files loaded to save.")
)
return
filenames = (
[name.strip() for name in filename_input.value.split(",")]
if filename_input.value
else [event[0] for event in all_event_data]
)
# TODO: ADD checks for valid formats
formats = (
[fmt.strip() for fmt in format_input.value.split(",")]
if format_input.value
else []
)
if format_checkbox.value:
formats = ["hdf5" for _ in range(len(all_event_data))]
if len(filenames) < len(all_event_data):
context.update_container('output_box',
create_loadingdata_output_box("Please specify names for all loaded files.")
)
return
if len(filenames) != len(all_event_data):
context.update_container('output_box',
create_loadingdata_output_box(
"Please ensure that the number of names matches the number of loaded files."
)
)
return
if len(formats) < len(all_event_data):
context.update_container('output_box',
create_loadingdata_output_box(
"Please specify formats for all loaded files or check the default format option."
)
)
return
saved_files = []
try:
for (loaded_name, event_list), file_name, file_format in zip(
all_event_data, filenames, formats
):
if os.path.exists(
os.path.join(loaded_data_path, f"{file_name}.{file_format}")
):
context.update_container('output_box',
create_loadingdata_output_box(
f"A file with the name '{file_name}' already exists. Please provide a different name."
)
)
return
save_path = os.path.join(loaded_data_path, f"{file_name}.{file_format}")
# Use export service to save the event list
result = context.services.export.export_event_list(
name=file_name,
file_path=save_path,
fmt=file_format
)
if result["success"]:
saved_files.append(result["message"])
else:
saved_files.append(f"Error saving '{file_name}': {result['message']}")
context.update_container('output_box',
create_loadingdata_output_box("\n".join(saved_files))
)
if warning_handler.warnings:
context.update_container('warning_box',
create_loadingdata_warning_box("\n".join(warning_handler.warnings))
)
else:
context.update_container('warning_box', create_loadingdata_warning_box("No warnings."))
except Exception as e:
user_msg, tech_msg = ErrorHandler.handle_error(
e,
context="Saving loaded files",
save_directory=loaded_data_path
)
context.update_container('warning_box',
create_loadingdata_warning_box(f"Error: {user_msg}")
)
# Clear the warnings after displaying them
warning_handler.warnings.clear()
# TODO: ADD better comments, error handlling and docstrings
def delete_selected_files(
event,
file_selector,
context: AppContext,
warning_handler,
):
"""
Delete selected files from the file system.
Args:
event: The event object triggering the function.
file_selector (FileSelector): The file selector widget.
context (AppContext): The application context containing containers and state.
warning_handler (WarningHandler): The handler for warnings.
Side effects:
- Deletes files from the file system.
- Updates the output and warning containers with messages.
Restrictions:
- Cannot delete `.py` files for safety reasons.
Example:
>>> delete_selected_files(event, file_selector, context, warning_handler)
>>> os.path.exists('/path/to/deleted/file')
False # Assuming the file was deleted successfully
"""
# Define allowed extensions for deletion
allowed_extensions = {
".pkl",
".pickle",
".fits",
".evt",
".h5",
".hdf5",
".ecsv",
".txt",
".dat",
".csv",
".vot",
".tex",
".html",
".gz",
}
if not file_selector.value:
context.update_container('output_box',
create_loadingdata_output_box(
"No file selected. Please select a file to delete."
)
)
return
file_paths = file_selector.value
deleted_files = []
for file_path in file_paths:
if not any(file_path.endswith(ext) for ext in allowed_extensions):
deleted_files.append(
f"Cannot delete file '{file_path}': File type is not allowed for deletion."
)
continue
try:
# Change the file permissions to ensure it can be deleted
os.chmod(file_path, stat.S_IWUSR | stat.S_IREAD | stat.S_IWRITE)
os.remove(file_path)
deleted_files.append(f"File '{file_path}' deleted successfully.")
except Exception as e:
user_msg, tech_msg = ErrorHandler.handle_error(
e,
context="Deleting file",
file_path=file_path,
log_level=logging.WARNING
)
deleted_files.append(f"Error deleting '{file_path}': {user_msg}")
context.update_container('output_box', create_loadingdata_output_box("\n".join(deleted_files)))
if warning_handler.warnings:
context.update_container('warning_box',
create_loadingdata_warning_box("\n".join(warning_handler.warnings))
)
else:
context.update_container('warning_box', create_loadingdata_warning_box("No warnings."))
warning_handler.warnings.clear()
# TODO: ADD better comments, error handlling and docstrings
def preview_loaded_files(
event,
context: AppContext,
warning_handler,
time_limit=10,
):
"""
Preview the loaded event data files and light curves.
Args:
event: The event object triggering the function.
context (AppContext): The application context containing containers and state.
warning_handler (WarningHandler): The handler for warnings.
time_limit (int): The number of time entries to preview.
Side Effects:
Updates the output and warning containers with preview information.
Example:
>>> preview_loaded_files(event, context, warning_handler)
"Event List - my_event_list:\nTimes (first 10): [0.1, 0.2, ...]\nMJDREF: 58000"
"""
preview_data = []
# Get all data from state manager
all_event_data = context.state.get_event_data()
all_light_curves = context.state.get_light_curve()
# Add a summary of loaded files and their names
if all_event_data:
preview_data.append(
f"Loaded Event Files: {len(all_event_data)}\n"
f"Event File Names: {[file_name for file_name, _ in all_event_data]}\n"
)
else:
preview_data.append("No Event Files Loaded.\n")
if all_light_curves:
preview_data.append(
f"Loaded Light Curves: {len(all_light_curves)}\n"
f"Light Curve Names: {[lc_name for lc_name, _ in all_light_curves]}\n"
)
else:
preview_data.append("No Light Curves Loaded.\n")
# Preview EventList data
if all_event_data:
for file_name, event_list in all_event_data:
try:
# Gather available attributes dynamically
attributes = [
("Times (first entries)", event_list.time[:time_limit]),
("Energy (keV)", getattr(event_list, "energy", "Not available")),
("PI Channels", getattr(event_list, "pi", "Not available")),
("MJDREF", event_list.mjdref),
("Good Time Intervals (GTIs)", event_list.gti),
("Mission", getattr(event_list, "mission", "Not available")),
("Instrument", getattr(event_list, "instr", "Not available")),
(
"Detector IDs",
getattr(event_list, "detector_id", "Not available"),
),
("Ephemeris", getattr(event_list, "ephem", "Not available")),
("Time Reference", getattr(event_list, "timeref", "Not available")),
("Time System", getattr(event_list, "timesys", "Not available")),
("Header", getattr(event_list, "header", "Not available")),
]
# Format preview data
event_preview = "\n\n\n----------------------\n"
event_preview += f"Event List - {file_name}:\n"
for attr_name, attr_value in attributes:
if isinstance(
attr_value, np.ndarray
): # Show limited entries for arrays
attr_value = attr_value[:time_limit]
event_preview += f"{attr_name}: {attr_value}\n\n"
event_preview += "----------------------\n\n\n"
preview_data.append(event_preview)
except Exception as e:
user_msg = ErrorHandler.handle_warning(
str(e),
context="Generating event list preview",
file_name=file_name
)
warning_handler.warn(user_msg, category=RuntimeWarning)
# Preview Lightcurve data
if all_light_curves:
for lc_name, lightcurve in all_light_curves:
try:
attributes = [
("Times (first entries)", lightcurve.time[:time_limit]),
("Counts (first entries)", lightcurve.counts[:time_limit]),
(
"Count Errors (first entries)",
getattr(lightcurve, "counts_err", "Not available"),
),
(
"Background Counts",
getattr(lightcurve, "bg_counts", "Not available"),
),
(
"Background Ratio",
getattr(lightcurve, "bg_ratio", "Not available"),
),
(
"Fractional Exposure",
getattr(lightcurve, "frac_exp", "Not available"),
),
("Mean Rate", getattr(lightcurve, "meanrate", "Not available")),
("Mean Counts", getattr(lightcurve, "meancounts", "Not available")),
("Number of Points", getattr(lightcurve, "n", "Not available")),
("Time Resolution (dt)", lightcurve.dt),
("MJDREF", lightcurve.mjdref),
("Good Time Intervals (GTIs)", lightcurve.gti),
("Duration (tseg)", getattr(lightcurve, "tseg", "Not available")),
(
"Start Time (tstart)",
getattr(lightcurve, "tstart", "Not available"),
),
(
"Error Distribution",
getattr(lightcurve, "err_dist", "Not available"),
),
("Mission", getattr(lightcurve, "mission", "Not available")),
("Instrument", getattr(lightcurve, "instr", "Not available")),
]
lightcurve_preview = "\n\n----------------------\n"
lightcurve_preview += f"Light Curve - {lc_name}:\n"
for attr_name, attr_value in attributes:
if isinstance(attr_value, np.ndarray):
attr_value = attr_value[:time_limit]
lightcurve_preview += f"{attr_name}: {attr_value}\n"
lightcurve_preview += "----------------------\n\n"
preview_data.append(lightcurve_preview)
except Exception as e:
user_msg = ErrorHandler.handle_warning(
str(e),
context="Generating lightcurve preview",
lc_name=lc_name
)
warning_handler.warn(user_msg, category=RuntimeWarning)
# Display preview data or message if no data available
if preview_data:
context.update_container('output_box',
create_loadingdata_output_box("\n\n".join(preview_data))
)
else:
context.update_container('output_box',
create_loadingdata_output_box(
"No valid files or light curves loaded for preview."
)
)
if warning_handler.warnings:
context.update_container('warning_box',
create_loadingdata_warning_box("\n".join(warning_handler.warnings))
)
else:
context.update_container('warning_box', create_loadingdata_warning_box("No warnings."))
warning_handler.warnings.clear()
# TODO: ADD better comments, error handlling and docstrings
def clear_loaded_files(event, context: AppContext):
"""
Clear all loaded event data files and light curves from memory.
Args:
event: The event object triggering the function.
context (AppContext): The application context containing containers and state.
Side effects:
- Clears event data and light curves from state manager.
- Updates the output container with messages.
Example:
>>> clear_loaded_files(event, context)
"Loaded event files have been cleared."
"""
event_data_count = len(context.state.get_event_data())
light_curve_count = len(context.state.get_light_curve())
event_data_cleared = False
light_curve_data_cleared = False
# Clear EventList data
if event_data_count > 0:
context.state.clear_event_data()
event_data_cleared = True
# Clear Lightcurve data
if light_curve_count > 0:
context.state.clear_light_curves()
light_curve_data_cleared = True
# Create appropriate messages based on what was cleared
messages = []
if event_data_cleared:
messages.append("Loaded event files have been cleared.")
if light_curve_data_cleared:
messages.append("Loaded light curves have been cleared.")
if not messages:
messages.append("No files or light curves loaded to clear.")
# Update the output container
context.update_container('output_box', create_loadingdata_output_box("\n".join(messages)))
context.update_container('warning_box', create_loadingdata_warning_box("No warnings."))
# TODO: ADD better comments, error handlling and docstrings
def create_loading_tab(context: AppContext, warning_handler):
"""
Create the tab for loading event data files.
Args:
context (AppContext): The application context containing containers and state.
warning_handler (WarningHandler): The handler for warnings.
Returns:
Column: A Panel Column containing the widgets and layout for the loading tab.
Example:
>>> tab = create_loading_tab(context, warning_handler)
>>> isinstance(tab, pn.Column)
True
"""
# Get the user's home directory
home_directory = os.path.expanduser("~")
file_selector = pn.widgets.FileSelector(
home_directory, only_files=True, name="Select File", show_hidden=True
)
filename_input = pn.widgets.TextInput(
name="Enter File Names",
placeholder="Enter file names, comma-separated",
width=400,
)
format_input = pn.widgets.TextInput(
name="Enter Formats",
placeholder="Enter formats (e.g., ogip, pickle, hdf5), comma-separated",
width=400,
)
format_checkbox = pn.widgets.Checkbox(
name='Use default format ("ogip" for reading, "hdf5" for writing/saving)',
value=False,
)
load_button = pn.widgets.Button(name="Read as EventLists", button_type="primary")
save_button = pn.widgets.Button(
name="Save loaded EventLists", button_type="success"
)
delete_button = pn.widgets.Button(
name="Delete Selected Files", button_type="danger"
)
preview_button = pn.widgets.Button(
name="Preview loaded EventLists", button_type="default"
)
clear_button = pn.widgets.Button(
name="Clear Loaded EventLists", button_type="warning"
)
tooltip_format = pn.widgets.TooltipIcon(
value=Tooltip(
content="""For HEASoft-supported missions, use 'ogip'. Using 'fits' directly might cause issues with Astropy tables.""",
position="bottom",
)
)
tooltip_file = pn.widgets.TooltipIcon(
value=Tooltip(
content="""Ensure the file contains at least a 'time' column.""",
position="bottom",
)
)
tooltip_rmf = pn.widgets.TooltipIcon(
value=Tooltip(
content="""Calibrates PI(Pulse invariant) values to physical energy.""",
position="bottom",
)
)
tooltip_additional_columns = pn.widgets.TooltipIcon(
value=Tooltip(
content="""Any further keyword arguments to be passed for reading in event lists in OGIP/HEASOFT format""",
position="bottom",
)
)
# FileDropper for RMF file
rmf_file_dropper = pn.widgets.FileDropper(
# accepted_filetypes=['.rmf', '.fits'], # Accept RMF files or compatible FITS files
multiple=False, # Only allow a single file
name="Upload RMF(Response Matrix File) File (optional)",
max_file_size="1000MB", # Limit file size
layout="compact", # Layout style
)
additional_columns_input = pn.widgets.TextInput(
name="Additional Columns (optional)", placeholder="Comma-separated column names"
)
# Lazy loading controls
use_lazy_loading = pn.widgets.Checkbox(
name="Use lazy loading (recommended for files >1GB)",
value=False,
)
tooltip_lazy = pn.widgets.TooltipIcon(
value=Tooltip(
content="""Lazy loading reads large files in chunks without loading everything into memory.
Recommended for files >1GB. Prevents memory crashes but some operations may be slower.""",
position="bottom",
)
)
# Preview mode controls (for extremely large files)
use_preview_mode = pn.widgets.Checkbox(
name="Preview mode (load only first segment)",
value=False,
)
preview_duration_input = pn.widgets.FloatInput(
name="Preview duration (seconds)",
value=100.0,
start=10.0,
end=1000.0,
step=10.0,
)
tooltip_preview = pn.widgets.TooltipIcon(
value=Tooltip(
content="""Preview mode loads only the first segment of data for extremely large files.
Useful when file is too large to fit in memory even with lazy loading.
You can analyze the preview and decide on next steps.""",
position="bottom",
),
)
# File size info pane (updated dynamically)
file_size_info = pn.pane.Markdown("", sizing_mode="stretch_width")
def update_file_size_info(event=None):
"""Update file size info when file selection changes."""
if not file_selector.value:
file_size_info.object = ""
use_lazy_loading.value = False
return
try:
file_path = file_selector.value[0] if isinstance(file_selector.value, list) else file_selector.value
# Check file size using data service
result = context.services.data.check_file_size(file_path)
if result["success"]:
data = result["data"]
risk_level = data["risk_level"]
file_size_mb = data["file_size_mb"]
file_size_gb = data["file_size_gb"]
estimated_mem_mb = data["estimated_memory_mb"]
memory_info = data["memory_info"]
recommend_lazy = data["recommend_lazy"]
# Color code based on risk
color_map = {
'safe': 'green',
'caution': 'orange',
'risky': 'darkorange',
'critical': 'red'
}
color = color_map.get(risk_level, 'black')
# Auto-enable lazy loading for large/risky files
if recommend_lazy and not use_lazy_loading.value:
use_lazy_loading.value = True
# Create info message
recommendation_text = "Use lazy loading" if recommend_lazy else "Standard loading OK"
# Add preview mode suggestion for critical/extremely large files
show_preview_warning = (risk_level == 'critical') or (file_size_gb > 5.0)
info_md = f"""
**File Size Info:**
- **File Size**: {file_size_gb:.2f} GB ({file_size_mb:.1f} MB)
- **Estimated Memory**: ~{estimated_mem_mb:.1f} MB
- **Risk Level**: <span style="color:{color}; font-weight:bold">{risk_level.upper()}</span>
- **Available RAM**: {memory_info['available_mb']:.0f} MB ({100-memory_info['percent']:.1f}% free)
- **Recommendation**: {recommendation_text}
"""
if show_preview_warning:
info_md += "\n- **CRITICAL**: File may be too large for full load. Consider using Preview Mode!"
file_size_info.object = info_md
else:
file_size_info.object = f"**Error checking file size:** {result['message']}"
except Exception as e:
file_size_info.object = f"**Error:** {str(e)}"
# Update file size info when file selection changes
file_selector.param.watch(update_file_size_info, 'value')
def on_load_click(event):
# Clear previous outputs and warnings
context.update_container('output_box', create_loadingdata_output_box("N.A."))
context.update_container('warning_box', create_loadingdata_warning_box("N.A."))
warning_handler.warnings.clear()
warnings.resetwarnings()
read_event_data(
event,
file_selector,
filename_input,
format_input,
format_checkbox,
rmf_file_dropper,
additional_columns_input,
use_lazy_loading,
use_preview_mode,
preview_duration_input,
context,
warning_handler,
)
def on_save_click(event):
# Clear previous outputs and warnings
context.update_container('output_box', create_loadingdata_output_box("N.A."))
context.update_container('warning_box', create_loadingdata_warning_box("N.A."))
warning_handler.warnings.clear()
warnings.resetwarnings()
save_loaded_files(
event,
filename_input,
format_input,
format_checkbox,
context,
warning_handler,
)
def on_delete_click(event):
# Clear previous outputs and warnings
context.update_container('warning_box', create_loadingdata_warning_box("N.A."))
context.update_container('output_box', create_loadingdata_output_box("N.A."))
warning_handler.warnings.clear()
warnings.resetwarnings()
delete_selected_files(
event,
file_selector,
context,
warning_handler,
)
def on_preview_click(event):
# Clear previous outputs and warnings
context.update_container('output_box', create_loadingdata_output_box("N.A."))
context.update_container('warning_box', create_loadingdata_warning_box("N.A."))
warning_handler.warnings.clear()
warnings.resetwarnings()
preview_loaded_files(
event, context, warning_handler
)
def on_clear_click(event):
# Clear the loaded files list
context.update_container('output_box', create_loadingdata_output_box("N.A."))
context.update_container('warning_box', create_loadingdata_warning_box("N.A."))
warning_handler.warnings.clear()
warnings.resetwarnings()
clear_loaded_files(event, context)
load_button.on_click(on_load_click)
save_button.on_click(on_save_click)
delete_button.on_click(on_delete_click)
preview_button.on_click(on_preview_click)
clear_button.on_click(on_clear_click)
# Left column: Basic file selection and configuration
left_column = pn.Column(
pn.Row(
pn.pane.Markdown("<h2> Read an EventList object from File</h2>"),
pn.widgets.TooltipIcon(
value=Tooltip(
content="Supported Formats: pickle, hea or ogip, any other astropy.table.Table(ascii.ecsv, hdf5, etc.)",
position="bottom",
)
),
),
file_selector,
file_size_info, # Show file size and memory info
pn.pane.Markdown("---"), # Separator
pn.Row(filename_input, tooltip_file),
pn.Row(format_input, tooltip_format),
format_checkbox,
width_policy="min",
)
# Right column: Advanced options and actions
right_column = pn.Column(
pn.pane.Markdown("<h3>Advanced Options</h3>"),
pn.Row(rmf_file_dropper, tooltip_rmf),
pn.Row(additional_columns_input, tooltip_additional_columns),
pn.pane.Markdown("---"), # Separator
pn.pane.Markdown("<h3>Loading Options</h3>"),
pn.Row(use_lazy_loading, tooltip_lazy),
pn.Row(use_preview_mode, tooltip_preview),
preview_duration_input,
pn.pane.Markdown("---"), # Separator
pn.pane.Markdown("<h3>Actions</h3>"),
pn.Row(load_button, save_button, delete_button),
pn.Row(preview_button, clear_button),
width_policy="min",
)
# Two-column layout
tab_content = pn.Row(
left_column,
right_column,
width_policy="max",
)
return tab_content
# TODO: Add better comments, error handlling and docstrings and increase the functionality
def create_fetch_eventlist_tab(context: AppContext, warning_handler):
"""
Create the tab for fetching EventList data from a link.
Args:
context (AppContext): The application context containing containers and state.
warning_handler (WarningHandler): The handler for warnings.
Returns:
Column: A Panel Column containing the widgets and layout for the fetch tab.
"""
link_input = pn.widgets.TextInput(
name="Enter File Link",
placeholder="Enter the URL to the EventList file",
width=400,
)
filename_input = pn.widgets.TextInput(
name="File Name",
placeholder="Provide a name for the EventList",
width=400,
)
format_select = pn.widgets.Select(
name="File Format",
options=["ogip", "hdf5", "ascii.ecsv", "fits", "pickle"],
value="ogip",
)
fetch_button = pn.widgets.Button(
name="Fetch and Load EventList",
button_type="primary",
)
tooltip_link = pn.widgets.TooltipIcon(
value=Tooltip(
content="""When using urls from github use raw links.""",
position="bottom",
)
)
def fetch_eventlist(event):
if not link_input.value or not filename_input.value:
context.update_container('output_box',
create_loadingdata_output_box(
"Error: Please provide both the link and file name."
)
)
return
try:
link = link_input.value.strip()
# Download the file to a temporary file
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
temp_filename = tmp_file.name
response = requests.get(link, stream=True)
if response.status_code != 200:
raise ValueError(f"Failed to download file. Status code: {response.status_code}")
# Save file
with open(temp_filename, "wb") as f:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
# Use data service to load from URL
result = context.services.data.load_event_list_from_url(
url=link_input.value,
name=filename_input.value.strip(),
fmt=format_select.value
)
if result["success"]:
context.update_container('output_box',
create_loadingdata_output_box(result["message"])
)
else:
warning_handler.warn(result["error"], category=RuntimeWarning)
context.update_container('output_box',
create_loadingdata_output_box(f"Error: {result['message']}")
)
except Exception as e:
user_msg, tech_msg = ErrorHandler.handle_error(
e,
context="Loading event list from URL",
url=link_input.value,
filename=filename_input.value
)
warning_handler.warn(tech_msg, category=RuntimeWarning)
context.update_container('output_box',
create_loadingdata_output_box(f"Error: {user_msg}")
)
finally:
# Ensure the temporary file is deleted after processing
if os.path.exists(temp_filename):
os.remove(temp_filename)
fetch_button.on_click(fetch_eventlist)
tab_content = pn.Column(
pn.pane.Markdown("### Fetch EventList from Link"),
pn.Row(link_input, tooltip_link),
filename_input,
format_select,
fetch_button,
)
return tab_content
def create_loadingdata_main_area(context: AppContext):
"""
Create the main area for the data loading tab, including all sub-tabs.
Args:
context (AppContext): The application context containing containers and state.
Returns:
MainArea: An instance of MainArea with all the necessary tabs for data loading.
Example:
>>> main_area = create_loadingdata_main_area(context)
>>> isinstance(main_area, MainArea)
True
"""
warning_handler = create_warning_handler()
tabs_content = {
"Read Event List from File": create_loading_tab(
context=context,
warning_handler=warning_handler,
),
"Fetch EventList from Link": create_fetch_eventlist_tab(
context=context,
warning_handler=warning_handler,
),
}
return MainArea(tabs_content=tabs_content)
def create_loadingdata_help_area():
"""
Create the help area for the data loading tab.
Returns:
HelpBox: An instance of HelpBox with the help content.
"""
# Content for "Introduction to Event Lists"
intro_content = """
## Introduction to Event Lists
### What are Event Lists?
In X-ray astronomy, an **Event List** represents a record of individual photon detection events as observed by a telescope. Each event corresponds to the detection of a photon and includes attributes like:
- **Time of Arrival (TOA)**: The exact time when the photon was detected.
- **Photon Energy**: Derived from the pulse height or energy channel recorded.
- **Good Time Intervals (GTIs)**: Periods during which the instrument was actively recording valid data.
- **Pulse Invariant (PI) Channel**: A standardized representation of photon energy.
Event Lists are typically the starting point for data analysis in high-energy astrophysics. They provide unbinned, high-precision information about individual photon arrivals, enabling various scientific analyses such as timing, spectral, and correlation studies.
### Scientific Significance of Event Lists
Event Lists allow astronomers to study the variability of astrophysical sources across a wide range of timescales:
- **Fast Transients**: Sources like X-ray bursts, magnetar flares, or fast radio bursts, which brighten and dim on millisecond-to-minute scales.
- **Quasi-Periodic Oscillations (QPOs)**: Oscillations in black hole and neutron star systems that vary unpredictably around a central frequency.
- **Stochastic Variability**: Random fluctuations in brightness, often associated with accretion processes.
Additionally, Event Lists are fundamental for studying:
- **Time Lags**: Delays between high- and low-energy photon emissions due to processes like reflection or turbulent flows in accretion disks.
- **Spectral Timing**: Techniques that combine time and energy data to probe the physical processes near compact objects.
### Anatomy of an Event List
An Event List is often stored as a FITS (Flexible Image Transport System) file, with each row in the table corresponding to a single detected photon. The table contains columns for various attributes:
- **Time**: Precise timestamp of the event (e.g., in seconds or Modified Julian Date).
- **Energy or PI Channel**: Photon energy or pulse invariant channel.
- **GTIs**: Intervals of valid observation time.
- **Spatial Information** (optional): Detector coordinates or celestial coordinates.
### How Event Lists are Used
Event Lists are typically processed and filtered to remove invalid events or background noise. They can then be converted into:
- **Light Curves**: Binned time series of photon counts.
- **Spectra**: Energy distributions of detected photons.
- **Power Spectra**: Frequency-domain representations of variability.
### Key Terms in Event Lists
- **Photon Time of Arrival (TOA)**: The recorded time when a photon hits the detector.
- **Good Time Intervals (GTIs)**: Periods when the instrument was actively recording valid data.
- **Pulse Invariant (PI) Channel**: A detector-specific channel number that maps to the photon’s energy.
- **RMF File**: Response Matrix File, used to calibrate PI channels into physical energy values (e.g., keV).
- **FITS Format**: The standard file format for Event Lists in high-energy astrophysics.
### Example: Event List Data Structure
A typical Event List in FITS format contains columns like:
```
TIME PI ENERGY GTI
---------------------------------
0.0012 12 2.3 keV [0, 100]
0.0034 15 3.1 keV [0, 100]
0.0048 10 1.8 keV [0, 100]
```
### Advantages of Event Lists
- **High Precision**: Tracks individual photon events without binning, preserving maximum information.
- **Flexibility**: Can be transformed into various forms (e.g., light curves, spectra) for different analyses.
- **Time-Energy Data**: Enables advanced spectral-timing techniques.
### Challenges and Considerations
- **Dead Time**: Time intervals when the detector cannot record new events, affecting variability measurements.
- **Instrumental Noise**: False events caused by electronics or background radiation.
- **Time Resolution**: Limited by the instrument's precision in recording photon arrival times.
By understanding Event Lists, astronomers gain insight into the underlying physical processes driving variability in high-energy astrophysical sources.
### References
- van der Klis, M. (2006). "Rapid X-ray Variability."
- Miniutti, G., et al. (2019). "Quasi-Periodic Eruptions in AGN."
- Galloway, D., & Keek, L. (2021). "X-ray Bursts: Physics and Observations."
- HEASARC Guidelines for FITS Event List Formats.
<br><br>
"""
eventlist_read_content = """
## Reading EventList
The `EventList.read` method is used to read event data files and load them as `EventList` objects in Stingray.
This process involves parsing photon event data, such as arrival times, PI (Pulse Invariant) channels, and energy values.
### Supported File Formats
- **`pickle`**: Serialized Python objects (not recommended for long-term storage).
- **`hea`** / **`ogip`**: FITS event files (commonly used in X-ray astronomy).
- **Other Table-supported formats**: e.g., `hdf5`, `ascii.ecsv`, etc.
### Parameters
- **`filename` (str)**: Path to the file containing the event data.
- **`fmt` (str)**: File format. Supported formats include:
- `'pickle'`
- `'hea'` or `'ogip'`
- Table-compatible formats like `'hdf5'`, `'ascii.ecsv'`.
- If `fmt` is not specified, the method attempts to infer the format based on the file extension.
- **`rmf_file` (str, default=None)**:
- Path to the RMF (Response Matrix File) for energy calibration.
- Behavior:
1. **If `fmt="hea"` or `fmt="ogip"`**:
- `rmf_file` is ignored during the `read` process.
- You must apply it manually after loading using `convert_pi_to_energy`.
2. **If `fmt` is not `hea` or `ogip`**:
- `rmf_file` can be directly specified in the `read` method for automatic energy calibration.
- **`kwargs` (dict)**:
- Additional parameters passed to the FITS reader (`load_events_and_gtis`) for reading OGIP/HEASOFT-compatible event lists.
- Example: `additional_columns` for specifying extra data columns to read.
### Attributes in the Loaded EventList
- **`time`**: Array of photon arrival times in seconds relative to `mjdref`.
- **`energy`**: Array of photon energy values (if calibrated using `rmf_file`).
- **`pi`**: Array of Pulse Invariant (PI) channels.
- **`mjdref`**: Reference time (Modified Julian Date).
- **`gtis`**: Good Time Intervals, defining valid observation periods.
### Stingray Classes and Functions in Use
Below are the key classes and methods from Stingray that are used during this process:
#### Class: `EventList`
```python
from stingray.events import EventList
class EventList:
def __init__(self, time=None, energy=None, pi=None, gti=None, mjdref=0, rmf_file=None):
# Initializes the event list with time, energy, PI channels, and other parameters
```
#### Method: `EventList.read`
```python
@classmethod
def read(cls, filename, fmt=None, rmf_file=None, **kwargs):
if fmt in ("hea", "ogip"):
evt = FITSTimeseriesReader(filename, output_class=EventList, **kwargs)[:]
if rmf_file:
evt.convert_pi_to_energy(rmf_file) # Must be applied manually for hea/ogip
return evt
return super().read(filename, fmt=fmt)
```
#### Function: `convert_pi_to_energy`
```python
def convert_pi_to_energy(self, rmf_file):
self.energy = pi_to_energy(self.pi, rmf_file)
```
### Example Usage
```python
from stingray.events import EventList
# Reading an OGIP-compatible FITS file
event_list = EventList.read("example.evt", fmt="ogip")
# Applying RMF manually after reading
event_list.convert_pi_to_energy("example.rmf")
# Reading an HDF5 file with direct RMF calibration
event_list = EventList.read("example.hdf5", fmt="hdf5", rmf_file="example.rmf")
# Accessing attributes
print(event_list.time) # Photon arrival times
print(event_list.energy) # Calibrated energy values (if rmf_file used)
print(event_list.pi) # PI channels
print(event_list.gtis) # Good Time Intervals
```
### Important Notes
1. **FITS Event Files (`hea` or `ogip`)**:
- `rmf_file` must be applied manually after loading:
```python
event_list.convert_pi_to_energy("example.rmf")
```
2. **Energy Calibration**:
- Ensure the file contains PI channel data for energy calibration.
- Without PI channels, RMF calibration will not work, and energy values will remain `None`.
3. **Good Time Intervals (GTIs)**:
- GTIs define valid observation periods and are automatically extracted from compatible files.
### Common Issues
- **Unsupported File Format**:
Ensure the file extension and format (`fmt`) match.
- **Energy Not Calibrated**:
Check for PI channels and provide an RMF file if needed.
- **Missing Columns**:
For OGIP/HEASOFT-compatible files, ensure required columns (e.g., `time`, `PI`) are available.
### Additional Parameters for Advanced Use
- **`additional_columns`**:
Specify extra columns to read from the file.
Example:
```python
event_list = EventList.read("example.fits", fmt="hea", additional_columns=["detector_id"])
```
<br><br>
"""
# Create the help box
return HelpBox(
title="Help Section",
tabs_content={
"Event Lists": pn.pane.Markdown(intro_content),
"Reading EventList": pn.pane.Markdown(eventlist_read_content),
},
)
def create_loadingdata_plots_area():
"""
Create the plots area for the data loading tab.
Returns:
PlotsContainer: An instance of PlotsContainer with the plots for the data loading tab.
Example:
>>> plots_area = create_loadingdata_plots_area()
>>> isinstance(plots_area, PlotsContainer)
True
"""
return PlotsContainer()
|