Skip to content

Commit 973a7b1

Browse files
author
Bert Vandenbroucke
committed
Added and applied formatting script.
1 parent b795830 commit 973a7b1

File tree

14 files changed

+144
-88
lines changed

14 files changed

+144
-88
lines changed

docs/source/conf.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ def run_apidoc(_):
8686

8787
api_doc_dir = os.path.join(cur_dir, "modules")
8888
module = os.path.join(cur_dir, "../..", "velociraptor")
89-
ignore = [
90-
os.path.join(cur_dir, "../..", "tests"),
91-
]
89+
ignore = [os.path.join(cur_dir, "../..", "tests")]
9290

9391
os.environ["SPHINX_APIDOC_OPTIONS"] = "members,undoc-members,show-inheritance"
9492
main(["-M", "-f", "-e", "-T", "-d 0", "-o", api_doc_dir, module, *ignore])

examples/auto_plotter.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
from velociraptor.autoplotter.metadata import AutoPlotterMetadata
1010
from velociraptor import load
1111

12-
catalogue = load("/Users/mphf18/Documents/science/halo_matching/ref/halo_2729.properties")
12+
catalogue = load(
13+
"/Users/mphf18/Documents/science/halo_matching/ref/halo_2729.properties"
14+
)
1315

1416
ap = AutoPlotter("auto_plotter_example.yml")
1517

@@ -18,4 +20,3 @@
1820

1921
metadata = AutoPlotterMetadata(auto_plotter=ap)
2022
metadata.write_metadata("test_auto_plotter/test_metadata.yml")
21-

examples/swift_integration.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,3 @@
7070
fig.savefig(f"{particle_type}_halo_image_{halo_id}.png")
7171

7272
plt.close(fig)
73-

format.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/bin/bash
2+
3+
# Check if we can run pip
4+
# This also serves as a check for python3
5+
python3 -m pip --version > /dev/null
6+
if [[ $? -ne 0 ]]
7+
then
8+
echo "ERROR: cannot run 'python3 -m pip'"
9+
exit 1
10+
fi
11+
12+
# Check if the virtual environment with black exists
13+
if [ ! -d black_formatting_env ]
14+
then
15+
echo "Formatting environment not found, installing it..."
16+
python3 -m venv black_formatting_env
17+
./black_formatting_env/bin/python3 -m pip install click==8.0.4 black==19.3b0
18+
fi
19+
# Now we know exactly which black to use
20+
black="./black_formatting_env/bin/python3 -m black"
21+
22+
# Formatting command
23+
cmd="$black -t py38 $(find . -name '*.py' -not -path './black_formatting_env/*')"
24+
25+
# Print the help
26+
function show_help {
27+
echo -e "This script formats all Python scripts using black"
28+
echo -e " -h, --help \t Show this help"
29+
echo -e " -c, --check \t Test if the Python scripts are well formatted"
30+
}
31+
32+
# Parse arguments
33+
TEST=0
34+
while [[ $# -gt 0 ]]
35+
do
36+
key="$1"
37+
38+
case $key in
39+
# print the help and exit
40+
-h|--help)
41+
show_help
42+
exit
43+
;;
44+
# check if the code is well formatted
45+
-c|--check)
46+
TEST=1
47+
shift
48+
;;
49+
# unknown option
50+
*)
51+
echo "Argument '$1' not implemented"
52+
show_help
53+
exit
54+
;;
55+
esac
56+
done
57+
58+
# Run the required commands
59+
if [[ $TEST -eq 1 ]]
60+
then
61+
# Note trapping the exit status from both commands in the pipe. Also note
62+
# do not use -q in grep as that closes the pipe on first match and we get
63+
# a SIGPIPE error.
64+
echo "Testing if Python scripts are correctly formatted"
65+
$cmd --check
66+
status=$?
67+
68+
# Check formatting
69+
if [[ ! ${status} -eq 0 ]]
70+
then
71+
echo "ERROR: needs formatting"
72+
exit 1
73+
else
74+
echo "Everything is correctly formatted"
75+
fi
76+
else
77+
echo "Formatting all Python scripts"
78+
$cmd
79+
fi

tests/test_observational_data.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,3 @@ def test_multi_obs():
128128
assert new_multi_z.get_datasets_overlapping_with([0.4, 0.6])[0].redshift == 0.5
129129

130130
os.remove("multi_z_test.hdf5")
131-

velociraptor/__init__.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def load(
3737
filename: str,
3838
disregard_units: bool = False,
3939
registration_file_path: Union[str, None] = None,
40-
mask: slice = Ellipsis
40+
mask: slice = Ellipsis,
4141
) -> VelociraptorCatalogue:
4242
"""
4343
Loads a velociraptor catalogue, producing a VelociraptorCatalogue
@@ -79,12 +79,10 @@ def load(
7979
"""
8080

8181
catalogue = VelociraptorCatalogue(
82-
filename,
83-
disregard_units=disregard_units,
84-
mask=mask
82+
filename, disregard_units=disregard_units, mask=mask
8583
)
8684

8785
if registration_file_path is not None:
8886
catalogue.register_derived_quantities(registration_file_path)
89-
87+
9088
return catalogue

velociraptor/autoplotter/objects.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ def _add_lines_to_axes(self, ax: Axes, x: unyt_array, y: unyt_array) -> None:
745745
return
746746

747747
def get_quantity_from_catalogue_with_mask(
748-
self, quantity: str, catalogue: VelociraptorCatalogue,
748+
self, quantity: str, catalogue: VelociraptorCatalogue
749749
) -> unyt_array:
750750
"""
751751
Get a quantity from the catalogue using the mask.
@@ -755,17 +755,17 @@ def get_quantity_from_catalogue_with_mask(
755755
# We give each dataset a custom name, that gets ruined when masking
756756
# in versions of unyt less than 2.6.0
757757
name = x.name
758-
758+
759759
if self.structure_mask is not None:
760760
# if structure_mask already set, mask and return
761761
x_mask = logical_and(self.global_mask, self.structure_mask)
762762
x = x[x_mask]
763763
x.name = name
764764
return x
765-
765+
766766
# allow all entries by default
767767
self.structure_mask = ones(x.shape).astype(bool)
768-
768+
769769
if self.selection_mask is not None:
770770
# Create mask
771771
self.structure_mask = reduce(
@@ -779,24 +779,22 @@ def get_quantity_from_catalogue_with_mask(
779779
)
780780
self.structure_mask = logical_and(
781781
self.structure_mask,
782-
catalogue.structure_type.structuretype
783-
== self.select_structure_type,
782+
catalogue.structure_type.structuretype == self.select_structure_type,
784783
)
785784
if self.exclude_structure_type is not None:
786785
self.structure_mask = logical_and(
787786
self.structure_mask,
788-
catalogue.structure_type.structuretype
789-
!= self.exclude_structure_type,
787+
catalogue.structure_type.structuretype != self.exclude_structure_type,
790788
)
791-
789+
792790
# combine global and structure masks
793791
x_mask = logical_and(self.global_mask, self.structure_mask)
794792

795793
# apply to the unyt array of values
796794
x = x[x_mask]
797795
x.name = name
798796
return x
799-
797+
800798
def _make_plot_scatter(
801799
self, catalogue: VelociraptorCatalogue
802800
) -> Tuple[Figure, Axes]:
@@ -962,7 +960,7 @@ def _make_plot_cumulative_histogram(
962960
return fig, ax
963961

964962
def make_plot(
965-
self, catalogue: VelociraptorCatalogue, directory: str, file_extension: str,
963+
self, catalogue: VelociraptorCatalogue, directory: str, file_extension: str
966964
):
967965
"""
968966
Federates out data parsing to individual functions based on the
@@ -1048,7 +1046,7 @@ class AutoPlotter(object):
10481046
created_successfully: List[bool]
10491047
# global mask
10501048
global_mask: Union[None, array]
1051-
1049+
10521050
def __init__(
10531051
self,
10541052
filename: Union[str, List[str]],
@@ -1113,7 +1111,9 @@ def parse_yaml(self):
11131111

11141112
return
11151113

1116-
def link_catalogue(self, catalogue: VelociraptorCatalogue, global_mask_tag: Union[None, str]):
1114+
def link_catalogue(
1115+
self, catalogue: VelociraptorCatalogue, global_mask_tag: Union[None, str]
1116+
):
11171117
"""
11181118
Links a catalogue with this object so that the plots
11191119
can actually be created.

velociraptor/catalogue/catalogue.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,7 @@ def register_field_properties(self):
8888
return
8989

9090

91-
def generate_getter(
92-
filename,
93-
name: str,
94-
field: str,
95-
full_name: str,
96-
unit
97-
):
91+
def generate_getter(filename, name: str, field: str, full_name: str, unit):
9892
"""
9993
Generates a function that:
10094
@@ -122,7 +116,9 @@ def getter(self):
122116
with h5py.File(filename, "r") as handle:
123117
try:
124118
mask = getattr(self, "mask")
125-
setattr(self, f"_{name}", unyt.unyt_array(handle[field][mask], unit))
119+
setattr(
120+
self, f"_{name}", unyt.unyt_array(handle[field][mask], unit)
121+
)
126122
getattr(self, f"_{name}").name = full_name
127123
getattr(self, f"_{name}").file = filename
128124
except KeyError:
@@ -168,7 +164,7 @@ def generate_sub_catalogue(
168164
registration_function: Callable,
169165
units: VelociraptorUnits,
170166
field_metadata: List[VelociraptorFieldMetadata],
171-
mask: slice = Ellipsis
167+
mask: slice = Ellipsis,
172168
):
173169
"""
174170
Generates a sub-catalogue object with the correct properties set.
@@ -181,10 +177,7 @@ def generate_sub_catalogue(
181177
"""
182178

183179
# This creates a _copy_ of the _class_, not object.
184-
this_sub_catalogue_bases = (
185-
__VelociraptorSubCatalogue,
186-
object,
187-
)
180+
this_sub_catalogue_bases = (__VelociraptorSubCatalogue, object)
188181
this_sub_catalogue_dict = {}
189182

190183
valid_sub_paths = []
@@ -198,7 +191,7 @@ def generate_sub_catalogue(
198191
metadata.snake_case,
199192
metadata.path,
200193
metadata.name,
201-
metadata.unit
194+
metadata.unit,
202195
),
203196
generate_setter(metadata.snake_case),
204197
generate_deleter(metadata.snake_case),
@@ -411,7 +404,7 @@ def __create_sub_catalogues(self):
411404
registration_function=self.registration_functions[attribute_name],
412405
units=self.units,
413406
field_metadata=field_metadata,
414-
mask=self.mask
407+
mask=self.mask,
415408
),
416409
)
417410

velociraptor/catalogue/registration.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ def registration_fail_all(
3737
+ name: A fancy (possibly LaTeX'd) name for the field.
3838
+ snake_case: A correct snake_case name for the field.
3939
"""
40-
41-
40+
4241
if field_path == "ThisFieldPathWouldNeverExist":
4342
return (
4443
unit_system.length,
@@ -1042,10 +1041,10 @@ def registration_cold_dense_gas_properties(
10421041
aperture_size = match.group(2)
10431042

10441043
try:
1045-
long_quantity = {"DiffuseMetal": "Diffuse Metal", "Gas": "Gas",}[
1044+
long_quantity = {"DiffuseMetal": "Diffuse Metal", "Gas": "Gas"}[
10461045
quantity_key
10471046
]
1048-
short_quantity = {"DiffuseMetal": "diffuse_metal", "Gas": "gas",}[
1047+
short_quantity = {"DiffuseMetal": "diffuse_metal", "Gas": "gas"}[
10491048
quantity_key
10501049
]
10511050
except KeyError:
@@ -1097,8 +1096,8 @@ def registration_log_element_ratios_times_masses(
10971096
"LogIronOverHydrogen": "Fe/H",
10981097
}[long_species]
10991098

1100-
short_floortype = {"Lo": "lowfloor", "Hi": "highfloor",}[floor_type]
1101-
floor_value = {"Lo": "-4", "Hi": "-3",}[floor_type]
1099+
short_floortype = {"Lo": "lowfloor", "Hi": "highfloor"}[floor_type]
1100+
floor_value = {"Lo": "-4", "Hi": "-3"}[floor_type]
11021101
except KeyError:
11031102
raise RegistrationDoesNotMatchError
11041103

@@ -1482,7 +1481,7 @@ def registration_element_masses_in_stars(
14821481

14831482
return
14841483

1485-
1484+
14861485
# TODO
14871486
# lambda_B
14881487
# q

0 commit comments

Comments
 (0)