User Tools

Site Tools


howtos:toolcoding:using_maps

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
howtos:toolcoding:using_maps [2011/06/14 17:11]
chris.strashok
howtos:toolcoding:using_maps [2011/06/17 16:03] (current)
chris.strashok
Line 1: Line 1:
 ====== Working with Maps ====== ====== Working with Maps ======
  
-Mapping allows you to pass the elements from one variable into the elemants of another variable based on defined map structure. Here we will outline the instances that mapping is used.+Mapping allows you to transform ​the data in one variable ​(defined by one or more informants) ​into another variable ​(with different informants), ​based on defined map structure. Here we will outline the instances that mapping is used.
  
 ===== Map Tool ===== ===== Map Tool =====
  
-The map tool is the main way to map the elements in one variable according to a map into the elements in another variable. There are three main types of maps that the map tool uses: full matrix, dimension and index. Examples and uses of these mapping types are explored below+The map tool is the main way to map the elements in one variable according to a map into the elements in another variable. There are three main types of maps that the map tool uses: full matrix, dimension and index. Examples and uses of these mapping types are explored below.
  
 ==== Full Matrix Mapping ==== ==== Full Matrix Mapping ====
Line 165: Line 165:
 ==== Dimension Mapping ==== ==== Dimension Mapping ====
  
-The dimension map is a quick map that is used to map a set of elements to another set of the elements. However for a dimension map to work the element names in both informants need to be spelled exactly the same. This method is useful for mapping data from a set of elements to a subset of those elements as seen in the example below. \\+The dimension map is a quick method ​that is used to map a set of elements to another set of the elements. However for a dimension map to work the element names in both informants need to be spelled exactly the same. This method is useful for mapping data from a set of elements to a subset of those elements as seen in the example below. \\
  
 {{:​howtos:​toolcoding:​mapdata3.gif|}} {{:​howtos:​toolcoding:​mapdata3.gif|}}
Line 195: Line 195:
 {{:​howtos:​toolcoding:​mapresults3.gif|image}} {{:​howtos:​toolcoding:​mapresults3.gif|image}}
  
 +==== Index Mapping ====
 +
 +Index mapping allows you to map the right-most dimension of a variable onto the new dimension. This is the most difficult method to use because you need to know the position in the array or index number of the element(s) you are interested in mapping as well as the order of your informants.
 +
 +This example demonstrates how you can take a subsection of elements from the data in the table below and map them into the complete set using both the one to one and many to one mapping parameters.
 +
 +{{:​howtos:​toolcoding:​mapdata4.gif|}}
 +
 +The supporting tool script code for building the informants and importing data from the data source can be found [[howtos:​toolcoding:​mapEx4|here]].
 +
 +As in the full matrix method we need to first create a map indicating how the elements relate to each other. The data parameter in the create tool indicates the position in the array or the index number that we will be mapping to. Also notice that the map tool requires you to specify the new dimension
 +
 +<​code>​
 +
 +local mapPriaire[pp] = create (; dim=prairieProv,​ dataType=integer,​ data="​4 5 6")
 +
 +regions_a[t,​r] = map ( prairieData[t,​pp],​ mapPriaire[pp];​ mapping=oneToOne,​ newDim=region)
 +
 +table (regions_a[t,​r])
 +
 +</​code>​
 +
 +As you can see in the following table data for Alberta, British Columbia and Saskatchewan has been individually mapped into the variable regions_a[t,​r].
 +
 +{{:​howtos:​toolcoding:​mapresults4a.gif|image}}
 +
 +As with the one to one mapping the map indicating how the elements related to each other needs to be created. However notice how the index numbers are all pointing to the same position in the array.
 +
 +<​code>​
 +
 +local mapAtlantic[ap] = create (; dim=atlanticProv,​ dataType=integer,​ data="​1 1 1 1")
 +
 +regions_b[t,​r] = map ( atlanticData[t,​ap],​ mapAtlantic[ap];​ mapping=manyToOne,​ newDim=region)
 +
 +table (regions_b[t,​r])
 +
 +</​code>​
 +
 +In contrast to the one to one example all of the values for the Atlantic provinces have been aggregated and mapped into the Atlantic element in the regions_b[t,​r] variable.
 +
 +{{:​howtos:​toolcoding:​mapresults4b.gif|image}}
 +
 +===== Mapcat Tool =====
 +
 +A //​category//​ is a useful way of grouping elements into meaningfull collections. By categorizing related elements together you can create elements that are more descriptive while also reducing the number of elements you are carrying through your model.
 +
 +The mapcat tool can also be used for aggregating elements into classes or groups. The example below uses the data found in the following table to use the categorization as a map to transform the population data for each province to a region class while aggregating over each province to get the total population in each region.
 +
 +{{:​howtos:​toolcoding:​mapdata5_rev1.gif|}}
 +
 +The supporting tool script code for building the informants and importing data above can be found [[howtos:​toolcoding:​mapEx5|here]].
 +
 +The mapping template for a category is held in the category itself. As you can see in the code below the provinces is the group place holder and the locations are members of the province and they are related to each based on a relationship provided by the modeler.
 +
 +<​code>​
 +
 +localinformant locProv[l->​p] = create (; object=category,​ groups=location,​ members=province,​ \
 + "​west"​="​Alberta",​ \
 + "​west"​="​British Columbia",​\
 + "​east"​="​Newfoundland",​\
 + "​east"​="​Nova Scotia",​\
 + "​central"​="​Ontario",​\
 + "​east"​="​PEI"​)
 +
 +</​code>​
 +
 +This categorization or map can then be applied using the mapcat tool below to bring the elements together in the category.
 +
 +<​code>​
 +
 +local provLoc_Cat[l->​p] = mapcat (provinceData[p];​ category=locProv)
 +
 +table (provLoc_Cat[l->​p])
 +
 +</​code>​
 +
 +See how the province and location informants have been categorized into the provLoc category.
 +
 +{{:​howtos:​toolcoding:​mapresults5a_rev1.gif|}}
 +
 +Now we can use the mapcat tool again, but this time we will be splitting the category back up into the respective group and members.
 +
 +<​code>​
 +
 +local regions[l,​p] = mapcat (provLoc_Cat[l->​p])
 +
 +table (regions[l,​p])
 +
 +</​code>​
 +
 +See how we have now created a two dimensional array from a data set that only had one dimension.
 +
 +{{:​howtos:​toolcoding:​mapresults5c.gif|}}
 +
 +We can now sum up the provinces dimension to get the total population for each region as seen in the table below.
 +
 +<​code>​
 +
 +local regions_sum[l] = sum (regions[l,​p];​ \
 + dim=province)
 +
 +table (regions_sum[l])
 +
 +</​code>​
 +
 +{{:​howtos:​toolcoding:​mapresults5b_rev1.gif|}}
 +
 +This process has been broken down inorder to see how the transformation occurs, however there is no need to take this many steps. This process can be done on one line by combining the two mapcat and sum tool commands as follows:
 +
 +<​code>​
 +
 +local regions[l] = sum (mapcat (mapcat (provinceData[p];​ \
 + category=provLoc);​ \
 + dim=provLoc);​ \ 
 + dim=province)
 +
 +</​code>​
howtos/toolcoding/using_maps.1308071476.txt.gz ยท Last modified: 2011/06/14 17:11 by chris.strashok