How to create columns in a table using NetLogo 6.2 table extension?

157 views
Skip to first unread message

Rafaela Lorena

unread,
Feb 17, 2022, 9:41:57 AM2/17/22
to netlogo-users
Hi,

I'm having a hard time working with the table extension.

I want to export an output with 7 columns (n_initial, n_final, death, birth, reproduction (R), turtle profile code and tick)... But when reading the manual of the table extension I didn't find out how I can add columns and from what I understand it should be done in pairs. So, should I create 6 tables (table with turtle code and death, then turtle code and birth, then turtle code and n_initial, then turtle code and n_final and finally turtle code and reproduction)? I with some tweaks and help created a table with death, but I would like to add the birth, n_initial, n_final and reproduction columns. But, I'm having difficulty even with the help of the table extension manual. Can anyone tell me how I can do this, or suggest an example that does this? Below as far as I could get!

Thanks in advance!

extensions [ table ]

globals [ ListProfiles Death NInitial NFinal R Birth deaths-dict ]

turtles-own [ profiles-code metabolism-code reproduction-code metabolism reproduction resource-turtle ]

patches-own [ resources ]

to setup
  ca
  prepare
  set deaths-dict table:make
  ask patches [ set resources random 100 ]
  let list1 ( list 2 4 )
  let list2 ( list 5 10 )
  (
    foreach list1
  [
    this-metabolism ->

      foreach list2
      [
        this-reproduction ->
        ask n-of 1 patches
        [
          sprout 1
          [
            set metabolism this-metabolism
            set reproduction this-reproduction
            setup-turtles            
            table:put deaths-dict ( word metabolism-code reproduction-code ) 0
          ]
        ]
      ]
    ]
  )
  print deaths-dict
  reset-ticks
end

to setup-turtles
  (
    ifelse
    metabolism = 2 [ set metabolism-code "M1" ]
    metabolism = 4 [ set metabolism-code "M2" ]
  )

  (
    ifelse
    reproduction = 5 [ set reproduction-code "R1" ]
    reproduction = 10 [ set reproduction-code "R2" ]
  )
  set profiles-code ( word metabolism-code reproduction-code )
  print ( word "profiles-code: " profiles-code )
end

to go
  if not any? turtles [ stop ]
  ListProfilesProc
  MetaboProc
  ProbDieProc
  output
  tick  
end

to ListProfilesProc
  set ListProfiles sort remove-duplicates [ profiles-code ] of turtles
end

to MetaboProc
  ask turtles [
    (
      ifelse
    metabolism = 2
      [
        set resource-turtle ( resources - metabolism )
        if resource-turtle <= 60 [ DieProc ]
        (
          ifelse
          reproduction = 5
          [
            if resource-turtle >= 5 [ hatch 1 ]
          ]
          reproduction = 10
          [
            if resource-turtle >= 10 [ hatch 1 ]
          ]
        )
      ]
    )
  ]
end

to ProbDieProc
  ask turtles
  [
    let prob-die random-float 1
    if prob-die < 0.4 [
      DieProc
    ]
  ]
end

to DieProc
  let current-profile-death-count table:get deaths-dict profiles-code  
  let current-profile-new-death-count current-profile-death-count + 1  
  table:put deaths-dict profiles-code current-profile-new-death-count
  die
end

to prepare
  carefully
    [ file-delete ( word "output.csv" ) ]
  [ ]  
  file-open  ( word "output.csv" )
  file-print ( word "code_profile,death,tick" )
  file-close
end

to output
  file-open ( "output.csv" )
  foreach ListProfiles
  [
    t ->
    let profile-deaths table:get deaths-dict t
    file-print ( word t "," profile-deaths "," ticks "," birth )
  ]
  file-close
end

John Chen

unread,
Feb 17, 2022, 10:11:12 AM2/17/22
to Rafaela Lorena, netlogo-users
My intuition is to do a nested table. 

In an upcoming change for NLW, nested tables will be exported to nested JSON objects, and that seems neat for the data structure you described.

Rafaela Lorena <rafael...@gmail.com> 于2022年2月17日周四 08:41写道:
--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/CAHs8kB8hDCsz9Et1_K40d80Uy-%3D%2B0aoLJ%2B_hdDtEC4_hpy9ExQ%40mail.gmail.com.

James Steiner

unread,
Feb 17, 2022, 5:24:45 PM2/17/22
to Rafaela Lorena, netlogo-users
Hi!

You probably want the CSV extension. 

The name of Table extension may be misleading. It’s not about dat tables, like spreadsheets or CSV files. It’s key-value pairs, like JSON or dictionaries in other languages. 

Also: If you want to write out a report of columns of data, you can always simply use type and print, or the file versions, or build up a string and print or save that at once. 


To-report column [ value width ]
;;;; returns the value followed by spaces
;;;; so the result is ‘width’ characters 
     let spaces “(15 spaces, more if needed)“
     Report ( word substring ( word value spaces ) 0 ( width - 1 ) “ “)
End

To print-turtle-report
      Let vars [
      [ “who” 3]
      [ “xcor” 5]
      [ “ycor” 5]
     ]
;;;; print column headings
   ( Foreach vars [ v -> 
     Let name first v
     Let width last v
     Type column  name width
   ] print “”
     
;;;;; print column values
Ask turtles 
[
    Foreach vars [ v -> 
     Let value runresult (first v)
     Let width last v
     Type column  value width
   ] print “”
]

This can be modified to handle both left and right-alignment, delimiters, or table borders. 

Note also, you can also print html tables with similar code.

Let html-first [
“!DOCTYPE html”
”<html><head>”
“<title>Turtle Report</title>”
“</head>“
“<body>”
“<table>”
]
Let html-last [
“</table></body></html>”

Let html-mid “”

;;;  modify above to wrap columns in <td> and rows in <tr>, but as a string

;;; then display   or save the string. 

~~James
Reply all
Reply to author
Forward
0 new messages