Skip to content

Commit

Permalink
fix: changing isinstance to issubclass
Browse files Browse the repository at this point in the history
was calling isinstance incorrectly, but it makes more sense to use subclass. Although beartype won't let that.
  • Loading branch information
lachlangrose committed Feb 9, 2024
1 parent 503170c commit 0e9ecc8
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 24 deletions.
37 changes: 37 additions & 0 deletions docs/source/sg_execution_times.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

:orphan:

.. _sphx_glr_sg_execution_times:


Computation times
=================
**00:00.009** total execution time for 1 file **from all galleries**:

.. container::

.. raw:: html

<style scoped>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
</style>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
<script type="text/javascript" class="init">
$(document).ready( function () {
$('table.sg-datatable').DataTable({order: [[1, 'desc']]});
} );
</script>

.. list-table::
:header-rows: 1
:class: table table-striped sg-datatable

* - Example
- Time
- Mem (MB)
* - :ref:`sphx_glr__auto_examples_plot_hamersley.py` (``..\..\examples\plot_hamersley.py``)
- 00:00.009
- 0.0
12 changes: 6 additions & 6 deletions map2loop/deformation_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ def findfault(self, id):
Returns:
pandas.DataFrame: The sliced data frame containing the requested fault
"""
if isinstance(type(id), int):
if issubclass(type(id), int):
return self.faults[self.faults["eventId"] == id]
elif isinstance(type(id), str):
elif issubclass(type(id), str):
return self.faults[self.faults["name"] == id]
else:
print("ERROR: Unknown identifier type used to find fault")
Expand All @@ -130,9 +130,9 @@ def findfold(self, id):
Returns:
pandas.DataFrame: The sliced data frame containing the requested fold
"""
if isinstance(type(id), int):
if issubclass(type(id), int):
return self.folds[self.folds["foldId"] == id]
elif isinstance(type(id), str):
elif issubclass(type(id), str):
return self.folds[self.folds["name"] == id]
else:
print("ERROR: Unknown identifier type used to find fold")
Expand All @@ -145,7 +145,7 @@ def addFault(self, fault):
fault (pandas.DataFrame or dict):
The fault information to add
"""
if isinstance(type(fault), pandas.DataFrame) or isinstance(type(fault), dict):
if issubclass(type(fault), pandas.DataFrame) or issubclass(type(fault), dict):
if "name" in fault.keys():
if fault["name"] in self.faults.index:
print("Replacing fault", fault["name"])
Expand Down Expand Up @@ -183,7 +183,7 @@ def addFold(self, fold):
fold (pandas.DataFrame or dict):
The fold information to add
"""
if isinstance(type(fold), pandas.DataFrame) or isinstance(type(fold), dict):
if issubclass(type(fold), pandas.DataFrame) or issubclass(type(fold), dict):
if "name" in fold.keys():
if fold["name"] in self.folds.index:
print("Replacing fold", fold["name"])
Expand Down
12 changes: 6 additions & 6 deletions map2loop/mapdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ def set_working_projection(self, projection):
projection (int or str):
The projection to use for map reprojection
"""
if isinstance(type(projection), int):
if issubclass(type(projection), int):
projection = "EPSG:" + str(projection)
self.working_projection = projection
elif isinstance(type(projection), str):
elif issubclass(type(projection), str):
self.working_projection = projection
else:
print(
Expand All @@ -124,7 +124,7 @@ def set_bounding_box(self, bounding_box):
The bounding box to use for maps
"""
# Convert tuple bounding_box to dict else assign directly
if isinstance(type(bounding_box), tuple):
if issubclass(type(bounding_box), tuple):
self.bounding_box = {
"minx": bounding_box[0],
"maxx": bounding_box[1],
Expand All @@ -134,7 +134,7 @@ def set_bounding_box(self, bounding_box):
if len(bounding_box) == 6:
self.bounding_box["top"] = bounding_box[4]
self.bounding_box["base"] = bounding_box[5]
elif isinstance(type(bounding_box), dict):
elif issubclass(type(bounding_box), dict):
self.bounding_box = bounding_box
else:
raise TypeError(f"Invalid type for bounding_box {type(bounding_box)}")
Expand Down Expand Up @@ -1296,8 +1296,8 @@ def extract_all_contacts(self, save_contacts=True):
geology = self.get_map_data(Datatype.GEOLOGY).copy()
geology = geology.dissolve(by="UNITNAME", as_index=False)
# Remove intrusions
geology = geology[geology["INTRUSIVE"] is False]
geology = geology[geology["SILL"] is False]
geology = geology[~geology["INTRUSIVE"]]
geology = geology[~geology["SILL"]]
# Remove faults from contact geomety
if self.get_map_data(Datatype.FAULT) is not None:
faults = self.get_map_data(Datatype.FAULT).copy()
Expand Down
7 changes: 4 additions & 3 deletions map2loop/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def __init__(
config_filename = kwargs["metadata_filename"]

# Sanity check on working projection parameter
if isinstance(type(working_projection), str) or isinstance(type(working_projection), int):
if issubclass(type(working_projection), str) or issubclass(type(working_projection), int):
self.map_data.set_working_projection(working_projection)
elif type(working_projection) is None:
if verbose_level != VerboseLevel.NONE:
Expand All @@ -140,7 +140,8 @@ def __init__(
raise TypeError(f"Invalid type for working_projection {type(working_projection)}")

# Sanity check bounding box
if isinstance(type(bounding_box), dict) or isinstance(type(bounding_box), tuple):

if issubclass(type(bounding_box), dict) or issubclass(type(bounding_box), tuple):
if len(bounding_box) == 4 or len(bounding_box) == 6:
self.map_data.set_bounding_box(bounding_box)
else:
Expand Down Expand Up @@ -454,7 +455,7 @@ def run_all(self, user_defined_stratigraphic_column=None, take_best=False):
self.map_data.extract_all_contacts()

# Calculate the stratigraphic column
if isinstance(type(user_defined_stratigraphic_column), list):
if issubclass(type(user_defined_stratigraphic_column), list):
self.stratigraphic_column.column = user_defined_stratigraphic_column
else:
if user_defined_stratigraphic_column is not None:
Expand Down
3 changes: 3 additions & 0 deletions map2loop/random.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import numpy as np

rng = np.random.default_rng()
3 changes: 2 additions & 1 deletion map2loop/random_colour.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import numpy as np
from .random import rng


def random_colours_hex(n):
"""
Generate n random colours in hex format.
"""
rgb = np.random.Generator.rand(n, 3)
rgb = rng.random((n, 3))
return rgb_to_hex(rgb)


Expand Down
4 changes: 2 additions & 2 deletions map2loop/sorter.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ def sort(
return stratigraphic_order_hint

geol = map_data.get_map_data(Datatype.GEOLOGY).copy()
geol = geol[geol["INTRUSIVE"] is False]
geol = geol[geol["SILL"] is False]
geol = geol[~geol["INTRUSIVE"]]
geol = geol[~geol["SILL"]]
orientations = map_data.get_map_data(Datatype.STRUCTURE).copy()

verbose = False
Expand Down
12 changes: 6 additions & 6 deletions map2loop/stratigraphic_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ def findStratigraphicUnit(self, id):
Returns:
pandas.DataFrame: The sliced data frame containing the requested unit
"""
if isinstance(type(id), int):
if issubclass(type(id), int):
return self.stratigraphicUnits[self.stratigraphicUnits["layerId"] == id]
elif isinstance(type(id), str):
elif issubclass(type(id), str):
return self.stratigraphicUnits[self.stratigraphicUnits["name"] == id]
else:
print("ERROR: Unknown identifier type used to find stratigraphic unit")
Expand All @@ -95,9 +95,9 @@ def findLithologyUnit(self, id):
Returns:
pandas.DataFrame: The sliced data frame containing the requested unit
"""
if isinstance(type(id), int):
if issubclass(type(id), int):
return self.lithologyUnits[self.lithologyUnits["layerId"] == id]
elif isinstance(type(id), str):
elif issubclass(type(id), str):
return self.lithologyUnits[self.lithologyUnits["name"] == id]
else:
print("ERROR: Unknown identifier type used to find lithology unit")
Expand All @@ -110,7 +110,7 @@ def addStratigraphicUnit(self, unit):
fault (pandas.DataFrame or dict):
The unit information to add
"""
if isinstance(type(unit), pandas.DataFrame) or isinstance(type(unit), dict):
if issubclass(type(unit), pandas.DataFrame) or issubclass(type(unit), dict):
if "name" in unit.keys():
if unit["name"] in self.stratigraphicUnits.index:
print("Replacing stratigraphic unit", unit["name"])
Expand All @@ -128,7 +128,7 @@ def addLithologyUnit(self, unit):
fault (pandas.DataFrame or dict):
The unit information to add
"""
if isinstance(type(unit), pandas.DataFrame) or isinstance(type(unit), dict):
if issubclass(type(unit), pandas.DataFrame) or issubclass(type(unit), dict):
if "name" in unit.keys():
if unit["name"] in self.lithologyUnits.index:
print("Replacing lithology unit", unit["name"])
Expand Down

0 comments on commit 0e9ecc8

Please sign in to comment.