Python Coding Semester Booklet

Page 1


TABLE OF CONTENTS PYTHON: An introduction to coding

01

Ÿ Ÿ

ASSIGNMENT 1: TURTLE 1.1. Polygonal contour 1.2. Circle contour

02 03 04

Ÿ Ÿ

ASSIGNMENT 2: AGENT-BASED PROGRAMMING 2.1. Predators and preys 2.2. Food chain

05 06 08

Ÿ

ASSIGNMENT 3: TIME VISUALIZATION 3. Time flower

13 14

Ÿ

ASSIGNMENT 4: DATA INVESTIGATION 4. Titanic survival rate investigation

18 19

ASSIGNMENT 5: MUSIC VISUALIZATION 5.1. MusicXML file data extraction 5.2. Musical network 5.3. Other examples

26 27 28 35

ASSIGNMENT 5 - DEVELOPMENT: REAL TIME MUSIC VISUALIZATION Ÿ 6. Musical flower

36 37

CONCLUSION

40

Ÿ Ÿ Ÿ


PYTHON An introduction

to coding

Firstly, I want to thank Professor Dr. Mark Meagher for an interesting module, introducing us to the language of coding. Although it might seem strange at first, now I actually find it really useful and, unexpectedly, it has some similarities to the architectural design process. From my point of view, coding and architecture, in their making process, both try to combine and balance the aesthetic and scientific value. Therefore, after the initial idea is addressed, a logical thinking process is required to achieved it while intentionally heading towards an visually effective graphic. Following that idea, five assignments in this booklet is organized with the same informative arrangement: 1. An abstract which includes the initial idea and the logical thinking process that led to the final result 2. Detail explanations of the making of the code, if necessary 3. The python code with implied information and narration. In each assignment, I tried to use different ideas and concepts about computational design that I have learned throughout the module. As a result, they gave out very interesting outcomes, in my opinion. In addition, I have developed an upgraded version of my 5th assignment (music visualization). This time, I combine it will Turtle (my 3rd assignment) to create particle movement that can visualize the relationship between musical notes in real time. It locates at the final chapter of this booklet. Binh Vinh Duc Nguyen - 01


TU

R T

ASSIGNMENT 1 This assignment is the first step for us to get used to python coding language, using Trinket. To control the movement of the turtle, several coordinate-transformation and mathematical functions was introduced.

L

E

Using these tools, I create some variations of turtle movement inspired by a mathematical geometry concept - the spiral, or infinite geometry division. The idea is: If we divide the measurable value of a geometry (e.g. length, area, volume, ...) to create a smaller geometry, the process can be repeated forever and give out notable results. From that concept, three kinds of geometry were applied in the assignment: 1. Spiral polygon 2. Polygonal contour 3. Circle contour I selected number 2 and 3 to show in this booklet, since number 1 (Spiral polygon) is respectively simple and non-innovative

Binh Vinh Duc Nguyen - 02


1.1. POLYGONAL CONTOUR In this function, new (or smaller) polygon is made by connecting the middle point of the previous (or larger) polygon. A mathematical formula evolving length and angle calculator is used to control the turtle’s movement.

Sides = 4; Number = 7

Sides = 5; Number = 8

Sides = 6; Number = 10

Sides = 9; Number = 25

Three input values are: . SIZE: length the first polygon’s segments . SIDES: number of segments of the first polygon . NUMBER: quantity of contour polygons or steps to process By changing these input values, different variations of the geometry were achieved. Python code:

Binh Vinh Duc Nguyen - 03


1.2. CIRCLE CONTOUR The idea of this function is to equally divide the circle into a number of point, then connect these points with arcs that have the same radius with the circle. The function has one input parameter: NUMBER, which is the quantity of arcs. Since this function used the turtle movement to draw geometry, these circles and arcs are actually poly-lines created by continuously rotate and move-forward the turtle. Thus, mathematical formulas were able to be used to calculate the movements required to draw the input number of inner arcs.

Number = 3

Number = 4

Number = 5

Number = 9

Number = 15

Number = 20

Python code:

Binh Vinh Duc Nguyen - 04


AGENT-BASED

programming ASSIGNMENT 2 The requirement of this assignment is to create one function that expands the range of available turtle behaviours. Since each turtle is expected to move individually, I developed an idea based on the concept of agent-based programming: 1. There are groups of agents (turtles), and each group has their own rules of behaviour. 2. Each agent (turtle) can perceive its position and other agents’ positions. It also can determine its next movement based on learning the current stage. 3. Agents will interact with each other, therefore forming ‘trends’ of movements based on their programmed behaviour. There are two functions that I created in this assignment: 1. Predators and preys, in which relationship between agent groups is a linear process. (A hunts B, B hunts C) 2. Food chain, a development from the previous function, in which agent groups interact with each other in an circular process, thus creating a loop in behaviour. (A hunts B, B hunts C, C hunts A). However, these functions have not adequately address an important aspect in agent-based modelling, which is interacting with environment. After a period of time, agents can move out of the screen thus unable for the viewer to track them. Some of other students in the class have created a ‘boundary’ to limit agents movement on the screen, which is an effective and worth-learning solution.

Binh Vinh Duc Nguyen - 05


2.1. PREDATORS AND PREYS This function creates hunting and running behaviours for the turtles, by dividing them into three groups: Ÿ Red turtles (predator) will hunt the blue turtle Ÿ Blue turtles (prey) will run away from the red turtles Ÿ The green turtle (top predator): this one turtle will hunt either red or blue turtle that is the closet to it, and both red and blue turtles have to run away from it. The function is made based on these activities: Divide the turtles into group by their index number (0-green, even number-red, odd numberblue) Ÿ Get the angle between 2 turtles' vectors by mathematical function (atan2) Ÿ Get the distance between green turtle and all other turtles, identify the closet prey, move forward it. Ÿ Get the distance between each red turtles and all blue turtles, identify the closet prey, get the angle, decide whether to run away from the green, or to hunt the red, move forward to the chosen direction Ÿ Get the distance between each blue turtles and all red turtles, identify the closet predator, get the angle, decide whether to run away from the green or the red, move forward to the chosen direction Ÿ

The pictures show patterns created by different numbers of turtle. Because they behavior in one-direction action (to hunt and to run), all the turtles seem to run away from the screen. The zig-zag line that usually against the big flow is of the green turtle and its chosen preys.

Binh Vinh Duc Nguyen - 06


Python code:

Binh Vinh Duc Nguyen - 07


2.2. FOOD CHAIN This function is a development of the previous function, with the same hunt and run behaviors, but with some additional points: 타 타

The turtles will be divided equally into 3 groups: red, blue, green They will be given roles in a 'loop' food-chain: the red hunts the blue, the blue hunts the green, and the green hunts the red. 타 If the prey is so close to the predator (in this case, if their distance is smaller than 10), the prey will turn in to the predator (in both its colour and behaviour) 타 The function will end if one of the turtle group has no turtle left (it means they are all be eaten and become their predators) Binh Vinh Duc Nguyen - 08


The function is added with some activities: The turtles have to find the closet predator and the closet prey The turtles have to decide whether to hunt the prey or run away from the predator. In this case, if the distance is so close, they will choose to run first. Ÿ The turtles have to decide their colour group based on the distance between them and their closet prey Ÿ Ÿ

This time, the turtles seem to get close together at a specific point on the screen, which is because they all follow their prey and at the same time - run from others. In each case, the winner group is different based on their initial positions that were randomly given. Binh Vinh Duc Nguyen - 09


Python code:

Binh Vinh Duc Nguyen - 10


Binh Vinh Duc Nguyen - 11


Binh Vinh Duc Nguyen - 12


ASSIGNMENT 3 In this assignment, I looked into the method of visualizing time, as a digital innovative version of a clock. There are many artistic installation that was created to show the flow of time, and some of them inspired me an idea of time-based particle movements. The initial concept is that particles (in this case, turtles), will move from the centre of the screen in real time, to create a ‘flower’ shape. The flower will show how much time have passed based on its size and the density of its particles. However, the function came up with a challenge: how to arrange the particle to move half-randomly but also halfintentionally, which is, making sure they don’t overlapped each other so that the flower can ‘grow’, and setting a movement rule to make the flower form symmetrically.

T I

Therefore, mathematical formulas based on time value was used to calculate (1) the angle at which the particle will rotate, and (2) the distance they will move to form the flower. The result ‘flower’ is an interesting visualization, which is, in my opinion, has the potential to be developed further in the future.

M visualization

E Binh Vinh Duc Nguyen - 13


3. TIME FLOWER In this function, a time-flower graphic is created: A number of turtles will be placed on a circle in the centre of the scene They will rotate and drawing lines to create a flower shape that show the development of itself through every seconds Ÿ After every minute, a new set of turtles will be created, with higher quantity Ÿ After one hour, the screen will be cleaned and the process will star again Ÿ Ÿ

The function is made based on these activities: Set a turtle list which forms a circle in the centre of the flower, the number of turtle is calculated by increasing the number of turtle in the previous list. Ÿ Get the angle between each turtle and the centre point, to create the movement direction. Ÿ Move the turtle with that direction in a random distance, which is controlled by a random range Ÿ A new set of turtle will be created every minute, and the whole turtle circle will be rotated by an angle calculated from the time value, to make sure they don't overlap the previous set. Ÿ

Binh Vinh Duc Nguyen - 14


Python code:

Binh Vinh Duc Nguyen - 15


Binh Vinh Duc Nguyen - 16


Binh Vinh Duc Nguyen - 17


DATA investigation ASSIGNMENT 4 In this assignment, the Titanic passenger dataset was investigated. Using Panda as a tool for data analysis, we were expected to determine the factors effecting the survival rate. My initial idea is to look at the data variables not individually, but as an complex context that resulted the survival rate. In order to do that, various variables had to be combined and analysis. Therefore, the challenges here are: 1. To create new data from previous data, which is either (1) a selection, arrangement or combination from different variables, or (2) a new estimated value or mathematical value calculated from different variables. 2. To fill the gaps existing in the dataset, based on calculation or assumption. 3. To effectively visualize various variables in a chart / graphic. Luckily, the library of Matlob has many interesting visualization style that can be applied. In the end, many new variables were conducted, for example, the survival ratio for each age group, gender, or ticket class; the title separated from the name; the family size and their quantity. Data analysis evolving combination of different variable was also used frequently, for example, age and ticket fare, age and gender, cabin and family size, ...

Binh Vinh Duc Nguyen - 18


4.TITANIC SURVIVAL RATE INVESTIGATION

Binh Vinh Duc Nguyen - 19


Binh Vinh Duc Nguyen - 20


Binh Vinh Duc Nguyen - 21


Binh Vinh Duc Nguyen - 22


Binh Vinh Duc Nguyen - 23


Binh Vinh Duc Nguyen - 24


Binh Vinh Duc Nguyen - 25


visualization

MUSIC

ASSIGNMENT 5

The main theme of this assignment is to visualize data. My initial idea is to use music as the source data set. In the past, there has already been many computational programming successfully visualizing music in real time, which mostly use the digital sound properties (for example, frequency, amplitude, speed, ...). This approach was able to create memorizing interactive graphics, however it can be used for every sound input, thus did not adequately address the nature of music. This function which I made have a more ‘analog’ approach. Musical elements, in this case, the notes, the chords, their position, arrangement and relationship are tools that composers used to create music. Therefore, if a graphic can visualize all that factors, the user can achieve a deeper understanding of the artist’s composing process, thus providing a different viewpoint for the user, from ‘audience’ to a kind of ‘musician’. The challenge here is how to find the music data source that can provide all those factors. Fortunately, there is one spectacular music coding-based file type that was developed for sharing musical sheet: MusicXML. There are also various website that provide Free XML file. As a result, there are two steps that the function have to address: 1. To extract musical variables from the XML file 2. To visualize those variables effectively Since the purpose of visualizing here is the ‘relationship’ between musical notes, I choose NetworkX as the suitable platform. Different types of network visualizing were experienced throughout the coding process, each has its own visual effect and provides different informative understanding from the musical piece. Binh Vinh Duc Nguyen - 26


5. 1. MUSICXML FILE DATA EXTRACTION This function will look into the MusicXML file format, which is respectively suitable for Python programming, trying to extract data from it and re-interpret those data into NetworkX input. Below is an example of a MusicXML file:

The structure of MusicXML file is simply musical elements value positioned inside various tag name, in order to provide necessary factors creating a musical sheet. Using the xml.dom library, those musical values can be extracted by the function dom.getElementsByTagName(’TagName’). Below is an example of extracted musical notes, with their position similar to that inside the musical sheet. Each note is a combination of 3 musical values extracted from XML file: Note step (A, B, C, ...), Note octave (1, 2, 3, ...) and Note type (half, quarter, eighth, 16th, ...). These information will be used later to visualize the musical network.

Binh Vinh Duc Nguyen - 27


5. 2. MUSICAL NETWORK Music is a kind of code that involves different combinations from the same series of notes, with different pitch and duration. In the 12-tone chromatic scale, each note has a name (for example, A4, B4, D5) showing their pitch or in other words, their position in the staff. A musical composition is made from specific arrangements of notes by the composer. Using these information, music can be visualized based on the relationship between the notes, in order to give the user a clear idea of the structure and musical method of the composer There are three variations of Network was conducted in this function: 5.2.1.The spring network

The data integrated in the nodes and edges of this network are: Each node shows a note and has the label which is the note's name (D3, C4, ...) The colour of the node show the duration, or type of the note (16th, eighth, quarter note, ...), so there can be different nodes with different colour but have the same label Ÿ The size of the node show the appearing frequency of the musical note in the piece, the bigger the node, the more that note appears Ÿ The edge connect 2 nodes together with the direction. For example: a-->b means note b is at the right of note a in the music piece Ÿ The weight of the edge shows the appearing frequency of the relationship in the piece. For example, if the edge a-->b is bold, it means the composition note a to note b appears many time in the music piece. Ÿ The colour of the edge shows their direction. For example, if an edge connect a-->b, it will have the colour of a. So the direction information is always visible even without the arrow Ÿ Ÿ

The spring network automatically groups the note with the most connections together, so that we can see the main chords and composition that the composer used to create the music piece. In this case, it can be seen that he used eighth notes the most, typically A3, B3, D4, ... Binh Vinh Duc Nguyen - 28


5.2.2. Circular network and Arrow Patch

This is an attempt to combine NetworkX and Matplot Patch. The nodes is located on the circle thanks to NetworkX Circular layout function, then their position is extracted and translated into list structures that can be used to draw the Curve arrow (in this case, FancyArrowPatch). Other information is the same as the previous network. 5.2.3.Advanced-position network

The idea of this network is to visualize the nodes based on their notes' position on the octaves. The note's octave number (1,2,3 in C1, B2, A3,...) is used to caculate the circle's radius, while their position on the circle is decided by the note's name (A,BC,D,E,...). This way, the data of the music piece is clearer for the user to see. Other information is the same as previous charts, except that the node's size is reduced to be the same in order to cause less confusion Binh Vinh Duc Nguyen - 29


Python code:

Binh Vinh Duc Nguyen - 30


Binh Vinh Duc Nguyen - 31


Binh Vinh Duc Nguyen - 32


Binh Vinh Duc Nguyen - 33


Binh Vinh Duc Nguyen - 34


5. 3. OTHER EXAMPLES This function can be applied to different XML files, for example, these are 2 musical pieces from Bach (left) and Beethoven (right). The visualization shows us while Bach’s Étude seems to be more improvised, Beethoven’s String Quartet is similar to a combination of various repetition between specific chords.

Binh Vinh Duc Nguyen - 35


real time visualization

MUSIC

ASSIGNMENT 5 - DEVELOPMENT This is a development from the 5th assignment (music visualization). The initial idea is to create a real-time graphic that can show the arrangement relationship of the notes. In order to do that, a programming platform providing particle movement is needed. Therefore, I use Turtle and combine the data analyst of the 5th assignment with time-based movement of the 3rd assignment. In this function, there are two works to do: (1) creating the musical instrument (that have unique notes and unique positions, just as a piano); and (2) creating the player movement that shows the musical progress from the first to the last note based on the music sheet. In the ‘musical instrument’, each turtle will present a unique note in the musical sheet. As a result, after data extraction, the note list need to be filtered and re-organized. The position of each turtle will present the name and octave of its note, calculated from the XML data. The colour of each turtle will be based on the note type (or the duration of each note). The turtle ‘player’ will move between the notes, at the same time will create a network of its footprint, showing the relationship between the notes as in the musical sheet.

Binh Vinh Duc Nguyen - 36


6. MUSICAL FLOWER This is a development from the 5th assignment (music visualization). The idea is to visualize the relationship between musical notes in real time. Using turtle movement, the function creates a socall ‘developing network' that can be used to understand deeper about the musical piece. There are seven steps in the function: Ÿ Extracting the information from the xml file, including: . The Note Octave . The Note Step . The Note Type Ÿ Filtering and sorting the note list into a unique-list based on the Note Step Ÿ Assign the turtles to the notes Ÿ Moving each turtle to a specific position: . Each turtle demonstrates a unique note . Each turtle will move to a direction which is the result of 360 degree divide to the number of turtles . The distance that each turtle will move is calculated from the Note Octave Ÿ Assign the main 'Player' turtle Ÿ The player turtle will move from the first note to the last note in the musical piece which is exactly similar to their arrangement in the music sheet. Ÿ The final result is a 'network' formed by the Player's movement

The picture shows the process of the function: (1) Placement of the note-turtles; (2) (3) The movement of the Player; (4) The final 'network' Binh Vinh Duc Nguyen - 37


Example for another XML file

The Python code:

Binh Vinh Duc Nguyen - 38


Binh Vinh Duc Nguyen - 39


CONCLUSION In conclusion, throughout five assignments, I was given opportunities to exercise my logical thinking, and to get used to various computational design concept, including: Ÿ Ÿ Ÿ Ÿ Ÿ

Coordinates movement and mathematical calculating functions. Concept of agent-based modelling. Time-based visualization. Data organization and investigation. Data re-creation, data extraction and informative visualization.

They are all notably useful knowledge for my study in digital design, as well as my thesis project. In the future, I hope to widen my knowledge in coding to another sub-category, which is user interactive programming. In my opinion, coding has a great potential for creating a more user-friendly environment in architectural digital co-design, and it’s really necessary for architects and architectural researchers to have knowledge about this field of science.

Binh Vinh Duc Nguyen - 40


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.