Assign turtle variables from csv in Netlogo - csv

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.

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

netlogo export to 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?

creating agents using the CSV example in the models library in Netlogo

I have a question about the number of turtles created from this code :
to read-turtles-from-csv
file-close-all ; close all open files
if not file-exists? "turtles.csv" [
user-message "No file 'turtles.csv' exists! Try pressing WRITE-TURTLES-TO-CSV."
stop
]
file-open "turtles.csv" ; open the file with the turtle data
; We'll read all the data in a single loop
while [ not file-at-end? ] [
let data csv:from-row file-read-line
create-turtles 1 [
print "item column 4"
show item 4 data
]
]
file-close ; make sure to close the file
end
My turtles.csv file has only two rows, so I What is expected here is that create-turtles 1 is repeated for the number of rows and I have two agents for which 2 numbers in the 4th column get printed. Surprisingly though, 4 turtles are created! Why?
Thanks
I'm wondering if your turtles.csv is being read in as having more lines than it should? Try doing something like:
to read-file
file-close-all
file-open "turtles.csv"
while [not file-at-end?] [
print csv:from-row file-read-line
]
file-close-all
end
to see how Netlogo is reading the file. It seems like you're on the right track, otherwise- I just tested a similar file following your example and I got my two turtles as expected. Using a .csv file called "turtle_details.csv" that looks like:
color size heading
1 red 2 90
2 blue 4 180
I used this code to produce two turtles with the variables in the .csv:
extensions [ csv ]
to setup
ca
reset-ticks
file-close-all
file-open "turtle_details.csv"
;; To skip the header row in the while loop,
; read the header row here to move the cursor
; down to the next line.
let headings csv:from-row file-read-line
while [ not file-at-end? ] [
let data csv:from-row file-read-line
print data
create-turtles 1 [
set color read-from-string item 0 data
set size item 1 data
set heading item 2 data
]
]
file-close-all
end

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