Skip to content

Commit 54b0a87

Browse files
authored
Merge pull request #95 from SWIFTSIM/soap-catalogue
SOAP catalogue
2 parents cfd0dc3 + eaf4cd1 commit 54b0a87

15 files changed

+2623
-490
lines changed

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ tqdm
77
pyyaml
88
black
99
matplotlib
10+
swiftsimio>=6.1.0

tests/helper.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
Contains helper functions for the test routines.
3+
"""
4+
5+
import subprocess
6+
import os
7+
8+
webstorage_location = "http://virgodb.cosma.dur.ac.uk/swift-webstorage/IOExamples/"
9+
test_data_location = "test_data/"
10+
11+
12+
def requires(filename):
13+
"""
14+
Use this as a decorator around tests that require data.
15+
"""
16+
17+
# First check if the test data directory exists
18+
if not os.path.exists(test_data_location):
19+
os.mkdir(test_data_location)
20+
21+
file_location = f"{test_data_location}{filename}"
22+
23+
if os.path.exists(file_location):
24+
ret = 0
25+
else:
26+
# Download it!
27+
ret = subprocess.call(
28+
["wget", f"{webstorage_location}{filename}", "-O", file_location]
29+
)
30+
31+
if ret != 0:
32+
Warning(f"Unable to download file at {filename}")
33+
# It wrote an empty file, kill it.
34+
subprocess.call(["rm", file_location])
35+
36+
def dont_call_test(func):
37+
def empty(*args, **kwargs):
38+
return True
39+
40+
return empty
41+
42+
return dont_call_test
43+
44+
else:
45+
# Woo, we can do the test!
46+
47+
def do_call_test(func):
48+
def final_test():
49+
# Whack the path on there for good measure.
50+
return func(f"{test_data_location}{filename}")
51+
52+
return final_test
53+
54+
return do_call_test
55+
56+
raise Exception("You should never have got here.")

tests/test_load_catalogue.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
"""
44

55
from velociraptor import load
6+
from helper import requires
67

78

8-
def test_basic_load_catalogue_no_crash(
9-
filename="/Users/mphf18/Desktop/halo_027_z00p101.properties",
10-
):
9+
@requires("cosmo_0000.properties")
10+
def test_basic_load_catalogue_no_crash(filename="test_data/cosmo_0000.properties",):
1111
catalogue = load(filename)
1212

1313
return

velociraptor/__init__.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
2-
The velociraptor module.
2+
The velociraptor module.
33
4-
More information is available in the documnetation.
4+
More information is available in the documentation.
55
"""
66

77
# First things first, we need to upgrade msun and mh from a symbol to a
@@ -27,7 +27,9 @@
2727
pass
2828

2929

30-
from velociraptor.catalogue.catalogue import VelociraptorCatalogue
30+
from velociraptor.catalogue.catalogue import Catalogue, CatalogueTypeError
31+
from velociraptor.catalogue.velociraptor_catalogue import VelociraptorCatalogue
32+
from velociraptor.catalogue.soap_catalogue import SOAPCatalogue
3133
from velociraptor.__version__ import __version__
3234

3335
from typing import Union, List
@@ -38,10 +40,9 @@ def load(
3840
disregard_units: bool = False,
3941
registration_file_path: Union[List[str], str, None] = None,
4042
mask: slice = Ellipsis,
41-
) -> VelociraptorCatalogue:
43+
) -> Catalogue:
4244
"""
43-
Loads a velociraptor catalogue, producing a VelociraptorCatalogue
44-
object.
45+
Loads a velociraptor catalogue, producing a Catalogue object.
4546
4647
Parameters
4748
----------
@@ -73,14 +74,16 @@ def load(
7374
Returns
7475
-------
7576
76-
VelociraptorCatalogue
77-
The VelociraptorCatalogue object that describes your
78-
.properties file.
77+
Catalogue
78+
The Catalogue object that describes your .properties file.
7979
"""
8080

81-
catalogue = VelociraptorCatalogue(
82-
filename, disregard_units=disregard_units, mask=mask
83-
)
81+
try:
82+
catalogue = VelociraptorCatalogue(
83+
filename, disregard_units=disregard_units, mask=mask
84+
)
85+
except CatalogueTypeError:
86+
catalogue = SOAPCatalogue(filename)
8487

8588
if registration_file_path is not None:
8689
catalogue.register_derived_quantities(registration_file_path)

velociraptor/autoplotter/compare.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
from velociraptor.autoplotter.objects import AutoPlotter
2222
from velociraptor.autoplotter.metadata import AutoPlotterMetadata
2323
from velociraptor.observations import load_observations
24+
from velociraptor.catalogue.catalogue import Catalogue
2425

2526

26-
class FakeCatalogue(object):
27+
class FakeCatalogue(Catalogue):
2728
"""
28-
Fake VelociraptorCatalogue used to store redshift and
29+
Fake Catalogue used to store redshift and
2930
scale factor information if available.
3031
"""
3132

velociraptor/autoplotter/objects.py

+37-34
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Main objects for holding information relating to the autoplotter.
33
"""
44

5-
from velociraptor import VelociraptorCatalogue
5+
from velociraptor import Catalogue
66
from velociraptor.autoplotter.lines import VelociraptorLine, valid_line_types
77
from velociraptor.autoplotter.box_size_correction import VelociraptorBoxSizeCorrection
88
from velociraptor.exceptions import AutoPlotterError
@@ -19,7 +19,6 @@
1919
from pathlib import Path
2020

2121
from os import path, mkdir
22-
from functools import reduce
2322
from collections import OrderedDict
2423

2524
valid_plot_types = [
@@ -767,13 +766,13 @@ def _add_lines_to_axes(self, ax: Axes, x: unyt_array, y: unyt_array) -> None:
767766
return
768767

769768
def get_quantity_from_catalogue_with_mask(
770-
self, quantity: str, catalogue: VelociraptorCatalogue
769+
self, quantity: str, catalogue: Catalogue
771770
) -> unyt_array:
772771
"""
773772
Get a quantity from the catalogue using the mask.
774773
"""
775774

776-
x = reduce(getattr, quantity.split("."), catalogue)
775+
x = catalogue.get_quantity(quantity)
777776
# We give each dataset a custom name, that gets ruined when masking
778777
# in versions of unyt less than 2.6.0
779778
name = x.name
@@ -790,9 +789,9 @@ def get_quantity_from_catalogue_with_mask(
790789

791790
if self.selection_mask is not None:
792791
# Create mask
793-
self.structure_mask = reduce(
794-
getattr, self.selection_mask.split("."), catalogue
795-
).astype(bool)
792+
self.structure_mask = catalogue.get_quantity(self.selection_mask).astype(
793+
bool
794+
)
796795
if self.select_structure_type is not None:
797796
if self.select_structure_type == self.exclude_structure_type:
798797
raise AutoPlotterError(
@@ -801,12 +800,14 @@ def get_quantity_from_catalogue_with_mask(
801800
)
802801
self.structure_mask = logical_and(
803802
self.structure_mask,
804-
catalogue.structure_type.structuretype == self.select_structure_type,
803+
catalogue.get_quantity("structure_type.structuretype")
804+
== self.select_structure_type,
805805
)
806806
if self.exclude_structure_type is not None:
807807
self.structure_mask = logical_and(
808808
self.structure_mask,
809-
catalogue.structure_type.structuretype != self.exclude_structure_type,
809+
catalogue.get_quantity("structure_type.structuretype")
810+
!= self.exclude_structure_type,
810811
)
811812

812813
# combine global and structure masks
@@ -817,9 +818,7 @@ def get_quantity_from_catalogue_with_mask(
817818
x.name = name
818819
return x
819820

820-
def _make_plot_scatter(
821-
self, catalogue: VelociraptorCatalogue
822-
) -> Tuple[Figure, Axes]:
821+
def _make_plot_scatter(self, catalogue: Catalogue) -> Tuple[Figure, Axes]:
823822
"""
824823
Makes a scatter plot and returns the figure and axes.
825824
"""
@@ -835,9 +834,7 @@ def _make_plot_scatter(
835834

836835
return fig, ax
837836

838-
def _make_plot_2dhistogram(
839-
self, catalogue: VelociraptorCatalogue
840-
) -> Tuple[Figure, Axes]:
837+
def _make_plot_2dhistogram(self, catalogue: Catalogue) -> Tuple[Figure, Axes]:
841838
"""
842839
Makes a 2d histogram plot and returns the figure and axes.
843840
"""
@@ -855,9 +852,7 @@ def _make_plot_2dhistogram(
855852

856853
return fig, ax
857854

858-
def _make_plot_massfunction(
859-
self, catalogue: VelociraptorCatalogue
860-
) -> Tuple[Figure, Axes]:
855+
def _make_plot_massfunction(self, catalogue: Catalogue) -> Tuple[Figure, Axes]:
861856
"""
862857
Makes a mass function plot and returns the figure and axes.
863858
"""
@@ -888,15 +883,15 @@ def _make_plot_massfunction(
888883
return fig, ax
889884

890885
def _make_plot_adaptivemassfunction(
891-
self, catalogue: VelociraptorCatalogue
886+
self, catalogue: Catalogue
892887
) -> Tuple[Figure, Axes]:
893888
"""
894889
Makes the _adaptive_ mass function plot. Same as mass function.
895890
"""
896891
return self._make_plot_massfunction(catalogue=catalogue)
897892

898893
def _make_plot_luminosityfunction(
899-
self, catalogue: VelociraptorCatalogue
894+
self, catalogue: Catalogue
900895
) -> Tuple[Figure, Axes]:
901896
"""
902897
Makes a luminosity function plot and returns the figure and axes.
@@ -927,9 +922,7 @@ def _make_plot_luminosityfunction(
927922

928923
return fig, ax
929924

930-
def _make_plot_histogram(
931-
self, catalogue: VelociraptorCatalogue
932-
) -> Tuple[Figure, Axes]:
925+
def _make_plot_histogram(self, catalogue: Catalogue) -> Tuple[Figure, Axes]:
933926
"""
934927
Make histogram plot and return the figure and axes.
935928
"""
@@ -950,7 +943,7 @@ def _make_plot_histogram(
950943
return fig, ax
951944

952945
def _make_plot_cumulative_histogram(
953-
self, catalogue: VelociraptorCatalogue
946+
self, catalogue: Catalogue
954947
) -> Tuple[Figure, Axes]:
955948
"""
956949
Make cumulative histogram plot and return the figure and axes.
@@ -984,7 +977,7 @@ def _make_plot_cumulative_histogram(
984977

985978
def make_plot(
986979
self,
987-
catalogue: VelociraptorCatalogue,
980+
catalogue: Catalogue,
988981
directory: str,
989982
file_extension: str,
990983
no_plot: bool = False,
@@ -1065,7 +1058,7 @@ class AutoPlotter(object):
10651058
# Forward declarations
10661059
filename: Union[str, List[str]]
10671060
multiple_yaml_files: bool
1068-
catalogue: VelociraptorCatalogue
1061+
catalogue: Catalogue
10691062
yaml: Dict[str, Union[Dict, str]]
10701063
plots: List[VelociraptorPlot]
10711064
# Directory containing the observational data.
@@ -1150,9 +1143,7 @@ def parse_yaml(self):
11501143

11511144
return
11521145

1153-
def link_catalogue(
1154-
self, catalogue: VelociraptorCatalogue, global_mask_tag: Union[None, str]
1155-
):
1146+
def link_catalogue(self, catalogue: Catalogue, global_mask_tag: Union[None, str]):
11561147
"""
11571148
Links a catalogue with this object so that the plots
11581149
can actually be created.
@@ -1161,7 +1152,7 @@ def link_catalogue(
11611152
self.catalogue = catalogue
11621153

11631154
if global_mask_tag is not None:
1164-
self.global_mask = reduce(getattr, global_mask_tag.split("."), catalogue)
1155+
self.global_mask = catalogue.get_quantity(global_mask_tag)
11651156
else:
11661157
self.global_mask = True
11671158
return
@@ -1202,8 +1193,8 @@ def create_plots(
12021193
import sys, traceback
12031194

12041195
_, _, exc_traceback = sys.exc_info()
1205-
print("Traceback:")
1206-
traceback.print_tb(exc_traceback, limit=10, file=sys.stdout)
1196+
print("Traceback:", file=sys.stderr)
1197+
traceback.print_tb(exc_traceback, limit=10, file=sys.stderr)
12071198
except UnitConversionError as e:
12081199
print(
12091200
f"Unable to create plot {plot.filename} due to an error when "
@@ -1218,7 +1209,19 @@ def create_plots(
12181209
import sys, traceback
12191210

12201211
_, _, exc_traceback = sys.exc_info()
1221-
print("Traceback:")
1222-
traceback.print_tb(exc_traceback, limit=10, file=sys.stdout)
1212+
print("Traceback:", file=sys.stderr)
1213+
traceback.print_tb(exc_traceback, limit=10, file=sys.stderr)
1214+
except Exception as e:
1215+
print(
1216+
f"Unable to create plot {plot.filename} due to an unkown error: {e}!",
1217+
file=sys.stderr,
1218+
)
1219+
self.created_successfully.append(False)
1220+
if debug:
1221+
import sys, traceback
1222+
1223+
_, _, exc_traceback = sys.exc_info()
1224+
print("Traceback:", file=sys.stderr)
1225+
traceback.print_tb(exc_traceback, limit=10, file=sys.stderr)
12231226

12241227
return

velociraptor/autoplotter/plot.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from matplotlib import rcParams
1010

1111
from matplotlib.colors import LogNorm
12-
from velociraptor import VelociraptorCatalogue
12+
from velociraptor import Catalogue
1313
from velociraptor.autoplotter.objects import VelociraptorLine
1414
from typing import Tuple, Union
1515

@@ -124,7 +124,7 @@ def histogram(
124124

125125
def decorate_axes(
126126
ax: plt.Axes,
127-
catalogue: VelociraptorCatalogue,
127+
catalogue: Catalogue,
128128
comment: Union[str, None] = None,
129129
legend_loc: str = "upper left",
130130
redshift_loc: str = "lower right",

0 commit comments

Comments
 (0)