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

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

Related

Error with netlogo gis extension vertex-lists-of

I want to extract points from line feature from shapefile.
I have search for solutions, it shows that most of people use gis:vertex-lists-of to do this.
For example,
However I got an error message below when I try to do that:
Extension exception: not a VectorFeature:
org.myworldgis.netlogo.VectorDataset#ca24265
error while observer running GIS:VERTEX-LISTS-OF
called by procedure DRAW-CITIES
called by Button 'draw-cities'
Code below:
to draw-cities
gis:set-drawing-color red
gis:draw cities-dataset 1
foreach gis:vertex-lists-of cities-dataset [
a ->
]
end
cities-dataset is a points collection shapefile from Netlogo library ""GIS general examples" model
I don't understand that even I put a points feature in the function, I just got an error.
Do I misuse the function? How can I corectly use it? Thank you.
The reason for the error is because your foreach function is missing the action steps a foreach function looks something like this:
foreach [1 2 3][a -> print a + 1]
If you wanted to do something like the code you linked to then it would look like this:
extensions [ gis ]
globals [ cities-dataset]
breed [ cities city ]
breed [nodes node]
cities-own [ name country population ]
to setup
set cities-dataset gis:load-dataset "C:/Program Files/NetLogo 6.2.2/app/models/Code Examples/Extensions Examples/gis/data/cities.shp"
draw-cities
end
to draw-cities
gis:set-drawing-color red
gis:draw cities-dataset 1
foreach gis:feature-list-of cities-dataset[
i ->
foreach gis:vertex-lists-of i[
j ->
let first-node-point nobody
let previous-node-point nobody
foreach j [
k ->
let location gis:location-of k
if not empty? location [
ifelse any? nodes with [
xcor = item 0 location and ycor = item 1 location
]
[]
[
create-nodes 1[
set xcor item 0 location
set ycor item 1 location
set size 0.23
set shape "circle"
set color 23
set hidden? false
]
]
;to create links
let node-here (nodes with [
xcor = item 0 location and ycor = item 1 location
])
ifelse previous-node-point = nobody
[set first-node-point node-here]
[let who-node 0
let who-prev 0
ask node-here
[create-link-with previous-node-point
set who-node who]
ask previous-node-point[
set who-prev who
]
set previous-node-point one-of node-here
]
]
]
]
]
end
I'm not entirely sure what you're looking to do, but feel free to ask for more help.

Netlogo - item expected input to be a string but got zero instead

This is a follow-up question related to a previous post LinkI have data related to 16 laptop consumers' review ratings which are either satisfied (16 people) or dissatisfied (6 people). They are defined as turtles and they are distinguishable by asking if the boolean variable satisfied? or dissatisfied? is true.
The dataset is read as follows:
extensions [csv matrix array nw]
globals
[
rowcounter
csv
ii
Sc-headings Bat-headings Pr-headings income-headings average-headings;
Sc-set
Bat-set
Pr-set
prodcount ;num of producer agents
]
turtles-own [
turtle-Sc-list
turtle-Bat-list
turtle-Pr-list
turtle-income-list
turtle-average-list
review-set
satisfied?
dissatisfied?
LapUtl-set
ScPWU
BatPWU
PrPWU
]
to setup
clear-all
file-close-all
set rowcounter 1
proddata
readdataset
reset-ticks
end
breed [ producers producer ]
to go
Reviewrating
end
to intlz
set Sc-set []
set Bat-set []
set Pr-set []
end
Reading the dataset:
to readdataset
file-close-all ; close all open files
file-open "turtle_details.csv"
let headings csv:from-row file-read-line ;header is read
; Splitting headings of the csv file into 5 categories representing screen
; size data, battery charge data, Price data, income data, max age of an owner
set Sc-headings sublist headings 2 7
set Bat-headings sublist headings 7 12
set Pr-headings sublist headings 12 17
set income-headings sublist headings 17 18
set average-headings sublist headings 18 length headings
while [ not file-at-end? ] [
let data csv:from-row file-read-line
create-turtles 1 [
set shape "person"
set size 2.5
ifelse rowcounter < 11
[
set color 125
set satisfied? true
set dissatisfied? false ;
]
;else
[
set color 65
set satisfied? false
set dissatisfied? true ;
]
setxy random-xcor random-ycor
; hide-turtle
set turtle-Sc-list sublist data 2 7
set turtle-Bat-list sublist data 7 12
set turtle-Pr-list sublist data 12 17
set turtle-income-list sublist data 17 18
set turtle-averageage-list sublist data 18 length data
]
set rowcounter rowcounter + 1
]
file-close-all
end
There are 3 producers who have some attribute levels for the screen, battery, price.
Sc Bat Pr
24 10 18000
18 6 22000
30 8 26000
to proddata
file-close-all ; close all open files
if not file-exists? "Prodinitattr.csv" [
user-message "No file 'Prodinitattr.csv' exists!"
stop
]
file-open "Prodinitattr.csv" ; open the file with the producers' initial attributes
let headings csv:from-row file-read-line
while [ not file-at-end? ] [
let data csv:from-row file-read-line
create-producers 1 [
hide-turtle
set producer? true ; this agent is a producer
set satisfied? false ; this agent is not a referrer ; REFERRERS
set dissatisfied? false ; this agent is not a pbuyer
set prodcount prodcount + 1
; set shape "house"
setxy random-xcor random-ycor
]
set Sc-set lput item 0 data Sc-set
set Bat-set lput item 1 data Bat-set
set Pr-set lput item 2 data Pr-set
]
file-close-all
end
The thing that should be extracted from the data set is the evaluation of consumers (reviews). Each consumer has a review-set which is at first an empty set ,[].Then it will keep thee values which corresponds to the three review values for each of the three producers.
to reviewrating
ask turtles [
set review-set []
]
ask turtles [
set ii 0
while [ii < 3 ][
set ScPWU turtle-Sc-rating item ii Sc-set
set BatPWU turtle-Bat-rating item ii Bat-set
set PrPWU turtle-Pr-rating item ii Pr-set
set LapUtl-set lput (ScPWU + BatPWU + PrPWU) LapUtl-set
set ii ii + 1
] ; while
];ask
end
to-report turtle-Sc-rating [Sc]
let pos position Sc Sc-headings
if is-number? position Sc Sc-headings
[
let turt-Sc-rate-value item pos turtle-Sc-list
report turt-Sc-rate-value
]
end
to-report turtle-Bat-rating [Bat]
let pos position Bat Bat-headings
if is-number? position Bat Bat-headings
[
let turt-Bat-rate-value item pos turtle-Bat-list
report turt-Bat-rate-value
]
;***************
end
to-report turtle-Pr-rating [Pr]
let pos position Pr Pr-headings
if is-number? position Pr Pr-headings
[
let turt-Pr-rate-value item pos turtle-Pr-list
report turt-Pr-rate-value
]
end
The problem is I cannot see consumers' LapUtl vector because of the error. I had reported another error previously here, but I changed where the "go" procedure was written, and now the error is marking this line :
let turt-Sc-rate-value **item** pos turtle-Sc-list
How can I resolve tihs?
Thank you,
I suspect you are not correctly reporting the error. I suspect the error is ERROR: ITEM expected this input to be a string or list, but got a number instead. Here is an example of a way to produce this error: item 0 0. If I am right, then you are running the code let turt-Sc-rate-value item pos turtle-Sc-list while turtle-Sc-list has a value of 0. In order to confirm this, replace this code with
ifelse (is-list? turtle-Sc-list)
[let turt-Sc-rate-value item pos turtle-Sc-list]
[error (word "turtle-Sc-list is not a list.")]
Now run your code. If it raises the error "turtle-Sc-list is not a list.", then you are ready to search for how you failed to initialize this variable correctly.

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

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