Commit d7561ea8 authored by ecofractal's avatar ecofractal
Browse files

upload fractaL code

parent fc42d928
This diff is collapsed.
# fractaL
# fractaL
fractaL is a tool for promotion of digital literacy and ecological thinking using images and sounds from environmental data.
https://ecofractal.gitlab.io/
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
{"description":{"title":"Global Land and Ocean Temperature Anomalies, January","units":"Degrees Celsius","base_period":"1901-2000","missing":-999},"data":{"1880":"-0.01","1881":"-0.03","1882":"0.08","1883":"-0.27","1884":"-0.19","1885":"-0.44","1886":"-0.12","1887":"-0.40","1888":"-0.39","1889":"-0.10","1890":"-0.36","1891":"-0.37","1892":"-0.23","1893":"-0.67","1894":"-0.43","1895":"-0.43","1896":"-0.10","1897":"-0.18","1898":"-0.03","1899":"-0.10","1900":"-0.23","1901":"-0.12","1902":"-0.09","1903":"-0.21","1904":"-0.54","1905":"-0.35","1906":"-0.18","1907":"-0.35","1908":"-0.37","1909":"-0.55","1910":"-0.31","1911":"-0.51","1912":"-0.28","1913":"-0.35","1914":"0.09","1915":"-0.13","1916":"-0.18","1917":"-0.43","1918":"-0.21","1919":"-0.23","1920":"-0.11","1921":"-0.06","1922":"-0.31","1923":"-0.21","1924":"-0.25","1925":"-0.32","1926":"0.17","1927":"-0.16","1928":"-0.07","1929":"-0.47","1930":"-0.32","1931":"-0.05","1932":"0.16","1933":"-0.31","1934":"-0.32","1935":"-0.29","1936":"-0.26","1937":"-0.17","1938":"-0.04","1939":"-0.17","1940":"-0.13","1941":"0.17","1942":"0.29","1943":"-0.03","1944":"0.42","1945":"0.16","1946":"0.22","1947":"-0.19","1948":"0.05","1949":"0.12","1950":"-0.28","1951":"-0.29","1952":"0.15","1953":"0.10","1954":"-0.24","1955":"0.09","1956":"-0.18","1957":"-0.12","1958":"0.30","1959":"0.12","1960":"-0.00","1961":"0.12","1962":"0.13","1963":"0.04","1964":"0.05","1965":"-0.07","1966":"-0.04","1967":"-0.11","1968":"-0.19","1969":"-0.13","1970":"0.12","1971":"-0.00","1972":"-0.24","1973":"0.26","1974":"-0.19","1975":"0.12","1976":"-0.02","1977":"0.06","1978":"0.16","1979":"0.16","1980":"0.31","1981":"0.47","1982":"0.14","1983":"0.52","1984":"0.28","1985":"0.18","1986":"0.33","1987":"0.31","1988":"0.55","1989":"0.22","1990":"0.36","1991":"0.45","1992":"0.47","1993":"0.38","1994":"0.29","1995":"0.55","1996":"0.27","1997":"0.37","1998":"0.60","1999":"0.50","2000":"0.34","2001":"0.48","2002":"0.70","2003":"0.69","2004":"0.60","2005":"0.62","2006":"0.46","2007":"0.88","2008":"0.27","2009":"0.59","2010":"0.70","2011":"0.47","2012":"0.42","2013":"0.58","2014":"0.69","2015":"0.82","2016":"1.06","2017":"0.91","2018":"0.72","2019":"0.88"}}
\ No newline at end of file
from . import midisource
from .core import *
__version__ = '0.0.2'
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Using specific functions for sonification. This code is partially inspired by
the project Sonify (https://github.com/erinspace/sonify) with some
modifications to be applied on environmental data.
Although some parameters were changed, we decided to keep some functions name
as originally defined by Erin Braswell at her work.
Currently our focus will be the temperature, conductivity and salinity data.
Further steps: biodiversity, birds and big mammals migratory data.
For visual analysis of discrete nonlinear dynamical systems most of the code
bases its function on Pynamical package.
TODO
Lacunarity Test - Measure of the nonuniformity (heterogeneity) of structure or
the degree of structural variance within an object
version 0.0.2
June, 7th 2019
sjacques
"""
import csv
import io
import numpy as np
import pygame
from midiutil.MidiFile import MIDIFile
from pretty_midi import note_name_to_number
from time import sleep
from .midisource import KEYS, INSTRUMENTS, PERCUSSION
'''
FRACTAL GEOMETRY ANALYSIS
=========================
###########
#Box Count#
###########
Estimation of Fractal Dimension:
Minkowski–Bouligand dimension (computed)
Haussdorf dimension (theoretical)
'''
def rgb2gray(rgb):
r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray
'''
the threshold must to be set up according to the image
'''
def fracdim(Z, threshold):
assert(len(Z.shape) == 2)
def boxcount(Z, k):
S = np.add.reduceat(
np.add.reduceat(Z, np.arange(0, Z.shape[0], k), axis=0),
np.arange(0, Z.shape[1], k), axis=1)
return len(np.where((S > 0) & (S < k*k))[0])
Z = (Z < threshold)
p = min(Z.shape)
n = 2**np.floor(np.log(p)/np.log(2))
n = int(np.log(n)/np.log(2))
sizes = 2**np.arange(n, 1, -1)
counts = []
for size in sizes:
counts.append(boxcount(Z, size))
coeffs = np.polyfit(np.log(sizes), np.log(counts), 1)
return -coeffs[0]
'''
SONIFICATION
============
'''
'''
Defining the starting note based on the subtraction of y_values by notes_in_key
new_y.append create values based on the sum of y and traspose_value
'''
def make_first_number_match_key(y_values, notes_in_key):
first_note_in_key = notes_in_key[0]
transpose_value = first_note_in_key - y_values[0]
new_y = []
for y in y_values:
new_y.append(y + transpose_value)
return new_y
'''
Convert a key name to notes, using C3=60
:param key: String matching one of the values in pre-defined KEY dict
:param octave_start: octave for the first note, as defined by C3=60
:param number_of_octaves: The number of octaves to include in the list
'''
def key_name_to_notes(key, octave_start=1, number_of_octaves=4):
key = KEYS.get(key)
if not key:
raise ValueError('Opps! No key by that name found')
notes = []
octave = octave_start + 1
while len(notes) < number_of_octaves * 7:
for note in key:
note_with_octave = note + str(octave)
note_number = note_name_to_number(note_with_octave)
if note_number % 12 == 0 and len(notes) != 0:
octave += 1
note_with_octave = note + str(octave)
note_number = note_name_to_number(note_with_octave)
notes.append(note_number)
return notes
'''
Define notes using the more close/possible value of MIDI.
It's done sorting the "possible values" according to lambda i: abs(i - value)
'''
def get_closest_midi_value(value, possible_values):
return sorted(possible_values, key=lambda i: abs(i - value))[0]
'''
midi notes have a range of 0 - 127. Make sure the data is in that range
data: list of tuples of x, y coordinates for pitch and timing
min: min data value, defaults to 0
max: max data value, defaults to 127
return: data, but y normalized to the range specified by min and max
'''
def scale_y_to_midi_range(data, new_min=0, new_max=127):
if new_min < 0 or new_max > 127:
raise ValueError('Midi notes must be in a range from 0 - 127')
x, y = zip(*data)
new_y = scale_list_to_range(y, new_min, new_max)
return list(zip(x, new_y))
'''
Use the min and max values from "old_value" and the new parameters to set scaled
values inside the range
'''
def get_scaled_value(old_value, old_min, old_max, new_min, new_max):
return ((old_value - old_min)/(old_max - old_min)) * (new_max - new_min)
+ new_min
'''
set a list inside a MIDI range defined by new_min and new_max
the output is based on the list_to_scale and uses get_scaled_value function
'''
def scale_list_to_range(list_to_scale, new_min, new_max):
old_min = min(list_to_scale)
old_max = max(list_to_scale)
return [get_scaled_value(value, old_min, old_max, new_min, new_max) for
value in list_to_scale]
'''
Restrict the x range to something that's a multiple of the number of steps given
'''
def quantize_x_value(list_to_quantize, steps=0.5):
quantized_x = []
for x in list_to_quantize:
quantized_x.append(round(steps * round(float(x) / steps), 2))
return quantized_x
'''
Access the midisource.py file and set up the instrument sound
'''
def get_instrument(instrument_name):
instrument_type = 'melodic'
program_number = INSTRUMENTS.get(instrument_name.lower())
if not program_number:
program_number = PERCUSSION.get(instrument_name.lower())
instrument_type = 'percussion'
if not program_number:
raise AttributeError('No instrument could be found by that name')
return program_number - 1, instrument_type
'''
Define the key. As defined before, the percussion is a default. Otherwise it
will be necessary to use key_name_to_notes, make_first_number_match_key and
scale_list to_range to fit the range for other instruments.
line 140: Finding the index of the note closest to all the notes in the
options list
'''
def convert_to_key(data, key, number_of_octaves=4):
instrument, instrument_type = None, None
if type(data[0]) != tuple:
instrument = data.pop(0)
_, instrument_type = get_instrument(instrument)
x, y = zip(*data)
if instrument_type == 'percussion':
new_y = y
else:
notes_in_key = key_name_to_notes(key,
number_of_octaves=number_of_octaves)
transposed_y = make_first_number_match_key(y, notes_in_key)
scaled_y = scale_list_to_range(transposed_y, new_min=min(notes_in_key),
new_max=max(notes_in_key))
new_y = []
for note in scaled_y:
new_y.append(get_closest_midi_value(note, notes_in_key))
processed_data = list(zip(x, new_y))
if instrument:
processed_data = [instrument] + processed_data
return processed_data
'''
This function is applied to JSON data (in this case for climate data)
'''
def normalize_climate_data(climate_json):
years = [int(year) for year in climate_json['data'].keys()]
temp_anomolies = [float(temp_anomaly) for temp_anomaly in
climate_json['data'].values()]
normalized_years = scale_list_to_range(years, new_min=0, new_max=30)
normalized_temp_anomolies = scale_list_to_range(temp_anomolies, new_min=30,
new_max=127)
normed_climate_data = list(zip(normalized_years, normalized_temp_anomolies))
return normed_climate_data
'''
Import df to list and certify we don't have NaN
With this code we generate a nested list of tuples for multitrack analisys
'''
def normalize_climate_multi(df):
df = df.replace(np.nan, 0)
#From df to list: year is the key and the othe variables will be its values
years_list = [int(year) for year in df['Date'].keys()]
temperature_list = [float(temp) for temp in df['Temperature'].tolist()]
conductivity_list = [float(conduct) for conduct in df['Conduct'].tolist()]
salinity_list = [float(sal) for sal in df['Salinity'].tolist()]
#normalize data
normalized_years_multi = scale_list_to_range(years_list, new_min=0,
new_max=30)
normalized_temp_multi = scale_list_to_range(temperature_list, new_min=30,
new_max=127)
normalized_cond_multi = scale_list_to_range(conductivity_list, new_min=30,
new_max=127)
normalized_sal_multi = scale_list_to_range(salinity_list, new_min=30,
new_max=127)
normed_climate_multi = list(zip( normalized_years_multi,
normalized_temp_multi))
normed_cond_multi = list(zip( normalized_years_multi,
normalized_cond_multi))
normed_sal_multi = list(zip( normalized_years_multi,
normalized_sal_multi))
normed_multi = [normed_climate_multi]+[normed_cond_multi]+[normed_sal_multi]
return(normed_multi)
'''
To use JSON with MIDItime library. It converts the df to a dictionary
'''
def csv_to_MIDITime_data(filename):
mydata = []
with open(filename, 'r') as f:
reader=csv.reader(f)
next(reader, None) #this is added only in case of a file with header
for row in reader:
mydict = {'days_since_epoch': float(row[0]) ,
'magnitude': float(row[1])}
mydata.append(mydict)
return mydata
"""
Export the MIDIfile
data: dictionary of x, y coordinates for pitch and timing
Optional: add a string to the start of the data list to specify instrument!
type: the type of data passed to create tracks. Either 'single' or 'multiple'
"""
def write_to_midifile(data, track_type='single'):
if track_type not in ['single', 'multiple']:
raise ValueError('Track type must be single or multiple')
if track_type == 'single':
data = [data]
memfile = io.BytesIO()
midifile = MIDIFile(numTracks=len(data), adjust_origin=False)
track = 0
time = 0
program = 0
channel = 0
duration = 1
volume = 90
for data_list in data:
midifile.addTrackName(track, time, 'Track {}'.format(track))
midifile.addTempo(track, time, 120)
instrument_type = 'melodic'
if type(data_list[0]) != tuple:
program, instrument_type = get_instrument(data_list.pop(0))
if instrument_type == 'percussion':
volume = 100
channel = 9
# Write the notes we want to appear in the file
for point in data_list:
time = point[0]
pitch = int(point[1]) if instrument_type == 'melodic' else program
midifile.addNote(track, channel, pitch, time, duration, volume)
midifile.addProgramChange(track, channel, time, program)
track += 1
channel = 0
midifile.writeFile(memfile)
return memfile
'''
To play MIDI without having to save to a file
This is is using pygame as we can see.
'''
def play_memfile_as_midi(memfile):
pygame.init()
pygame.mixer.init()
memfile.seek(0)
pygame.mixer.music.load(memfile)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
sleep(1)
print('Done playing!')
"""
As input_data it is used a list of tuples, or a list of lists of tuples to add
as separate tracks
e.g:
input_data = [(1, 7), (7, 9)]
OR
input_data = [
[(1, 3), (5, 2)],
[(4, 1), (3, 12)]
]
key: key to play back the graph -- see constants.py for current choices
number_of_octaves: number of octaves used to restrict the music playback
when converting to a key
optional -- append an instrument name to the start of each data list to play
back using that program number!
"""
def play_midi_from_data(input_data, key=None, number_of_octaves=4,
track_type='single'):
if key:
if track_type == 'multiple':
data = []
for data_list in input_data:
data.append(convert_to_key(data_list, key, number_of_octaves))
else:
data = convert_to_key(input_data, key, number_of_octaves)
else:
data = input_data
memfile = write_to_midifile(data, track_type)
play_memfile_as_midi(memfile)
\ No newline at end of file
NOTES = [
['C'], ['C#', 'Db'], ['D'], ['D#', 'Eb'], ['E'], ['F'], ['F#', 'Gb'],
['G'], ['G#', 'Ab'], ['A'], ['A#', 'Bb'], ['B']
]
def get_keys():
base_keys = {
'c_major': ['C', 'D', 'E', 'F', 'G', 'A', 'B'],
'd_major': ['D', 'E', 'F#', 'G', 'A', 'B', 'C#'],
'e_major': ['E', 'F#', 'G#', 'A', 'B', 'C#', 'D#'],
'f_major': ['F', 'G', 'A', 'Bb', 'C', 'D', 'E', 'F'],
'g_major': ['G', 'A', 'B', 'C', 'D', 'E', 'F#'],
'a_major': ['A', 'B', 'C#', 'D', 'E', 'F#', 'G#', 'A'],
'b_major': ['B', 'C#', 'D#', 'E', 'F#', 'G#', 'A#', 'B'],
'c_sharp_major': ['Db', 'Eb', 'F', 'Gb', 'Ab', 'Bb', 'C', 'Db'],
'd_sharp_major': ['Eb', 'F', 'G', 'Ab', 'Bb', 'C', 'D'],
'f_sharp_major': ['F#', 'G#', 'A#', 'B', 'C#', 'D#', 'F', 'F#'],
'g_sharp_major': ['Ab', 'Bb', 'C', 'Db', 'Eb', 'F', 'G', 'Ab'],
'a_sharp_major': ['Bb', 'C', 'D', 'Eb', 'F', 'G', 'A', 'Bb']
}
base_keys['d_flat_major'] = base_keys['c_sharp_major']
base_keys['e_flat_major'] = base_keys['d_sharp_major']
base_keys['g_flat_major'] = base_keys['f_sharp_major']
base_keys['a_flat_major'] = base_keys['g_sharp_major']
base_keys['b_flat_major'] = base_keys['a_sharp_major']
return base_keys
KEYS = get_keys()
# Instrument and Percussion map from
# https://www.midi.org/specifications/item/gm-level-1-sound-set
INSTRUMENTS = {
'accordion': 22,
'acoustic bass': 33,
'acoustic grand piano': 1,
'acoustic guitar (nylon)': 25,
'acoustic guitar (steel)': 26,
'agogo': 114,
'alto sax': 66,
'applause': 127,
'bagpipe': 110,
'banjo': 106,
'baritone sax': 68,
'bassoon': 71,
'bird tweet': 124,
'blown bottle': 77,
'brass section': 62,
'breath noise': 122,
'bright acoustic piano': 2,
'celesta': 9,
'cello': 43,
'choir aahs': 53,
'church organ': 20,
'clarinet': 72,
'clavi': 8,
'contrabass': 44,
'distortion guitar': 31,
'drawbar organ': 17,
'dulcimer': 16,
'electric bass (finger)': 34,
'electric bass (pick)': 35,
'electric grand piano': 3,
'electric guitar (clean)': 28,
'electric guitar (jazz)': 27,
'electric guitar (muted)': 29,
'electric piano 1': 5,
'electric piano 2': 6,
'english horn': 70,
'fiddle': 111,
'flute': 74,
'french horn': 61,
'fretless bass': 36,
'fx 1 (rain)': 97,
'fx 2 (soundtrack)': 98,
'fx 3 (crystal)': 99,
'fx 4 (atmosphere)': 100,
'fx 5 (brightness)': 101,
'fx 6 (goblins)': 102,
'fx 7 (echoes)': 103,
'fx 8 (sci-fi)': 104,
'glockenspiel': 10,
'guitar fret noise': 121,
'guitar harmonics': 32,
'gunshot': 128,
'harmonica': 23,
'harpsichord': 7,
'helicopter': 126,
'honky-tonk piano': 4,
'kalimba': 109,
'koto': 108,
'lead 1 (square)': 81,
'lead 2 (sawtooth)': 82,
'lead 3 (calliope)': 83,
'lead 4 (chiff)': 84,
'lead 5 (charang)': 85,
'lead 6 (voice)': 86,
'lead 7 (fifths)': 87,
'lead 8 (bass + lead)': 88,
'marimba': 13,
'melodic tom': 118,
'music box': 11,
'muted trumpet': 60,
'oboe': 69,
'ocarina': 80,
'orchestra hit': 56,
'orchestral harp': 47,
'overdriven guitar': 30,
'pad 1 (new age)': 89,
'pad 2 (warm)': 90,
'pad 3 (polysynth)': 91,
'pad 4 (choir)': 92,
'pad 5 (bowed)': 93,
'pad 6 (metallic)': 94,
'pad 7 (halo)': 95,
'pad 8 (sweep)': 96,
'pan flute': 76,
'percussive organ': 18,
'piccolo': 73,
'pizzicato strings': 46,
'recorder': 75,
'reed organ': 21,
'reverse cymbal': 120,
'rock organ': 19,
'seashore': 123,
'shakuhachi': 78,
'shamisen': 107,
'shanai': 112,
'sitar': 105,
'slap bass 1': 37,
'slap bass 2': 38,
'soprano sax': 65,
'steel drums': 115,
'string ensemble 1': 49,
'string ensemble 2': 50,
'synth bass 1': 39,
'synth bass 2': 40,
'synth drum': 119,
'synth voice': 55,
'synthbrass 1': 63,
'synthbrass 2': 64,
'synthstrings 1': 51,
'synthstrings 2': 52,
'taiko drum': 117,
'tango accordion': 24,
'telephone ring': 125,
'tenor sax': 67,
'timpani': 48,
'tinkle bell': 113,
'tremolo strings': 45,
'trombone': 58,
'trumpet': 57,
'tuba': 59,
'tubular bells': 15,
'vibraphone': 12,
'viola': 42,
'violin': 41,
'voice oohs': 54,
'whistle': 79,
'woodblock': 116,
'xylophone': 14
}
PERCUSSION = {
'acoustic bass drum': 35,
'acoustic snare': 38,
'bass drum 1': 36,
'cabasa': 69,
'chinese cymbal': 52,
'claves': 75,