Skip to content

Commit

Permalink
Fix bug in openProject function
Browse files Browse the repository at this point in the history
  • Loading branch information
mauritiusdadd committed Mar 19, 2024
1 parent a9758fc commit c7d89bd
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 122 deletions.
73 changes: 50 additions & 23 deletions src/redmost/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(self, *args: Any) -> None:
"width": 1.5
}

self.horizontall_lock: bool = False
self.horizontal_lock: bool = False
self.master: bool = False
self.show_lines: bool = False
self.sync_height: bool = False
Expand Down Expand Up @@ -159,7 +159,7 @@ def drawForeground(
pen.setWidthF(1.5)
pen.setDashPattern((2,2,2,2))
painter.setPen(pen)
print(">>>>", self._mouse_lambda)

mouse_line_x = self.chart().mapToPosition(
qt_api.QtCore.QPointF(self._mouse_lambda, 0.0)
).x()
Expand Down Expand Up @@ -343,9 +343,9 @@ def scroll(self, dx: float, dy: float) -> None:

chart = self.chart()

if self.vertical_lock and self.horizontall_lock:
if self.vertical_lock and self.horizontal_lock:
return
elif not (self.vertical_lock or self.horizontall_lock):
elif not (self.vertical_lock or self.horizontal_lock):
chart.scroll(dx, dy)
elif self.vertical_lock:
chart.scroll(dx, 0)
Expand All @@ -367,7 +367,7 @@ def setAxesRange(
return
x_axis, y_axis = axes[0:2]

if not self.horizontall_lock:
if not self.horizontal_lock:
if x_min is not None:
x_axis.setMin(x_min)
if x_max is not None:
Expand Down Expand Up @@ -528,7 +528,7 @@ def zoom(
if y_center is None:
y_center = rect.height()/2

if not self.horizontall_lock:
if not self.horizontal_lock:
width_original = rect.width()
rect.setWidth(width_original / value)
center_scale_x = x_center / width_original
Expand Down Expand Up @@ -2062,7 +2062,7 @@ def doImportSpectra(self, *args: Any, **kwargs: Any) -> None:
new_item.setData(qt_api.QtCore.Qt.ItemDataRole.UserRole, item_uuid)

info_dict: Dict[str, Any] = {
'redshift': None,
'redshift': 0,
'quality_flag': 0,
'lines': {
'list': [],
Expand Down Expand Up @@ -2588,23 +2588,40 @@ def openProject(self, file_name: str) -> None:
item_uuid = uuid.UUID(file_info['uuid'])
item_row = int(file_info['index'])

file_path = os.path.realpath(
os.path.join(
os.path.dirname(file_name),
file_info['path']
)
)

if not os.path.exists(file_path):
if os.path.exists(file_info['path']):
file_path = file_info['path']
else:
exception_tracker[item_uuid] = (
file_path,
f"File {file_path} does not exist!"
)
continue

try:
sp: Spectrum1D = loaders.read(file_info['path'])
sp: Spectrum1D = loaders.read(file_path)
except Exception as exc:
exception_tracker[item_uuid] = (file_info['path'], str(exc))
exception_tracker[item_uuid] = (file_path, str(exc))
continue

new_item = qt_api.QtWidgets.QListWidgetItem(file_info['text'])
new_item.setCheckState(qt_api.QtCore.Qt.CheckState(file_info['checked']))
new_item.setToolTip(file_info['path'])
new_item.setToolTip(file_path)
new_item.setData(qt_api.QtCore.Qt.ItemDataRole.UserRole, item_uuid)

if item_uuid == current_uuid:
current_spec_list_item = new_item

self.main_wnd.spec_list_widget.addItem(new_item)
self.open_spectra[item_uuid] = sp
self.open_spectra_files[item_uuid] = file_info['path']
self.open_spectra_files[item_uuid] = file_path
self.open_spectra_items[item_uuid] = new_item
self.main_wnd.spec_list_widget.insertItem(item_row, new_item)
self.qapp.processEvents()
Expand Down Expand Up @@ -2705,6 +2722,7 @@ def saveProject(self, file_name: str) -> None:
# Serialize program state for json dumping
serialized_open_file_list = []
item: Union[qt_api.QtWidgets.QListWidgetItem, None]
proj_path = os.path.dirname(file_name)
for k in range(self.main_wnd.spec_list_widget.count()):
item = self.main_wnd.spec_list_widget.item(k)
if item is None:
Expand All @@ -2717,7 +2735,10 @@ def saveProject(self, file_name: str) -> None:
'index': k,
'uuid': item_uuid.hex,
'text': item.text(),
'path': self.open_spectra_files[item_uuid],
'path': os.path.relpath(
self.open_spectra_files[item_uuid],
proj_path
),
'checked': int(item.checkState().value) # type: ignore
}

Expand All @@ -2730,19 +2751,25 @@ def saveProject(self, file_name: str) -> None:
serialized_z_list: List[Dict[str, Any]] = []

for line_info in obj_info['lines']['list']:
serialized_lines_list.append({
'row': int(line_info['row']),
'data': float(line_info['data']),
'text': str(line_info['text']),
'checked': int(line_info['checked'].value)
})
try:
serialized_lines_list.append({
'row': int(line_info['row']),
'data': float(line_info['data']),
'text': str(line_info['text']),
'checked': int(line_info['checked'].value)
})
except (TypeError, ValueError):
continue

for z_info in obj_info['lines']['redshifts']:
serialized_z_list.append({
'row': int(z_info['row']),
'text': str(z_info['text']),
'data': z_info['data']
})
try:
serialized_z_list.append({
'row': int(z_info['row']),
'text': str(z_info['text']),
'data': z_info['data']
})
except (TypeError, ValueError):
continue

serialized_info_dict: Dict[str, Any] = {
'redshift': float(obj_info['redshift']),
Expand Down
138 changes: 40 additions & 98 deletions test/data/test_project_1.json
Original file line number Diff line number Diff line change
@@ -1,134 +1,76 @@
{
"current_uuid": "d88e4949012247e596f080f55d3fd922",
"current_uuid": "b950d9990c5341579957dbe9d7236e27",
"open_files": [
{
"index": 0,
"uuid": "7b4dff55c0bd4aa3bbba255f0e363b19",
"text": "spec_M0416_10.fits",
"path": "/home/daddona/Downloads/refilelistesempiopandora_ez/spec_M0416_10.fits",
"uuid": "ee897121174640888b862e441ee6f0f6",
"text": "spec_95.fits",
"path": "./test/data/spec_95.fits",
"checked": 2
},
{
"index": 1,
"uuid": "2c877880a5fb44fcac896536c14ff63e",
"text": "spec_M0416_11701.fits",
"path": "/home/daddona/Downloads/refilelistesempiopandora_ez/spec_M0416_11701.fits",
"uuid": "637b29cd33ea48a1ad2023e91a60b47f",
"text": "spec_955.fits",
"path": "./test/data/spec_955.fits",
"checked": 2
},
{
"index": 2,
"uuid": "9300a75b0b684d8193eef071ede0250a",
"text": "spec_M0416_118.fits",
"path": "/home/daddona/Downloads/refilelistesempiopandora_ez/spec_M0416_118.fits",
"uuid": "f22874a92a0645a992fd9a7ca36a9c55",
"text": "spec_963.fits",
"path": "./test/data/spec_963.fits",
"checked": 2
},
{
"index": 3,
"uuid": "07175839ef01496aa99fec553d43f2fc",
"text": "spec_M0416_12300.fits",
"path": "/home/daddona/Downloads/refilelistesempiopandora_ez/spec_M0416_12300.fits",
"uuid": "74dee251bbf444cf9d947e00f951fcfc",
"text": "spec_965.fits",
"path": "./test/data/spec_965.fits",
"checked": 2
},
{
"index": 4,
"uuid": "9d49ea396bdb467d943f2908c5668874",
"text": "spec_M0416_1233.fits",
"path": "/home/daddona/Downloads/refilelistesempiopandora_ez/spec_M0416_1233.fits",
"uuid": "6614b0a71ccc4fa4b653509b7feb7272",
"text": "spec_966.fits",
"path": "./test/data/spec_966.fits",
"checked": 2
},
{
"index": 5,
"uuid": "d88e4949012247e596f080f55d3fd922",
"text": "spec_M0416_1303.fits",
"path": "/home/daddona/Downloads/refilelistesempiopandora_ez/spec_M0416_1303.fits",
"uuid": "855d157d44f34610a0b35266fe310651",
"text": "spec_971.fits",
"path": "./test/data/spec_971.fits",
"checked": 2
},
{
"index": 6,
"uuid": "f55e0a9a45d7440fb99fa4767a202303",
"text": "spec_M0416_13645.fits",
"path": "/home/daddona/Downloads/refilelistesempiopandora_ez/spec_M0416_13645.fits",
"uuid": "d9b9d10d8e2442f8bccf227b5edc3f24",
"text": "spec_988.fits",
"path": "./test/data/spec_988.fits",
"checked": 2
},
{
"index": 7,
"uuid": "10cc9306b28941738ea138ff099d5eaa",
"text": "spec_M0416_13919.fits",
"path": "/home/daddona/Downloads/refilelistesempiopandora_ez/spec_M0416_13919.fits",
"uuid": "55cd42374531496294a56ce2bc124b80",
"text": "spec_991.fits",
"path": "./test/data/spec_991.fits",
"checked": 2
}
],
"objects_properties": {
"7b4dff55c0bd4aa3bbba255f0e363b19": {
"redshift": 2.7609999999999997,
"quality_flag": 3,
"lines": {
"list": [],
"redshifts": []
}
},
"2c877880a5fb44fcac896536c14ff63e": {
"redshift": 0.0,
"quality_flag": 3,
"lines": {
"list": [],
"redshifts": []
}
},
"9300a75b0b684d8193eef071ede0250a": {
"redshift": 0.6716,
"quality_flag": 2,
"lines": {
"list": [],
"redshifts": []
}
},
"07175839ef01496aa99fec553d43f2fc": {
"redshift": 4.3231,
"quality_flag": 2,
"lines": {
"list": [],
"redshifts": []
}
},
"9d49ea396bdb467d943f2908c5668874": {
"redshift": 0.1983,
"quality_flag": 3,
"lines": {
"list": [],
"redshifts": []
}
},
"d88e4949012247e596f080f55d3fd922": {
"redshift": 0.8189,
"quality_flag": 4,
"lines": {
"list": [
{
"row": 0,
"data": 6779.873319885274,
"text": "6779.87 A",
"checked": 2
}
],
"redshifts": []
}
},
"f55e0a9a45d7440fb99fa4767a202303": {
"redshift": 0.0,
"quality_flag": 3,
"lines": {
"list": [],
"redshifts": []
}
{
"index": 8,
"uuid": "04bdfd679d2f41cc8dce28b8ba3d87a4",
"text": "spec_994.fits",
"path": "./test/data/spec_994.fits",
"checked": 2
},
"10cc9306b28941738ea138ff099d5eaa": {
"redshift": 1.3431,
"quality_flag": 1,
"lines": {
"list": [],
"redshifts": []
}
{
"index": 9,
"uuid": "b950d9990c5341579957dbe9d7236e27",
"text": "spec_997.fits",
"path": "./test/data/spec_997.fits",
"checked": 2
}
}
}
],
"objects_properties": {}
}
2 changes: 1 addition & 1 deletion test/test_pyqt_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_load_project(qtbot: QtBot, main_app: gui.GuiApp):
save_screen(qtbot, main_app.main_wnd)


def test_load_project_import_zcar(qtbot: QtBot, main_app: gui.GuiApp):
def test_load_project_import_zcat(qtbot: QtBot, main_app: gui.GuiApp):
print("Loading a project")
main_app.openProject(PROJECT_1_FILE)
main_app._doImportZcat(ZCAT_FILE)
Expand Down

0 comments on commit c7d89bd

Please sign in to comment.