Skip to content

Commit

Permalink
np.float -> float
Browse files Browse the repository at this point in the history
  • Loading branch information
postpop committed Jan 1, 2024
1 parent 10821fa commit 67e7ce3
Show file tree
Hide file tree
Showing 16 changed files with 577 additions and 361 deletions.
4 changes: 2 additions & 2 deletions conda/das-osxarm/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export PIP_IGNORE_INSTALLED=False

$PYTHON -m pip install keras-tuner==1.1.2 kt-legacy -vv --no-dependencies
# $PYTHON -m pip install kt-legacy -vv --no-dependencies
# $PYTHON -m pip install tensorflow-macos==2.8.0 tensorflow-metal=0.5.0
$PYTHON -m pip install tensorflow-macos==2.9 tensorflow-metal==0.5.0
# $PYTHON -m pip install tensorflow-macos==2.9 tensorflow-metal==0.5.0
$PYTHON -m pip install tensorflow-macos==2.13 tensorflow-metal==1.0.0
$PYTHON -m pip install das -vv --no-dependencies

10 changes: 5 additions & 5 deletions conda/das-osxarm/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@ requirements:
host:
- python {{ python }}
- pip
- h5py
- numpy # <1.24
# - h5py
# - numpy
run:
- python {{ python }}
- rich
- flammkuchen
- matplotlib
- pandas
- numpy # <1.24
- numpy
- peakutils
- pyyaml
- defopt=6.3
- pyside2
- scikit-learn
- scikit-image
- tqdm
- xarray-behave
- apple::tensorflow-deps=2.9
- xarray-behave>=0.34.0
- apple::tensorflow-deps=2.13
- keras
- keras-tuner

Expand Down
2 changes: 1 addition & 1 deletion src/das/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""DAS"""
__version__ = "0.31.0"
__version__ = "0.31.1"
4 changes: 2 additions & 2 deletions src/das/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def evaluate_segment_timing(segment_labels_true, segment_labels_pred, samplerate
# TODO: move to das.segment_utils
def segment_timing(labels, samplerate: float):
"""Get onset and offset time (in seconds) for each segment."""
segment_onset_times = np.where(np.diff(labels) == 1)[0].astype(np.float) / samplerate # explicit cast required?
segment_offset_times = np.where(np.diff(labels) == -1)[0].astype(np.float) / samplerate
segment_onset_times = np.where(np.diff(labels) == 1)[0].astype(float) / samplerate # explicit cast required?
segment_offset_times = np.where(np.diff(labels) == -1)[0].astype(float) / samplerate
return segment_onset_times, segment_offset_times


Expand Down
2 changes: 1 addition & 1 deletion src/das/event_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def match_events(eventindices_true, eventindices_pred, tol=100):
nearest_event = np.zeros_like(eventindices_pred)
# find nearest true event for each predicted event
_, nearest_event, nearest_dist = find_nearest(eventindices_true, eventindices_pred)
nearest_event = nearest_event.astype(np.float)
nearest_event = nearest_event.astype(float)

# flag those that have no nearby event
nearest_event = np.ma.masked_array(nearest_event, mask=nearest_dist > tol)
Expand Down
4 changes: 2 additions & 2 deletions src/das/make_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def init_store(
for target in ["train", "val", "test"]:
root.empty(name=f"{target}/x", shape=(0, nb_channels), chunks=(chunk_len, nb_channels), dtype=np.float16)
root.empty(name=f"{target}/y", shape=(0, nb_classes), chunks=(chunk_len, nb_classes), dtype=np.float16)
# root.empty(name=f'{target}/eventtimes', shape=(0, nb_classes), chunks=(1_000,), dtype=np.float)
# root.empty(name=f'{target}/eventtimes', shape=(0, nb_classes), chunks=(1_000,), dtype=float)
if make_single_class_datasets:
for class_name in class_names[1:]:
root.empty(name=f"{target}/y_{class_name}", shape=(0, 2), chunks=(chunk_len, nb_classes), dtype=np.float16)
Expand Down Expand Up @@ -209,7 +209,7 @@ def make_gaps(
gap_halfwidth = int(np.floor(gap_seconds * samplerate) / 2)

# widen gaps between adjacent syllables of different types
a = y.copy().astype(np.float)
a = y.copy().astype(float)
label_change = np.where(np.diff(a, axis=0) != 0)[0]
# remove on and offsets (0->label or label->0)
onset = a[label_change] == 0
Expand Down
27 changes: 23 additions & 4 deletions src/das/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import tensorflow.keras.layers as kl
from tensorflow.keras import regularizers
from tensorflow.keras.applications.resnet_v2 import ResNet50V2
from typing import List, Optional
from typing import List, Optional, Tuple
from . import tcn as tcn_layer
from .kapre.time_frequency import Spectrogram
from . import spec_utils
Expand Down Expand Up @@ -329,11 +329,30 @@ def tcn_stft_morph(

x = kl.Dense(nb_classes, activation="softmax")(x)

morph_kernel_duration = 16
# from .morpholayers.constraints import Disk
# import skimage.morphology as skm
# def ser(length):
# return skm.rectangle(length, 1)
# skimage.morphology.remove_small_holes
# skimage.morphology.remove_small_objects
morph_kernel_duration = 33
if morph_kernel_duration is not None:
x = x[:, :, tf.newaxis, :]
x = Closing2D(1, padding="same", kernel_size=(morph_kernel_duration, 1), kernel_regularization=l1lattice(0.002))(x)
x = Opening2D(1, padding="same", kernel_size=(morph_kernel_duration, 1), kernel_regularization=l1lattice(0.002))(x)
x = Closing2D(
num_filters=1,
padding="same",
kernel_size=(morph_kernel_duration, 1),
kernel_regularization=l1lattice(0.002),
# kernel_constraint=ser,
)(x)
x = Opening2D(
num_filters=1,
padding="same",
kernel_size=(morph_kernel_duration, 1),
kernel_regularization=l1lattice(0.002),
# kernel_constraint=ser,
)(x)

x = x[..., 0, :]

if nb_pre_conv > 0 and upsample:
Expand Down
80 changes: 50 additions & 30 deletions src/das/morpholayers/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
import skimage.morphology as skm
import scipy.ndimage.morphology as snm

"""
===============
GLOBAL VARIABLE
===============
"""
MIN_LATT = -1
MAX_LATT = 0


@tf.custom_gradient
def rounding_op1(x):
"""Round the input tensor to the nearest tenth.
Args:
x (tf.Tensor): Input tensor.
Returns:
tf.Tensor: Rounded tensor.
"""

def grad(dy):
return dy

Expand All @@ -24,6 +28,15 @@ def grad(dy):

@tf.custom_gradient
def rounding_op2(x):
"""Round the input tensor to the nearest hundredth.
Args:
x (tf.Tensor): Input tensor.
Returns:
tf.Tensor: Rounded tensor.
"""

def grad(dy):
return dy

Expand All @@ -32,6 +45,15 @@ def grad(dy):

@tf.custom_gradient
def rounding_op3(x):
"""Round the input tensor to the nearest thousandth.
Args:
x (tf.Tensor): Input tensor.
Returns:
tf.Tensor: Rounded tensor.
"""

def grad(dy):
return dy

Expand All @@ -40,14 +62,28 @@ def grad(dy):

@tf.custom_gradient
def rounding_op4(x):
"""Round the input tensor to the nearest ten-thousandth.
Args:
x (tf.Tensor): Input tensor.
Returns:
tf.Tensor: Rounded tensor.
"""

def grad(dy):
return dy

return tf.round(x * 10000) / 10000, grad


class Rounding(Constraint):
# Using Constraint to Round values
"""Constrains weights by rounding them to specified decimal places.
Args:
c (int): Number of decimal places for rounding.
"""

def __init__(self, c=4):
self.c = c

Expand All @@ -67,9 +103,7 @@ def get_config(self):


class NonPositive(Constraint):
"""
Constraint to NonPositive Values
"""
"""Constrains weights to be non-positive values."""

def __init__(self):
self.min_value = MIN_LATT
Expand All @@ -83,9 +117,7 @@ def get_config(self):


class NonPositiveExtensive(Constraint):
"""
Constraint to NonPositive and Center equal to zero
"""
"""Constrains weights to be non-positive and centers equal to zero."""

def __init__(self):
self.min_value = MIN_LATT
Expand All @@ -95,7 +127,6 @@ def __call__(self, w):
w = K.clip(w, self.min_value, 0)
data = np.ones(w.shape)
data[int(w.shape[0] / 2), int(w.shape[1] / 2), :, :] = 0
# data_tf = tf.convert_to_tensor(data, np.float32)
w = tf.multiply(w, tf.convert_to_tensor(data, np.float32))
return w

Expand All @@ -104,7 +135,8 @@ def get_config(self):


class ZeroToOne(Constraint):
# Constraint between 0 to 1 Values
"""Constrains weights to be between 0 and 1."""

def __init__(self):
self.min_value = 0.0
self.max_value = 1.0
Expand All @@ -117,9 +149,7 @@ def get_config(self):


class Lattice(Constraint):
"""
Contraint to Value Lattice Value
"""
"""Constrains weights to be within a lattice range."""

def __init__(self):
self.min_value = MIN_LATT
Expand All @@ -134,9 +164,7 @@ def get_config(self):


class SEconstraint(Constraint):
"""
Constraint any SE Shape
"""
"""Constrains weights to any structured element (SE) shape."""

def __init__(self, SE=skm.disk(1)):
self.min_value = MIN_LATT
Expand All @@ -156,17 +184,13 @@ def get_config(self):


class Disk(Constraint):
"""
Constraint to Disk Shape
Only for square filters.
"""
"""Constrains weights to a disk shape (only for square filters)."""

def __init__(self):
self.min_value = MIN_LATT
self.max_value = -MIN_LATT

def __call__(self, w):
# print('DISK CONSTRAINT',w.shape)
data = skm.disk(int(w.shape[0] / 2))
data = np.repeat(data[:, :, np.newaxis], w.shape[2], axis=2)
data = np.repeat(data[:, :, :, np.newaxis], w.shape[3], axis=3)
Expand All @@ -179,17 +203,13 @@ def get_config(self):


class Diamond(Constraint):
"""
Constraint to Diamond Shape
Only for square filters.
"""
"""Constrains weights to a diamond shape (only for square filters)."""

def __init__(self):
self.min_value = MIN_LATT
self.max_value = -MIN_LATT

def __call__(self, w):
# print('DIAMOND CONSTRAINT',w.shape)
data = skm.diamond(int(w.shape[0] / 2))
data = np.repeat(data[:, :, np.newaxis], w.shape[2], axis=2)
data = np.repeat(data[:, :, :, np.newaxis], w.shape[3], axis=3)
Expand Down
26 changes: 0 additions & 26 deletions src/das/morpholayers/examples/LinkstoTutorial.md

This file was deleted.

Loading

0 comments on commit 67e7ce3

Please sign in to comment.