Commit 545d089a authored by lucas_miranda's avatar lucas_miranda
Browse files

Changed all type() checks for isinstance() to take inheritance into account

parent 48ef8ea8
......@@ -40,8 +40,8 @@ import warnings
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
# DEFINE CUSTOM ANNOTATED TYPES #
Coordinates = deepof.utils.NewType("Coordinates", deepof.utils.Any)
Table_dict = deepof.utils.NewType("Table_dict", deepof.utils.Any)
Coordinates = deepof.utils.Newisinstance("Coordinates", deepof.utils.Any)
Table_dict = deepof.utils.Newisinstance("Table_dict", deepof.utils.Any)
# CLASSES FOR PREPROCESSING AND DATA WRANGLING
......@@ -549,7 +549,7 @@ class coordinates:
- self._scales[i][1] / 2
)
elif type(center) == str and center != "arena":
elif isinstance(center, str) and center != "arena":
for i, (key, value) in enumerate(tabs.items()):
......@@ -583,7 +583,7 @@ class coordinates:
for key, tab in tabs.items():
tabs[key].index = pd.timedelta_range(
"00:00:00", length, periods=tab.shape[0] + 1, closed="left"
).astype("timedelta64[s]")
).asisinstance("timedelta64[s]")
if align:
assert (
......@@ -667,7 +667,7 @@ class coordinates:
for key, tab in tabs.items():
tabs[key].index = pd.timedelta_range(
"00:00:00", length, periods=tab.shape[0] + 1, closed="left"
).astype("timedelta64[s]")
).asisinstance("timedelta64[s]")
if propagate_labels:
for key, tab in tabs.items():
......@@ -732,7 +732,7 @@ class coordinates:
for key, tab in tabs.items():
tabs[key].index = pd.timedelta_range(
"00:00:00", length, periods=tab.shape[0] + 1, closed="left"
).astype("timedelta64[s]")
).asisinstance("timedelta64[s]")
if propagate_labels:
for key, tab in tabs.items():
......@@ -833,7 +833,7 @@ class coordinates:
)
pbar.update(1)
if type(video_output) == list:
if isinstance(video_output, list):
vid_idxs = video_output
elif video_output == "all":
vid_idxs = list(self._tables.keys())
......
......@@ -25,7 +25,7 @@ import warnings
warnings.filterwarnings("ignore", message="All-NaN slice encountered")
# Create custom string type
Coordinates = NewType("Coordinates", Any)
Coordinates = Newisinstance("Coordinates", Any)
def close_single_contact(
......@@ -53,12 +53,12 @@ def close_single_contact(
close_contact = None
if type(right) == str:
if isinstance(right, str):
close_contact = (
np.linalg.norm(pos_dframe[left] - pos_dframe[right], axis=1) * arena_abs
) / arena_rel < tol
elif type(right) == list:
elif isinstance(right, list):
close_contact = np.any(
[
(np.linalg.norm(pos_dframe[left] - pos_dframe[r], axis=1) * arena_abs)
......@@ -528,7 +528,7 @@ def max_behaviour(
speeds = [col for col in behaviour_dframe.columns if "speed" in col.lower()]
behaviour_dframe = behaviour_dframe.drop(speeds, axis=1).astype("float")
behaviour_dframe = behaviour_dframe.drop(speeds, axis=1).asisinstance("float")
win_array = behaviour_dframe.rolling(window_size, center=True).sum()
if stepped:
win_array = win_array[::window_size]
......@@ -678,8 +678,8 @@ def rule_based_tagging(
return deepof.utils.smooth_boolean_array(
close_single_contact(
coords,
(left if type(left) != list else right),
(right if type(left) != list else left),
(left if not isinstance(left, list) else right),
(right if not isinstance(left, list) else left),
params["close_contact_tol"],
arena_abs,
arena[1][1],
......
......@@ -26,7 +26,7 @@ from typing import Tuple, Any, List, Union, NewType
# DEFINE CUSTOM ANNOTATED TYPES #
Coordinates = NewType("Coordinates", Any)
Coordinates = Newisinstance("Coordinates", Any)
# CONNECTIVITY FOR DLC MODELS
......@@ -750,7 +750,7 @@ def cluster_transition_matrix(
# Stores all possible transitions between clusters
clusters = [str(i) for i in range(nclusts)]
cluster_sequence = cluster_sequence.astype(str)
cluster_sequence = cluster_sequence.asisinstance(str)
trans = {t: 0 for t in product(clusters, clusters)}
k = len(clusters)
......
......@@ -488,12 +488,12 @@
# draw skeleton
def draw_line(start, end, df, col):
for bpart in end:
cv2.line(
image,
tuple(-df[start].loc[frame, :].astype(int) + w // 2),
tuple(-df[bpart].loc[frame, :].astype(int) + h // 2),
tuple(-df[start].loc[frame, :].asisinstance(int) + w // 2),
tuple(-df[bpart].loc[frame, :].asisinstance(int) + h // 2),
col,
1,
)
col = (0,0,255)
......@@ -553,11 +553,11 @@
(30002,)
%% Cell type:code id: tags:
``` python
moving = pd.Series(concat_tags).rolling(window=24, ).apply(lambda x: np.any(x > 2))[::24][1:].astype(bool)
moving = pd.Series(concat_tags).rolling(window=24, ).apply(lambda x: np.any(x > 2))[::24][1:].asisinstance(bool)
```
%% Cell type:code id: tags:
``` python
......@@ -685,12 +685,12 @@
# # draw skeleton
# def draw_line(start, end, df, col):
# for bpart in end:
# cv2.line(
# image,
# tuple(-df[start].loc[frame, :].astype(int) + w // 2),
# tuple(-df[bpart].loc[frame, :].astype(int) + h // 2),
# tuple(-df[start].loc[frame, :].asisinstance(int) + w // 2),
# tuple(-df[bpart].loc[frame, :].asisinstance(int) + h // 2),
# col,
# 1,
# )
# for df, col in zip([scaled_video_input, scaled_video_pred], [(0,0,255),(0,255,0)]):
......@@ -765,11 +765,11 @@
reducer = umap.UMAP(n_components=2)
encodings = reducer.fit_transform(encodings)
sns.scatterplot(encodings[:,0], encodings[:,1],
hue=np.squeeze(pheno_pred),#np.argmax(clusters, axis=1).astype(int).astype(str),
hue=np.squeeze(pheno_pred),#np.argmax(clusters, axis=1).asisinstance(int).asisinstance(str),
#palette=("jet" if k>1 else None), legend="none")
)
plt.title("GMVAE Latent space representation: L={}; k={}".format(l,k))
plt.xlabel("UMAP 1")
plt.ylabel("UMAP 2")
......@@ -1188,12 +1188,12 @@
%% Cell type:code id: tags:
``` python
random_ari = []
for i in tqdm(range(6)):
random_ari.append(adjusted_rand_score(np.random.uniform(0,6,50).astype(int),
np.random.uniform(0,6,50).astype(int)))
random_ari.append(adjusted_rand_score(np.random.uniform(0,6,50).asisinstance(int),
np.random.uniform(0,6,50).asisinstance(int)))
```
%% Cell type:code id: tags:
``` python
......@@ -1479,11 +1479,11 @@
%% Cell type:code id: tags:
``` python
reconst_df = pd.concat([pd.DataFrame(np.concatenate([np.repeat(k, len(v)).reshape(len(v),1), v.reshape(len(v),1)],axis=1)) for k,v in reconst_error.items()])
reconst_df = reconst_df.astype({0:str,1:float})
reconst_df = reconst_df.asisinstance({0:str,1:float})
```
%% Cell type:code id: tags:
``` python
......
......@@ -156,12 +156,12 @@
ax1.set_xticklabels([])
ax1.set_yticklabels([])
ax1.set_aspect("equal")
cv2.ellipse(
temp_image,
tuple(temp_ellipse[:2].astype(int)),
tuple(temp_ellipse[2:4].astype(int)),
tuple(temp_ellipse[:2].asisinstance(int)),
tuple(temp_ellipse[2:4].asisinstance(int)),
temp_ellipse[4],
0,
360,
(0, 255, 0),
3,
......@@ -241,12 +241,12 @@
ax1.set_xticklabels([])
ax1.set_yticklabels([])
ax1.set_aspect("equal")
cv2.ellipse(
temp_image,
tuple(temp_ellipse[:2].astype(int)),
tuple(temp_ellipse[2:4].astype(int)),
tuple(temp_ellipse[:2].asisinstance(int)),
tuple(temp_ellipse[2:4].asisinstance(int)),
temp_ellipse[4],
0,
360,
(0, 1, 0),
3,
......@@ -417,12 +417,12 @@
ax1.set_xticklabels([])
ax1.set_yticklabels([])
ax1.set_aspect("equal")
cv2.ellipse(
temp_image,
tuple(temp_ellipse[:2].astype(int)),
tuple(temp_ellipse[2:4].astype(int)),
tuple(temp_ellipse[:2].asisinstance(int)),
tuple(temp_ellipse[2:4].asisinstance(int)),
temp_ellipse[4],
0,
360,
(0, 1, 0),
3,
......
......@@ -50,9 +50,9 @@ def test_project_init(table_type, arena_type):
if table_type != ".foo" and arena_type != "foo":
assert type(prun) == deepof.data.project
assert type(prun.load_tables(verbose=True)) == tuple
assert type(prun.get_scale) == np.ndarray
assert isinstance(prun, deepof.data.project)
assert isinstance(prun.load_tables(verbose=True), tuple)
assert isinstance(prun.get_scale, np.ndarray)
def test_project_properties():
......@@ -103,7 +103,7 @@ def test_get_distances(nodes, ego):
prun.ego = ego
prun = prun.get_distances(prun.load_tables()[0], verbose=True)
assert type(prun) == dict
assert isinstance(prun, dict)
@settings(deadline=None)
......@@ -128,7 +128,7 @@ def test_get_angles(nodes, ego):
prun.ego = ego
prun = prun.get_angles(prun.load_tables()[0], verbose=True)
assert type(prun) == dict
assert isinstance(prun, dict)
@settings(deadline=None)
......@@ -153,7 +153,7 @@ def test_run(nodes, ego):
prun.ego = ego
prun = prun.run(verbose=True)
assert type(prun) == deepof.data.coordinates
assert isinstance(prun, deepof.data.coordinates)
def test_get_rule_based_annotation():
......@@ -168,7 +168,7 @@ def test_get_rule_based_annotation():
prun = prun.rule_based_annotation()
assert type(prun) == deepof.data.table_dict
assert isinstance(prun, deepof.data.table_dict)
assert prun._type == "rule-based"
......@@ -233,14 +233,14 @@ def test_get_table_dicts(nodes, ego, exclude, sampler):
# deepof.coordinates testing
assert type(coords) == deepof.data.table_dict
assert type(speeds) == deepof.data.table_dict
assert type(distances) == deepof.data.table_dict
assert type(angles) == deepof.data.table_dict
assert type(prun.get_videos()) == list
assert isinstance(coords, deepof.data.table_dict)
assert isinstance(speeds, deepof.data.table_dict)
assert isinstance(distances, deepof.data.table_dict)
assert isinstance(angles, deepof.data.table_dict)
assert isinstance(prun.get_videos(), list)
assert prun.get_exp_conditions is not None
assert type(prun.get_quality()) == defaultdict
assert type(prun.get_arenas) == tuple
assert isinstance(prun.get_quality(), defaultdict)
assert isinstance(prun.get_arenas, tuple)
# deepof.table_dict testing
......@@ -256,12 +256,12 @@ def test_get_table_dicts(nodes, ego, exclude, sampler):
test_videos=sampler.draw(st.integers(min_value=0, max_value=len(table) - 1))
)
assert len(tset) == 4
assert type(tset[0]) == np.ndarray
assert isinstance(tset[0], np.ndarray)
if table._type == "coords" and algn == "Nose" and polar is False and speed == 0:
assert (
type(table.plot_heatmaps(bodyparts=["Spine_1"])) == matplotlib.figure.Figure
isinstance(table.plot_heatmaps(bodyparts=["Spine_1"]), matplotlib.figure.Figure)
)
align = sampler.draw(
......@@ -284,10 +284,10 @@ def test_get_table_dicts(nodes, ego, exclude, sampler):
align=align,
)
assert type(prep[0]) == np.ndarray
assert isinstance(prep[0], np.ndarray)
# deepof dimensionality reduction testing
assert type(table.random_projection(n_components=2, sample=50)) == tuple
assert type(table.pca(n_components=2, sample=50)) == tuple
assert type(table.tsne(n_components=2, sample=50)) == tuple
assert isinstance(table.random_projection(n_components=2, sample=50), tuple)
assert isinstance(table.pca(n_components=2, sample=50), tuple)
assert isinstance(table.tsne(n_components=2, sample=50), tuple)
......@@ -44,7 +44,7 @@ def test_compute_mmd(tensor):
mmd_kernel = deepof.model_utils.compute_mmd(tuple([tensor1, tensor2]))
null_kernel = deepof.model_utils.compute_mmd(tuple([tensor1, tensor1]))
assert type(mmd_kernel) == EagerTensor
assert isinstance(mmd_kernel, EagerTensor)
assert null_kernel == 0
......@@ -53,7 +53,7 @@ def test_one_cycle_scheduler():
cycle1 = deepof.model_utils.one_cycle_scheduler(
iterations=5, max_rate=1.0, start_rate=0.1, last_iterations=2, last_rate=0.3
)
assert type(cycle1._interpolate(1, 2, 0.2, 0.5)) == float
assert isinstance(cycle1._interpolate(1, 2, 0.2, 0.5), float)
X = np.random.uniform(0, 10, [1500, 5])
y = np.random.randint(0, 2, [1500, 1])
......@@ -72,7 +72,7 @@ def test_one_cycle_scheduler():
)
fit = test_model.fit(X, y, callbacks=[onecycle], epochs=10, batch_size=100)
assert type(fit) == tf.keras.callbacks.History
assert isinstance(fit, tf.keras.callbacks.History)
assert onecycle.history["lr"][4] > onecycle.history["lr"][1]
assert onecycle.history["lr"][4] > onecycle.history["lr"][-1]
......@@ -102,7 +102,7 @@ def test_uncorrelated_features_constraint():
)
fit = test_model.fit(X, y, epochs=25, batch_size=100)
assert type(fit) == tf.keras.callbacks.History
assert isinstance(fit, tf.keras.callbacks.History)
correlations.append(np.mean(np.corrcoef(test_model.get_weights()[0])))
......@@ -124,7 +124,7 @@ def test_MCDropout():
)
fit = test_model.fit(X, y, epochs=10, batch_size=100)
assert type(fit) == tf.keras.callbacks.History
assert isinstance(fit, tf.keras.callbacks.History)
# noinspection PyUnresolvedReferences
......@@ -145,7 +145,7 @@ def test_dense_transpose():
)
fit = test_model.fit(X, y, epochs=10, batch_size=100)
assert type(fit) == tf.keras.callbacks.History
assert isinstance(fit, tf.keras.callbacks.History)
# noinspection PyCallingNonCallable,PyUnresolvedReferences
......@@ -189,7 +189,7 @@ def test_KLDivergenceLayer():
)
fit = test_model.fit(X, [y, y], epochs=1, batch_size=100)
assert tf.keras.callbacks.History == type(fit)
assert isinstance(fit, tf.keras.callbacks.History)
assert test_model.losses[0] == test_model.losses[1]
......@@ -234,7 +234,7 @@ def test_MMDiscrepancyLayer():
)
fit = test_model.fit(X, y, epochs=10, batch_size=100)
assert tf.keras.callbacks.History == type(fit)
assert isinstance(fit, tf.keras.callbacks.History)
# noinspection PyUnresolvedReferences
......@@ -252,7 +252,7 @@ def test_dead_neuron_control():
)
fit = test_model.fit(X, y, epochs=10, batch_size=100)
assert tf.keras.callbacks.History == type(fit)
assert isinstance(fit, tf.keras.callbacks.History)
def test_find_learning_rate():
......
......@@ -291,11 +291,11 @@ def test_single_behaviour_analysis(sampler):
)
assert len(out) == 1 if (stat_tests == 0 and plot == 0) else len(out) >= 2
assert type(out[0]) == dict
assert isinstance(out[0], dict)
if plot:
assert np.any(np.array([type(i) for i in out]) == matplotlib.figure.Figure)
assert np.any(np.array([isinstance(i, matplotlib.figure.Figure) for i in out]))
if stat_tests:
assert type(out[0]) == dict
assert isinstance(out[0], dict)
@settings(
......@@ -321,18 +321,18 @@ def test_max_behaviour(behaviour_dframe, window_size, stepped):
maxbe1 = max_behaviour(behaviour_dframe, wsize1, stepped)
maxbe2 = max_behaviour(behaviour_dframe, wsize2, stepped)
assert type(maxbe1) == np.ndarray
assert type(maxbe2) == np.ndarray
assert isinstance(maxbe1, np.ndarray)
assert isinstance(maxbe2, np.ndarray)
if not stepped:
assert type(maxbe1[wsize1 // 2 + 1]) == str
assert type(maxbe1[wsize2 // 2 + 1]) == str
assert isinstance(maxbe1[wsize1 // 2 + 1], str)
assert isinstance(maxbe1[wsize2 // 2 + 1], str)
assert maxbe1[wsize1 // 2 + 1] in behaviour_dframe.columns
assert maxbe2[wsize2 // 2 + 1] in behaviour_dframe.columns
assert len(maxbe1) >= len(maxbe2)
def test_get_hparameters():
assert type(get_hparameters()) == dict
assert isinstance(get_hparameters(), dict)
assert get_hparameters({"speed_pause": 20})["speed_pause"] == 20
......@@ -376,5 +376,5 @@ def test_rule_based_tagging(multi_animal, video_output):
video_output=video_output, frame_limit=50
)
assert type(hardcoded_tags) == deepof.data.table_dict
assert isinstance(hardcoded_tags, deepof.data.table_dict)
assert list(hardcoded_tags.values())[0].shape[1] == (21 if multi_animal else 6)
......@@ -24,13 +24,11 @@ import tensorflow as tf
def test_load_treatments():
assert deepof.train_utils.load_treatments(".") is None
assert (
type(
isinstance(
deepof.train_utils.load_treatments(
os.path.join("tests", "test_examples", "test_single_topview", "Others")
)
)
== dict
)
), dict
))
@given(
......@@ -69,11 +67,11 @@ def test_get_callbacks(
reg_cluster_variance=False,
logparam={"encoding": 2, "k": 15},
)
assert type(runID) == str
assert type(tbc) == tf.keras.callbacks.TensorBoard
assert type(cpc) == tf.keras.callbacks.ModelCheckpoint
assert type(entropy) == deepof.model_utils.neighbor_latent_entropy
assert type(cycle1c) == deepof.model_utils.one_cycle_scheduler
assert isinstance(runID, str)
assert isinstance(tbc, tf.keras.callbacks.TensorBoard)
assert isinstance(cpc, tf.keras.callbacks.ModelCheckpoint)
assert isinstance(entropy, deepof.model_utils.neighbor_latent_entropy)
assert isinstance(cycle1c, deepof.model_utils.one_cycle_scheduler)
@settings(max_examples=10, deadline=None, suppress_health_check=[HealthCheck.too_slow])
......
......@@ -33,17 +33,15 @@ def test_plot_heatmap(bparts):
)
assert (
type(
isinstance(
deepof.visuals.plot_heatmap(
prun["test"],
bparts,
tuple([-100, 100]),
tuple([-100, 100]),
dpi=200,
)
)
== matplotlib.figure.Figure
)
), matplotlib.figure.Figure
))
def test_model_comparison_plot():
......@@ -64,10 +62,9 @@ def test_model_comparison_plot():
)
assert (
type(
isinstance(
deepof.visuals.model_comparison_plot(
gmm_run[0], gmm_run[1], range(1, 3), cov_plot="full"
)
)
== matplotlib.figure.Figure
)
),
matplotlib.figure.Figure
))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment