New Feature, Real time graph for Amanuensis

in #utopian-io6 years ago

Screenshot from 2018-07-21 11-03-27.png
Repository
https://github.com/to-the-sun/amanuensis

<p dir="auto"><br /> <a href="https://steemit.com/utopian-io/@to-the-sun/add-an-ever-updating-visual-graph-to-a-python-script-a-task-request-for-the-open-source-project-the-amanuensis-automated" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Link to the task request<span>This development contribution is submitted in association with task request by <a href="/@to-the-sun">@to-the-sun: <h3>What is Amanuensis? <p dir="auto"><span>The Amanuensis is an automated songwriting and recording system created by <a href="/@to-the-sun">@to-the-sun aimed at ridding the process of anything left-brained, so one need never leave a creative, spontaneous and improvisational state of mind, from the inception of the song until its final master. The program will construct a cohesive song structure, using the best of what you give it, looping around you and growing in real-time as you play. All you have to do is jam and fully written songs will flow out behind you wherever you go. <h3>New Feature <h5>The graph <p dir="auto">I implemented a graph for plotting the values in the dictionary <code>rhythm. Rhythm dictionary acted as a sparse array which gets updated as the time passes when the user starts recording. I had to plot the plot the data stored in the <code>rhythm dictionary from the moment referred in <code>spout[0]. The graph should also have to be updated in real time so I was needed to clear the plotted data after the plotting has been done. <strong>Matplotlib was used to plot the graph. <h3>Implementation <p dir="auto"><span>Python script: <a href="https://github.com/to-the-sun/amanuensis/blob/master/consciousness.py" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://github.com/to-the-sun/amanuensis/blob/master/consciousness.py <p dir="auto">To make the graph real time I created a separate process using Python's <code>multiprocessing library. Also, a <code>queue was created to pass the data to be plotted to the <code>drawing_process in real time. The Queue was made non-blocking to prevent the graph window from freezing, waiting for new data. <h3>Relevant codes <p dir="auto">Funtion for graph drawing process <pre><code>def draw(queue): """ Process to draw & update the graph. """ # Initializing Graph xlim = stats['wake'] + 25 ylim = 2 plt.ion() figure = plt.figure() figure.subplots_adjust(bottom=0.22) figure.patch.set_facecolor('#eff3b9') figure.canvas.set_window_title('Rhythm') axes = figure.add_subplot(111) axes.set_facecolor('#eff3b9') axes.set_xlim([0, xlim]) axes.set_ylim([0, ylim]) axes.set_title('Rhythm') axes.set_xlabel('ms') axes.set_ylabel('cumulative beat intervals') try: x_data, y_data = queue.get(False) except que.Empty: x_data, y_data = new_data() l, = axes.plot(x_data, y_data, '#757981') manager = figure.canvas.manager manager.window.wm_geometry("+{}-{}".format(10, 20)) manager.resize(550, 200) figure.canvas.draw() figure.canvas.flush_events() # Update Loop while True: try: x_data, y_data = queue.get(False) ylim = np.amax(y_data) + 1 axes.set_ylim([0, ylim]) if stats['wake'] > xlim: xlim = stats['wake'] + 25 axes.set_xlim([0, xlim]) l.set_xdata(x_data) l.set_ydata(y_data) figure.canvas.draw() figure.canvas.flush_events() except que.Empty: try: figure.canvas.start_event_loop(0.5) except TclError: print("Graph closed!") break except TypeError: pass except TclError: break <p dir="auto">Function to prepare new data for plotting. <pre><code>def new_data(keys=None, values=None): """ Function to prepare data for plotting the graph. """ ydata = np.zeros(stats['wake'] + 25, dtype='int') xdata = np.linspace(0, stats['wake'] + 25, stats['wake'] + 25, dtype='int') if keys is not None: i = 0 j = 0 while j < len(values): try: if i in keys: ydata[i] = values[j] j += 1 i += 1 except Exception: break return xdata, ydata <p dir="auto">Removing plotted values from the <code>rhythm dictionary after plotting. <pre><code>def remove_past_moments(r_dict, spout): """ Removing keys from the rhythm dict which denotes moments in the past Returns new rhythm dict. """ for key in r_dict.copy(): if key - spout < 0: r_dict.pop(key) return r_dict <p dir="auto">Preparing new plot data and sending it via the queue <pre><code>try: # Stripping off past moments from the rhythm dict. rhythm = remove_past_moments(rhythm, spout[0]) lists = sorted(rhythm.items()) x, y = zip(*lists) x = np.array(x) - spout[0] x, y = new_data(x, y) except ValueError as v: pass except Exception as e: print(e) finally: try: q.put((x, y)) except Exception as e: print(e) <p dir="auto"><strong>A working implementation of the graph can be seen in this video: <a href="https://dlive.io/livestream/to-the-sun/8e75a760-8c98-11e8-9b20-0242ac110003" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Video Link <h4>GitHub Account <p dir="auto"><span><a href="https://github.com/ajmaln" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://github.com/ajmaln
Sort:  

Thank you for your contribution.Its better to show the commits you have worked on. Also for this commit https://github.com/to-the-sun/amanuensis/commit/ddc558e0e2879e6fd9dee60dc1cce46759e11655, I can see a lot of changes so do try to explain that too if you have done that change.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.



Chat with us on Discord.
[utopian-moderator]Need help? Write a ticket on https://support.utopian.io/.


Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!Hey @ajmaln

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

That is amazing. Following.

Thank you so much.

Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.Congratulations! This post has been upvoted from the communal account, @minnowsupport, by ajmaln from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

What can I say, excellent work yet again! Great to be working with you.

Thank you

@resteemator is a new bot casting votes for its followers. Follow @resteemator and vote this comment to increase your chance to be voted in the future!