netlogo export to csv - csv

I'm new to NetLogo so probably it's a dumb question.
while I'm trying to export turtles, patches, global variables to separated CSV files, this one works:
csv:to-file "turtles.csv" [ (list xcor ycor color shape) ] of turtles
but the following two don't:
csv:to-file "patches.csv" [ (list xcor ycor cluster-number) ] of patches
error: this code can't be run by a patch, only a turtle
csv:to-file "statistics.csv" (list meet meet-agg meetown meetown-agg meetother meetother-agg coopown coopown-agg coopother coopother-agg defown defown-agg defother defother-agg)
error: Extension exception: Expected a list of lists, but 1016 was one of the elements.
could someone help me with it? Thanks in advance!

About:
csv:to-file "patches.csv" [ (list xcor ycor cluster-number) ] of patches
I'm guessing the only problem is that you're trying to use xcor and ycor, which are turtle variables, instead of pxcor and pycor, which are patches variables. You need:
csv:to-file "patches.csv" [ (list pxcor pycor cluster-number) ] of patches
As for this one:
csv:to-file "statistics.csv" (list meet meet-agg meetown meetown-agg meetother meetother-agg coopown coopown-agg coopother coopother-agg defown defown-agg defother defother-agg)
one thing to keep in mind is that csv:to-file expects a "list of lists", and your code produces a single list, which is likely the problem. If all those variables are global variables, and you just want them in a single row in your CSV file, you could just wrap your list in another list:
csv:to-file "statistics.csv" (list (list meet meet-agg meetown meetown-agg meetother meetother-agg coopown coopown-agg coopother coopother-agg defown defown-agg defother defother-agg))
But it's hard to diagnose your problem precisely without more information. Can you tell us what makes you think it's not working? What are you trying to do?

Related

Reading a specific line of a csv file based on "ID"

I have a NetLogo model and I would like to assign values to patches from a .csv file. The patches have a certain value that I want to use as an "ID" and if "item 0" in a line of that .csv file matches the ID, the rest of the values in the line would be assigned to the patches with that ID.
I made a workaround that creates invisible turtles for each line in the files and then assigning the values is no problem, but is there a way to do this directly using just the csv file?
EDIT: Here's what the workaround does, first opens a file and then creates "helper" turtles like this:
while [ not file-at-end? ] [
let data csv:from-row file-read-line
create-turtles 1 [
set xcor 0
set ycor 0
set size 0
set color 0
set HPJid item 0 data
set A item 1 data
set B item 2 data
set iC item 3 data ] ]
Then I can just use:
ask patches [ let helper one-of turtles with [HPJid = HPJ of myself]
set D [A] of helper
set E [B] of helper
set F [C] of helper ]
Then all works, but I'd like a way to do this without the turtles.
Yes, having looked at your code, I think you can simply do:
while [ not file-at-end? ]
[ let data csv:from-row file-read-line
let in-ID item 0 data
ask one-of patches with [ID = in-ID]
[ set var1 item 1 data
set var2 item 2 data
]
]
or something like that anyway - with appropriate variable names of course

Assigning patch values from raster data in NetLogo

I am attempting to assign values to patches in NetLogo based upon raster values: 0, 1, and 2. These patches need only relate to the values of my raster, which does display properly using a greyscale, and then 'paint' themselves the colors blue, green, and white, respectively.
This raster data loads fine using the gis extension. Following gis:load-dataset, I attempt to use the apply-raster command and ifelse in order to give options based on the values. I believe I am misusing a boolean operator but very few examples online are as extensive as what I am attempting.
patches-own [value]
; Draws raster dataset (terrain of each Millenium)
to display-terrain
gis:paint terrain 62
ask patches [
(ifelse
value = 0 [
set pcolor blue
]
value = 1 [
set pcolor green
]
; elsecommands
[
set pcolor white
])
]
end
I currently cannot tell if the values are properly assigned and keep receiving the error that 'ifelse expects this to be a command block' so I assume the formatting is incorrect and/or a value association is missing.
Actually, you are using it exactly as the documentation says to use it, but you probably don't have the current version. The multiple choice ifelse is brand new in NetLogo v6.0.4. You need to explicitly include the cf extension and you need the extension name when calling the new ifelse syntax.
Earlier versions of NetLogo won't do this at all. The syntax you have is for v6.1 which has been released only in the last couple of weeks.
Try this for v6.0.4:
extensions [cf]
patches-own [value]
to testme
clear-all
ask patches [ set value one-of [0 1 2] ]
ask patches [
(cf:ifelse
value = 0 [
set pcolor blue
]
value = 1 [
set pcolor green
]
; elsecommands
[
set pcolor white
])
]
end

Assign turtle variables from csv in Netlogo

I want turtles to read and adopt data from csv file. I have written the following code: the problem is even-though the data gets loaded, i'm unable to make the individual turtles take on each of the income values. Any assistance to this effect would be appreciated
extensions [csv]
breed [households household]
households-own [income]
globals [income-data]
to setup
load-income-data
setup-households
end
to load-income-data
set income-data []
file-open "income.csv"
while [ not file-at-end? ]
[ set income-data sentence income-data ( file-read-line)
]
user-message "income data loading complete!"
file-close
end
to setup-households
create-households 700
ask one-of households
[ setxy random-xcor random-ycor
set income income-data
]
end
Have a look at the File Input Example in the NetLogo Model Library (Code Examples). You need to use a foreach to loop through the imported values / agents.

How do I set the number of turtles in each patch using a .csv file full of data?

Using some 35x12 matrix full of data in a .csv file, is there an easy way to import this into Netlogo and set the number of turtles equal to the elements of the matrix?
one way to do it is :
first read the file into a list , then use location of each item and see if a patches pxcor AND pycor matches the location of the item in the list, then I set that number as plabel for double checking the number of created turtles:
extensions [csv]
globals [li]
to setup
clear-all
resize-world 0 34 0 11
set-patch-size 30
load-File
ask patches [
set plabel item (pycor) item (pxcor) li
sprout item (pycor) item (pxcor) li
]
end
to load-File
set li []
file-open "t.csv"
if file-at-end? [ stop file-close ] ;; protect against end of file
while [not file-at-end? ]
[
let _line (csv:from-row file-read-line ",")
set li lput _line li
]
end

NetLogo - applying values to patches within polygons

I have animals walk around and a line then connects all the locations that they walked to. The line forms a closed polygon. I also used the graphics extension to fill the polygon for visual purposes. But I don't know how to have all of the patches that fall within the polygon become the territory of the owner (i.e., the animal that formed the polygon). It is possible for patches to be owned by multiple animals. The code below illustrates overlapping polygons. I'd really appreciate any help on this. Thanks!
extensions [graphics]
breed [animals animal]
breed [homeranges homerange]
animals-own
[
Name
X
Y
]
patches-own
[
owner
]
homeranges-own
[
Name
]
to setup
clear-all
random-seed 4
ask patches
[
set owner nobody
set pcolor grey
]
let $colors [brown orange violet sky lime]
let $Name ["t6" "t7" "t8" "t9" "t10"]
ask n-of 5 patches with [(pycor < 10 and pycor > -10) and (pxcor < 10 and pxcor > -10)]
[
sprout-animals 1
[
set shape "circle"
set color item who $colors
set pcolor color
set X (list xcor)
set Y (list ycor)
set Name item who $Name
set owner self
]
]
graphics:initialize min-pxcor max-pycor patch-size
reset-ticks
end
to go
repeat 5
[
ask animals
[
rt 45
fd 2
set X lput pxcor X
set Y lput pycor Y
set pcolor [color] of self
]
]
ask animals
[
pen-up
let tempXY (map [list ?1 ?2] X Y)
graphics:fill-polygon tempXY
; create a turtle, which draws the homerange boundary
hatch-homeranges 1
[
hide-turtle
set Name [Name] of myself
set color [color] of myself
]
; draw the homerange boundary
foreach tempXY
[
ask homeranges with [Name = [Name] of myself]
[
move-to patch (item 0 ?) (item 1 ?)
pen-down
]
]
; connect the last point of the homerange with the first one, to close the polygon
ask homeranges with [Name = [Name] of myself]
[
let lastpoint first tempXY
move-to patch (item 0 lastpoint) (item 1 lastpoint)
]
]
end
If you look at http://netlogo-users.18673.x6.nabble.com/Netlogo-Point-in-Polygon-td4980030.html you'll find a past (2012) discussion of solutions to this problem.
At the end of the thread, Jim Lyons posts a link to a model on the Modeling Commons, but the model doesn't seem to exist there anymore. He's here on Stack Overflow if you want to ask him about it.
An answer was provided in this post: NetLogo - misalignment with imported GIS shapefiles. The polygons are exported as a GIS shapefile and imported back in using the GIS extension. Then the gis:intersecting was used to give a variable to those patches that fall within the GIS-imported polygons.