... | ... | @@ -20,30 +20,27 @@ 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
|
|
|
```
|
|
|
|
|
|
While for dataframe we have this one:
|
|
|
normalize data, From df to list: year is the key and the othe variables will be its values
|
|
|
|
|
|
```python
|
|
|
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,
|
... | ... | @@ -64,3 +61,75 @@ def normalize_climate_multi(df): |
|
|
|
|
|
return(normed_multi)
|
|
|
```
|
|
|
|
|
|
#### Using MIDI
|
|
|
We can use both the direct reproduction
|
|
|
|
|
|
```python
|
|
|
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)
|
|
|
```
|
|
|
|
|
|
and export a MIDI file
|
|
|
|
|
|
|
|
|
```python
|
|
|
def export_midi(data, key=None, number_of_octaves=4, bpm = 120,
|
|
|
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]
|
|
|
|
|
|
expfile = 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, bpm)
|
|
|
|
|
|
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
|
|
|
|
|
|
expfile = open("output.mid", 'wb')
|
|
|
midifile.writeFile(expfile)
|
|
|
midifile.close()
|
|
|
|
|
|
return expfile
|
|
|
``` |