Skip to content

Commit

Permalink
Merge pull request #128 from NickleDave/rename-hz-ind
Browse files Browse the repository at this point in the history
CLN: rename `on/offsets_Hz` -> `on/offset_inds`, fix #87
  • Loading branch information
NickleDave authored Jan 2, 2022
2 parents c44f9a8 + de67b78 commit da1f84f
Show file tree
Hide file tree
Showing 30 changed files with 264 additions and 264 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ work with any format: namely, `Sequence`s made up of `Segment`s.
>>> from crowsetta import Segment, Sequence
>>> a_segment = Segment.from_keyword(
... label='a',
... onset_Hz=16000,
... offset_Hz=32000,
... onset_ind=16000,
... offset_ind=32000,
... file='bird21.wav'
... )
>>> list_of_segments = [a_segment] * 3
>>> seq = Sequence(segments=list_of_segments)
>>> print(seq)
Sequence(segments=[Segment(label='a', onset_s=None, offset_s=None, onset_Hz=16000,
offset_Hz=32000, file='bird21.wav'), Segment(label='a', onset_s=None, offset_s=None,
onset_Hz=16000, offset_Hz=32000, file='bird21.wav'), Segment(label='a', onset_s=None,
offset_s=None, onset_Hz=16000, offset_Hz=32000, file='bird21.wav')])
Sequence(segments=[Segment(label='a', onset_s=None, offset_s=None, onset_ind=16000,
offset_ind=32000, file='bird21.wav'), Segment(label='a', onset_s=None, offset_s=None,
onset_ind=16000, offset_ind=32000, file='bird21.wav'), Segment(label='a', onset_s=None,
offset_s=None, onset_ind=16000, offset_ind=32000, file='bird21.wav')])
```

You can load annotation from your format of choice into `Sequence`s of `Segment`s
Expand Down
4 changes: 2 additions & 2 deletions doc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## 0.2.0a5
### added
- Sequence instances have attributes: labels, onsets_s, offsets_s, onsets_Hz,
offsets_Hz, and file.
- Sequence instances have attributes: labels, onsets_s, offsets_s, onset_inds,
offset_inds, and file.
- Explanation of default `to_csv` function for user formats in `howto-user-config`.

### changed
Expand Down
12 changes: 6 additions & 6 deletions doc/howto-user-format.rst
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ us back an instance of a ``Sequence``. One such factory function is
a_sequence = Sequence.from_keyword(labels=annot_dict['seg_types'],
onsets_s=annot_dict['seg_start_times'],
offsets_s=annot_dict['seg_end_times'],
onsets_Hz=annot_dict['seg_start_times_Hz'],
offsets_Hz=annot_dict['seg_end_times_Hz'],
onset_inds=annot_dict['seg_start_times_Hz'],
offset_inds=annot_dict['seg_end_times_Hz'],
file=annot_dict['audio_file'])
print("a_sequence:\n", a_sequence)
Expand Down Expand Up @@ -268,8 +268,8 @@ Then at the end of your main loop, instead of making your
labels=seg_types,
onsets_s=seg_start_times,
offsets_s=seg_end_times,
onsets_Hz=seg_start_times_Hz,
offsets_Hz=seg_end_times_Hz)
onset_inds=seg_start_times_Hz,
offset_inds=seg_end_times_Hz)
seq_list.append(seq)
return seq_list
Expand Down Expand Up @@ -391,7 +391,7 @@ of ``Sequence``\ s from your format:
First item in seq_list: <Sequence with 15 segments>
First segment in first sequence:
Segment(label='1', file='lbr3009_0005_2017_04_27_06_14_46.wav', onset_s=0.0029761904761904934, offset_s=0.14150432900432905, onset_Hz=143, offset_Hz=6792)
Segment(label='1', file='lbr3009_0005_2017_04_27_06_14_46.wav', onset_s=0.0029761904761904934, offset_s=0.14150432900432905, onset_ind=143, offset_ind=6792)
Notice that we also get a ``to_csv`` function for free:
Expand All @@ -411,7 +411,7 @@ Notice that we also get a ``to_csv`` function for free:
.. parsed-literal::
['label', 'onset_s', 'offset_s', 'onset_Hz', 'offset_Hz', 'file']
['label', 'onset_s', 'offset_s', 'onset_ind', 'offset_ind', 'file']
['1', '0.0029761904761904934', '0.14150432900432905', '143', '6792', 'lbr3009_0005_2017_04_27_06_14_46.wav']
['1', '0.279125', '0.504625', '13398', '24222', 'lbr3009_0005_2017_04_27_06_14_46.wav']
['5', '0.5556472915365209', '0.5962916666666667', '26671', '28622', 'lbr3009_0005_2017_04_27_06_14_46.wav']
Expand Down
14 changes: 7 additions & 7 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,26 @@ The code block below shows some of the features of these data types.
>>> from crowsetta import Segment, Sequence
>>> a_segment = Segment.from_keyword(
... label='a',
... onset_Hz=16000,
... offset_Hz=32000,
... onset_ind=16000,
... offset_ind=32000,
... file='bird21.wav'
... )
>>> another_segment = Segment.from_keyword(
... label='b',
... onset_Hz=36000,
... offset_Hz=48000,
... onset_ind=36000,
... offset_ind=48000,
... file='bird21.wav'
... )
>>> list_of_segments = [a_segment, another_segment]
>>> seq = Sequence.from_segments(segments=list_of_segments)
>>> print(seq)
<Sequence with 2 segments>
>>> for segment in seq.segments: print(segment)
Segment(label='a', file='bird21.wav', onset_s=None, offset_s=None, onset_Hz=16000, offset_Hz=32000)
Segment(label='b', file='bird21.wav', onset_s=None, offset_s=None, onset_Hz=36000, offset_Hz=48000)
Segment(label='a', file='bird21.wav', onset_s=None, offset_s=None, onset_ind=16000, offset_ind=32000)
Segment(label='b', file='bird21.wav', onset_s=None, offset_s=None, onset_ind=36000, offset_ind=48000)
>>> seq.file
bird21.wav
>>> seq.onsets_Hz
>>> seq.onset_inds
array([16000, 36000])
You load annotation from your format of choice into ``Sequence``\ s of ``Segment``\ s
Expand Down
4 changes: 2 additions & 2 deletions doc/notebooks/batlab2seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def batlab2seq(mat_file):
labels=seg_types,
onsets_s=seg_start_times,
offsets_s=seg_end_times,
onsets_Hz=seg_start_times_Hz,
offsets_Hz=seg_end_times_Hz)
onset_inds=seg_start_times_Hz,
offset_inds=seg_end_times_Hz)
seq_list.append(seq)
return seq_list
12 changes: 6 additions & 6 deletions doc/notebooks/howto-user-format.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@
"a_sequence = Sequence.from_keyword(labels=annot_dict['seg_types'],\n",
" onsets_s=annot_dict['seg_start_times'],\n",
" offsets_s=annot_dict['seg_end_times'],\n",
" onsets_Hz=annot_dict['seg_start_times_Hz'],\n",
" offsets_Hz=annot_dict['seg_end_times_Hz'],\n",
" onset_inds=annot_dict['seg_start_times_Hz'],\n",
" offset_inds=annot_dict['seg_end_times_Hz'],\n",
" file=annot_dict['audio_file'])\n",
"print(\"a_sequence:\\n\", a_sequence)"
]
Expand Down Expand Up @@ -261,8 +261,8 @@
" labels=seg_types,\n",
" onsets_s=seg_start_times,\n",
" offsets_s=seg_end_times,\n",
" onsets_Hz=seg_start_times_Hz,\n",
" offsets_Hz=seg_end_times_Hz)\n",
" onset_inds=seg_start_times_Hz,\n",
" offset_inds=seg_end_times_Hz)\n",
"seq_list.append(seq)\n",
" return seq_list"
]
Expand Down Expand Up @@ -370,7 +370,7 @@
"text": [
"First item in seq_list: <Sequence with 15 segments>\n",
"First segment in first sequence:\n",
"Segment(label='1', file='lbr3009_0005_2017_04_27_06_14_46.wav', onset_s=0.0029761904761904934, offset_s=0.14150432900432905, onset_Hz=143, offset_Hz=6792)\n"
"Segment(label='1', file='lbr3009_0005_2017_04_27_06_14_46.wav', onset_s=0.0029761904761904934, offset_s=0.14150432900432905, onset_ind=143, offset_ind=6792)\n"
]
}
],
Expand All @@ -395,7 +395,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"['label', 'onset_s', 'offset_s', 'onset_Hz', 'offset_Hz', 'file']\n",
"['label', 'onset_s', 'offset_s', 'onset_ind', 'offset_ind', 'file']\n",
"['1', '0.0029761904761904934', '0.14150432900432905', '143', '6792', 'lbr3009_0005_2017_04_27_06_14_46.wav']\n",
"['1', '0.279125', '0.504625', '13398', '24222', 'lbr3009_0005_2017_04_27_06_14_46.wav']\n",
"['5', '0.5556472915365209', '0.5962916666666667', '26671', '28622', 'lbr3009_0005_2017_04_27_06_14_46.wav']\n"
Expand Down
6 changes: 3 additions & 3 deletions doc/notebooks/tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@
"first element of seq: <Sequence with 54 segments>\n",
"\n",
"First two Segments of first Sequence:\n",
"Segment(label='i', file='./data/cbin-notmat/032312/gy6or6_baseline_230312_0819.190.cbin', onset_s=0.435, offset_s=0.511, onset_Hz=13924, offset_Hz=16350)\n",
"Segment(label='i', file='./data/cbin-notmat/032312/gy6or6_baseline_230312_0819.190.cbin', onset_s=0.583, offset_s=0.662, onset_Hz=18670, offset_Hz=21184)\n"
"Segment(label='i', file='./data/cbin-notmat/032312/gy6or6_baseline_230312_0819.190.cbin', onset_s=0.435, offset_s=0.511, onset_ind=13924, offset_ind=16350)\n",
"Segment(label='i', file='./data/cbin-notmat/032312/gy6or6_baseline_230312_0819.190.cbin', onset_s=0.583, offset_s=0.662, onset_ind=18670, offset_ind=21184)\n"
]
}
],
Expand Down Expand Up @@ -417,7 +417,7 @@
" smoothed = evfuncs.smooth_data(raw_audio, samp_freq,\n",
" freq_cutoffs=(500, 10000))\n",
" for segment in sequence.segments:\n",
" smoothed_seg = smoothed[segment.onset_Hz:segment.offset_Hz]\n",
" smoothed_seg = smoothed[segment.onset_ind:segment.offset_ind]\n",
" mean_seg_amp = np.mean(smoothed_seg)\n",
" syl_amp_dict[segment.label].append(mean_seg_amp)\n",
"\n",
Expand Down
6 changes: 3 additions & 3 deletions doc/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ part of the sequence defined by an ``onset`` and ``offset`` that has a
first element of seq: <Sequence with 54 segments>
First two Segments of first Sequence:
Segment(label='i', file='./data/cbin-notmat/032312/gy6or6_baseline_230312_0819.190.cbin', onset_s=0.435, offset_s=0.511, onset_Hz=13924, offset_Hz=16350)
Segment(label='i', file='./data/cbin-notmat/032312/gy6or6_baseline_230312_0819.190.cbin', onset_s=0.583, offset_s=0.662, onset_Hz=18670, offset_Hz=21184)
Segment(label='i', file='./data/cbin-notmat/032312/gy6or6_baseline_230312_0819.190.cbin', onset_s=0.435, offset_s=0.511, onset_ind=13924, offset_ind=16350)
Segment(label='i', file='./data/cbin-notmat/032312/gy6or6_baseline_230312_0819.190.cbin', onset_s=0.583, offset_s=0.662, onset_ind=18670, offset_ind=21184)
**Using** ``crowsetta`` **data types to write clean code**
Expand Down Expand Up @@ -329,7 +329,7 @@ the signal, and then smooths it with a sliding window.
smoothed = evfuncs.smooth_data(raw_audio, samp_freq,
freq_cutoffs=(500, 10000))
for segment in sequence.segments:
smoothed_seg = smoothed[segment.onset_Hz:segment.offset_Hz]
smoothed_seg = smoothed[segment.onset_ind:segment.offset_ind]
mean_seg_amp = np.mean(smoothed_seg)
syl_amp_dict[segment.label].append(mean_seg_amp)
Expand Down
4 changes: 2 additions & 2 deletions notebooks/batlab2seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def batlab2seq(mat_file):
labels=seg_types,
onsets_s=seg_start_times,
offsets_s=seg_end_times,
onsets_Hz=seg_start_times_Hz,
offsets_Hz=seg_end_times_Hz)
onset_inds=seg_start_times_Hz,
offset_inds=seg_end_times_Hz)
seq_list.append(seq)
return seq_list
12 changes: 6 additions & 6 deletions notebooks/howto-user-format.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@
"a_sequence = Sequence.from_keyword(labels=annot_dict['seg_types'],\n",
" onsets_s=annot_dict['seg_start_times'],\n",
" offsets_s=annot_dict['seg_end_times'],\n",
" onsets_Hz=annot_dict['seg_start_times_Hz'],\n",
" offsets_Hz=annot_dict['seg_end_times_Hz'],\n",
" onset_inds=annot_dict['seg_start_times_Hz'],\n",
" offset_inds=annot_dict['seg_end_times_Hz'],\n",
" file=annot_dict['audio_file'])\n",
"print(\"a_sequence:\\n\", a_sequence)"
]
Expand Down Expand Up @@ -261,8 +261,8 @@
" labels=seg_types,\n",
" onsets_s=seg_start_times,\n",
" offsets_s=seg_end_times,\n",
" onsets_Hz=seg_start_times_Hz,\n",
" offsets_Hz=seg_end_times_Hz)\n",
" onset_inds=seg_start_times_Hz,\n",
" offset_inds=seg_end_times_Hz)\n",
"seq_list.append(seq)\n",
" return seq_list"
]
Expand Down Expand Up @@ -370,7 +370,7 @@
"text": [
"First item in seq_list: <Sequence with 15 segments>\n",
"First segment in first sequence:\n",
"Segment(label='1', file='lbr3009_0005_2017_04_27_06_14_46.wav', onset_s=0.0029761904761904934, offset_s=0.14150432900432905, onset_Hz=143, offset_Hz=6792)\n"
"Segment(label='1', file='lbr3009_0005_2017_04_27_06_14_46.wav', onset_s=0.0029761904761904934, offset_s=0.14150432900432905, onset_ind=143, offset_ind=6792)\n"
]
}
],
Expand All @@ -395,7 +395,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"['label', 'onset_s', 'offset_s', 'onset_Hz', 'offset_Hz', 'file']\n",
"['label', 'onset_s', 'offset_s', 'onset_ind', 'offset_ind', 'file']\n",
"['1', '0.0029761904761904934', '0.14150432900432905', '143', '6792', 'lbr3009_0005_2017_04_27_06_14_46.wav']\n",
"['1', '0.279125', '0.504625', '13398', '24222', 'lbr3009_0005_2017_04_27_06_14_46.wav']\n",
"['5', '0.5556472915365209', '0.5962916666666667', '26671', '28622', 'lbr3009_0005_2017_04_27_06_14_46.wav']\n"
Expand Down
6 changes: 3 additions & 3 deletions notebooks/tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@
"first element of seq: <Sequence with 54 segments>\n",
"\n",
"First two Segments of first Sequence:\n",
"Segment(label='i', file='./data/cbin-notmat/032312/gy6or6_baseline_230312_0819.190.cbin', onset_s=0.435, offset_s=0.511, onset_Hz=13924, offset_Hz=16350)\n",
"Segment(label='i', file='./data/cbin-notmat/032312/gy6or6_baseline_230312_0819.190.cbin', onset_s=0.583, offset_s=0.662, onset_Hz=18670, offset_Hz=21184)\n"
"Segment(label='i', file='./data/cbin-notmat/032312/gy6or6_baseline_230312_0819.190.cbin', onset_s=0.435, offset_s=0.511, onset_ind=13924, offset_ind=16350)\n",
"Segment(label='i', file='./data/cbin-notmat/032312/gy6or6_baseline_230312_0819.190.cbin', onset_s=0.583, offset_s=0.662, onset_ind=18670, offset_ind=21184)\n"
]
}
],
Expand Down Expand Up @@ -417,7 +417,7 @@
" smoothed = evfuncs.smooth_data(raw_audio, samp_freq,\n",
" freq_cutoffs=(500, 10000))\n",
" for segment in sequence.segments:\n",
" smoothed_seg = smoothed[segment.onset_Hz:segment.offset_Hz]\n",
" smoothed_seg = smoothed[segment.onset_ind:segment.offset_ind]\n",
" mean_seg_amp = np.mean(smoothed_seg)\n",
" syl_amp_dict[segment.label].append(mean_seg_amp)\n",
"\n",
Expand Down
12 changes: 6 additions & 6 deletions src/crowsetta/birdsongrec.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ def birdsongrec2annot(annot_path='Annotation.xml', concat_seqs_into_songs=True,

annot_list = []
for seq_xml in seq_list_xml:
onsets_Hz = np.asarray([syl.position for syl in seq_xml.syls])
offsets_Hz = np.asarray([syl.position + syl.length for syl in seq_xml.syls])
onset_inds = np.asarray([syl.position for syl in seq_xml.syls])
offset_inds = np.asarray([syl.position + syl.length for syl in seq_xml.syls])
labels = [syl.label for syl in seq_xml.syls]

wav_filename = os.path.join(wavpath, seq_xml.wav_file)
Expand All @@ -94,11 +94,11 @@ def birdsongrec2annot(annot_path='Annotation.xml', concat_seqs_into_songs=True,
f'annotation file {annot_path} is not found'
)
samp_freq = soundfile.info(wav_filename).samplerate
onsets_s = np.round(onsets_Hz / samp_freq, decimals=3)
offsets_s = np.round(offsets_Hz / samp_freq, decimals=3)
onsets_s = np.round(onset_inds / samp_freq, decimals=3)
offsets_s = np.round(offset_inds / samp_freq, decimals=3)

seq = Sequence.from_keyword(onsets_Hz=onsets_Hz,
offsets_Hz=offsets_Hz,
seq = Sequence.from_keyword(onset_inds=onset_inds,
offset_inds=offset_inds,
onsets_s=onsets_s,
offsets_s=offsets_s,
labels=labels
Expand Down
8 changes: 4 additions & 4 deletions src/crowsetta/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
'label',
'onset_s',
'offset_s',
'onset_Hz',
'offset_Hz',
'onset_ind',
'offset_ind',
'audio_path',
'annot_path',
'sequence',
Expand All @@ -26,8 +26,8 @@
'label': str,
'onset_s': float,
'offset_s': float,
'onset_Hz': int,
'offset_Hz': int,
'onset_ind': int,
'offset_ind': int,
'audio_path': str,
'annot_path': str,
'sequence': int,
Expand Down
22 changes: 11 additions & 11 deletions src/crowsetta/phn.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ def phn2annot(annot_path,

annot = []
for a_phn in annot_path:
labels, onsets_Hz, offsets_Hz = [], [], []
labels, onset_inds, offset_inds = [], [], []
with open(a_phn) as fp:
lines = fp.read().splitlines()
for line in lines:
onset, offset, label = line.split()
onsets_Hz.append(int(onset))
offsets_Hz.append(int(offset))
onset_inds.append(int(onset))
offset_inds.append(int(offset))
labels.append(label)

onsets_Hz = np.asarray(onsets_Hz)
offsets_Hz = np.asarray(offsets_Hz)
onset_inds = np.asarray(onset_inds)
offset_inds = np.asarray(offset_inds)
labels = np.asarray(labels)

# checking for audio_pathname need to be case insensitive
Expand All @@ -89,8 +89,8 @@ def phn2annot(annot_path,
)

samp_freq = soundfile.info(audio_pathname).samplerate
onsets_s = onsets_Hz / samp_freq
offsets_s = offsets_Hz / samp_freq
onsets_s = onset_inds / samp_freq
offsets_s = offset_inds / samp_freq

if round_times:
onsets_s = np.around(onsets_s, decimals=decimals)
Expand All @@ -104,8 +104,8 @@ def phn2annot(annot_path,
a_phn = os.path.basename(a_phn)

phn_seq = Sequence.from_keyword(labels=labels,
onsets_Hz=onsets_Hz,
offsets_Hz=offsets_Hz,
onset_inds=onset_inds,
offset_inds=offset_inds,
onsets_s=onsets_s,
offsets_s=offsets_s)
annot.append(
Expand Down Expand Up @@ -174,8 +174,8 @@ def annot2phn(annot,
annot_path = Path(annot_path)

lines = []
onsets_Hz, offsets_Hz, labels = annot.seq.onsets_Hz, annot.seq.offsets_Hz, annot.seq.labels
for onset, offset, label in zip(onsets_Hz, offsets_Hz, labels):
onset_inds, offset_inds, labels = annot.seq.onset_inds, annot.seq.offset_inds, annot.seq.labels
for onset, offset, label in zip(onset_inds, offset_inds, labels):
lines.append(
f'{onset} {offset} {label}\n'
)
Expand Down
Loading

0 comments on commit da1f84f

Please sign in to comment.