Skip to content

Commit

Permalink
Merge branch 'main' into intervals_streamlit_only
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardFreedman committed Sep 12, 2024
2 parents 62e34de + 2adfaaa commit 59c39f7
Show file tree
Hide file tree
Showing 88 changed files with 4,919 additions and 691 deletions.
18 changes: 14 additions & 4 deletions .github/workflows/interval_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

strategy:
matrix:
python-version: [ '3.10', '3.11' ]
python-version: [ '3.10', '3.11', '3.12']

steps:
- name: Use Node.js 20.x
Expand Down Expand Up @@ -39,17 +39,27 @@ jobs:
python -m pytest --cov visualizations --doctest-modules --junitxml=junit/test-results.xml --cov=com --cov-report term-missing --cov-report=html
python -m pytest --cov main_objs --doctest-modules --junitxml=junit/test-results.xml --cov=com --cov-report term-missing --cov-report=html
- name: Install poetry and "patch" the build
if: ${{ github.ref == 'refs/heads/main' && matrix.python-version == '3.11' }}
run: |
python -m pip install --upgrade pip
pip install poetry
poetry self add poetry-bumpversion
poetry version patch
- name: Publish to Pypi
if: ${{ matrix.python-version == 3.9 }}
if: ${{ github.ref == 'refs/heads/main' && matrix.python-version == '3.11' }} # This ensures publishing only happens on main branch
run: |
poetry config pypi-token.pypi "${{ secrets.PYPI_TOKEN }}"
poetry publish --build
- name: Update Markdown with New Version
if: ${{ github.ref == 'refs/heads/main' && matrix.python-version == '3.11' }} # Same condition to ensure it runs only on main branch
run: |
VERSION=$(poetry version -s)
sed -i "s/Current Version: .*/Current Version: $VERSION/" README.md
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git commit -am "Update README.md with new version $VERSION [skip ci]" # Add [skip ci] to prevent re-triggering
git push
- uses: actions/checkout@v4
- uses: stefanzweifel/git-auto-commit-action@v4

Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# CRIM Intervals: Python Tools for the Analysis of Encoded Music Scores

### Current Version: 2.0.12
### Current Version: 2.0.44

[CRIM Intervals on github](https://github.com/HCDigitalScholarship/intervals/tree/main)

Expand Down Expand Up @@ -118,9 +118,11 @@ All CRIM intervals tools are available via a **Creative Commons** license (Attri
* [10_Lyrics_Homorhythm](https://github.com/HCDigitalScholarship/intervals/blob/main/tutorial/10_Lyrics_Homorhythm.md)
* [11_Cadences](https://github.com/HCDigitalScholarship/intervals/blob/main/tutorial/11_Cadences.md)
* [12_Presentation_Types](https://github.com/HCDigitalScholarship/intervals/blob/main/tutorial/12_Presentation_Types.md)
* [13_Model_Finder](https://github.com/HCDigitalScholarship/intervals/blob/main/tutorial/13_Model_Finder.md)
* [14_Visualizations_Summary](https://github.com/HCDigitalScholarship/intervals/blob/main/tutorial/14_Visualizations_Summary.md)
* [13_Musical_Examples_Verovio](https://github.com/HCDigitalScholarship/intervals/blob/main/tutorial/13_Musical_Examples_Verovio.md)
* [14_Model_Finder](https://github.com/HCDigitalScholarship/intervals/blob/main/tutorial/14_Model_Finder.md)
* [15_Network_Graphs](https://github.com/HCDigitalScholarship/intervals/blob/main/tutorial/15_Network_Graphs.md)
* [16_Python_Basics](https://github.com/HCDigitalScholarship/intervals/blob/main/tutorial/16_Python_Basics.md)
* [17_Pandas_Basics](https://github.com/HCDigitalScholarship/intervals/blob/main/tutorial/17_Pandas_Basics.md)
* [18_Music21_Basics](https://github.com/HCDigitalScholarship/intervals/blob/main/tutorial/18_Music21_Basics.md)
* [18_Visualizations_Summary](https://github.com/HCDigitalScholarship/intervals/blob/main/tutorial/18_Visualizations_Summary.md)
* [19_Music21_Basics](https://github.com/HCDigitalScholarship/intervals/blob/main/tutorial/19_Music21_Basics.md)
* [99_Local_Installation](https://github.com/HCDigitalScholarship/intervals/blob/main/tutorial/99_Local_Installation.md)
4 changes: 2 additions & 2 deletions crim_intervals/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .main import *
# from .classify import *
#from .main_objs import *
# from .main_objs import *
from .visualizations import *
__version__ = "2.0.12"
__version__ = "2.0.44"
Binary file added crim_intervals/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file added crim_intervals/__pycache__/main.cpython-311.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
30 changes: 15 additions & 15 deletions crim_intervals/test_vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,21 +181,21 @@ def ngrams_heatmap_test_helper(model, notes):
# # helper_test_generate_ngrams_and_dur(model, mel_chromatic, -1)


def test_comparisons_heatmap():
# pieces
df_relationships = pd.DataFrame(RELATIONSHIPS_DICT_EXAMPLE)
relationships_chart = viz.plot_comparison_heatmap(df_relationships, 'model_observation.ema',
main_category='relationship_type', other_category='observer.name',
heat_map_width=800, heat_map_height=300)
assert isinstance(relationships_chart, alt.VConcatChart)
assert len(relationships_chart.vconcat) == 2

df_observations = pd.DataFrame(OBSERVATIONS_DICT_EXAMPLE)
observations_chart = viz.plot_comparison_heatmap(df_observations, 'ema',
main_category='musical_type', other_category='observer.name',
heat_map_width=800, heat_map_height=300)
assert isinstance(observations_chart, alt.VConcatChart)
assert len(observations_chart.vconcat) == 2
# def test_comparisons_heatmap():
# # pieces
# df_relationships = pd.DataFrame(RELATIONSHIPS_DICT_EXAMPLE)
# relationships_chart = viz.plot_comparison_heatmap(df_relationships, 'model_observation.ema',
# main_category='relationship_type', other_category='observer.name',
# heat_map_width=800, heat_map_height=300)
# assert isinstance(relationships_chart, alt.VConcatChart)
# assert len(relationships_chart.vconcat) == 2

# df_observations = pd.DataFrame(OBSERVATIONS_DICT_EXAMPLE)
# observations_chart = viz.plot_comparison_heatmap(df_observations, 'ema',
# main_category='musical_type', other_category='observer.name',
# heat_map_width=800, heat_map_height=300)
# assert isinstance(observations_chart, alt.VConcatChart)
# assert len(observations_chart.vconcat) == 2


# TODO: review if this is still wanted and used. If not, remove it and see if ipywidgets can be removed as a dependency
Expand Down
47 changes: 44 additions & 3 deletions crim_intervals/visualizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@
# from ipywidgets import interact, fixed
from pyvis.network import Network

import matplotlib as mplt # OY addition 6/12/24


def create_bar_chart(variable, count, color, data, condition, *selectors):
color_scale = alt.Scale(
domain = data['pattern'].unique(),
range = data['color'].unique()
) # OY addition 6/12/24

observer_chart = alt.Chart(data).mark_bar().encode(
x=variable,
y=count,
color=color,
color=alt.Color('pattern:N', scale=color_scale, legend=alt.Legend(title='Pattern')), # OY change 6/12/24
opacity=alt.condition(condition, alt.value(1), alt.value(0.2))
).add_params(
*selectors
Expand All @@ -25,12 +31,16 @@ def create_bar_chart(variable, count, color, data, condition, *selectors):


def create_heatmap(x, x2, y, color, data, heat_map_width, heat_map_height, selector_condition, *selectors, tooltip):
color_scale = alt.Scale(
domain = data['pattern'].unique(),
range = data['color'].unique()
) # OY addition 6/12/24

heatmap = alt.Chart(data).mark_bar().encode(
x=x,
x2=x2,
y=y,
color=color,
color=alt.Color('pattern:N', scale=color_scale, legend=alt.Legend(title='Pattern')), # OY change 6/12/24
opacity=alt.condition(selector_condition, alt.value(1), alt.value(0.2)),
tooltip=tooltip
).properties(
Expand All @@ -39,7 +49,6 @@ def create_heatmap(x, x2, y, color, data, heat_map_width, heat_map_height, selec
).add_params(
*selectors
)

return heatmap


Expand Down Expand Up @@ -101,6 +110,36 @@ def process_ngrams_df(ngrams_df, ngrams_duration=None, selected_pattern=None, vo

return ngrams_df

# OY addition - new function for generating distinct colors 6/12/24
def generate_distinct_colors(n):
# Generate `n` distinct colors using the HSV color space
colors = []
for i in range(n):
hue = i / n
saturation = 0.65 # Fixed saturation
value = 0.9 # Fixed value
color = mplt.colors.to_hex(mplt.colors.hsv_to_rgb((hue, saturation, value)))
colors.append(color)
return colors

# OY addition - new function for adding colors 6/12/24
def ngrams_color_helper(new_processed_ngrams_df: pd.DataFrame) -> pd.DataFrame:
"""
Add a Series to the dataframe that assigns a unique hex color to each unique pattern
:param new_processed_ngrams_df: processed crim-intervals getNgram's output where tuples have been converted to strings
:return: a dataframe containing a new column with hex color values
"""

# Calculate the number of unique values in 'pattern_str'
num_unique_values = new_processed_ngrams_df['pattern'].nunique() # Function to count unique values in 'pattern_str' column
# # Generate enough hex colors
hex_colors = generate_distinct_colors(num_unique_values)
# # Step 2: Assign colors to unique values
color_map = dict(zip(new_processed_ngrams_df['pattern'].unique(), hex_colors))
# # Add a new column 'color' to the DataFrame
new_processed_ngrams_df['color'] = new_processed_ngrams_df['pattern'].map(color_map)

return new_processed_ngrams_df

def _plot_ngrams_df_heatmap(processed_ngrams_df, heatmap_width=800, heatmap_height=300, includeCount=False):
"""
Expand All @@ -122,6 +161,8 @@ def _plot_ngrams_df_heatmap(processed_ngrams_df, heatmap_width=800, heatmap_heig
new_processed_ngrams_df = processed_ngrams_df.copy()
new_processed_ngrams_df['pattern'] = processed_ngrams_df['pattern'].map(lambda cell: ", ".join(str(item) for item in cell), na_action='ignore')

new_processed_ngrams_df = ngrams_color_helper(new_processed_ngrams_df) # OY addition 6/12/24

heatmap = create_heatmap('start', 'end', y, 'pattern', new_processed_ngrams_df, heatmap_width, heatmap_height,
selector, selector, tooltip=['start', 'end', 'pattern'])
if includeCount:
Expand Down
Loading

0 comments on commit 59c39f7

Please sign in to comment.