Netlogo: creating specific links between turtles with information from csv - csv

I'm working on a simulation of social behavior in an emergency situation, using anonymized data from an actual event. Below is the section of code I'm using to create the 'people' turtles.
to read-people-from-file [filename]
let rows bf csv:from-file filename
foreach rows
[[row] ->
create-people 1
[
set size .46
setxy (item 0 row) (item 1 row)
set age (item 2 row)
set gender (item 3 row)
set visited? (item 4 row)
set group-number (item 6 row)
set group-type (item 7 row)
if group-number < 300 and ((person group-number) = (other group-number))) [create-links-with other person]
]
]
end
Everything works except for the group-number based links. I've tried a few different variations of it, with no luck except where I attempted if group-number <300 [create-links-with other people which worked creating links but was functionally useless. What I'm trying to do is set it so that every person who came together (who has the same group number) has a link with other group members. It's specifically less than 300 because numbers 300 and above are for people who came alone or other designations.
I could theoretically create the links by hand after the turtles are created, but that seems both like a waste of time (I have a dataset of over 400) and like something that makes the code significantly less applicable to other scenarios.
How do I make Netlogo create links between members of the same group, based on what's in the CSV?
Actually, the dream answer: how do I make Netlogo create different breeds of links (based on group-type) between members of the same group-number, based on what's in the CSV?
Edit:
In the end, I had to separate creating links from the initial agent setup.
This was what worked:
to soclink ;;groups that came together have links based type of relationship
ask people [if group-number < 300
[
if group-type = 1 [ask other people with [group-number = [group-number] of myself] [create-coworker-with myself]]
if group-type = 2 [ask other people with [group-number = [group-number] of myself] [create-friend-with myself]]
if group-type = 3 [ask other people with [group-number = [group-number] of myself] [create-partner-with myself]]
if group-type = 4 [ask other people with [group-number = [group-number] of myself] [create-family-with myself]]
if group-type = 5 [ask other people with [group-number = [group-number] of myself] [create-multiple-with myself]]
show count links]]
end

For the first part, maybe this would work- once your people are created, with their numbers assigned to group-number:
to group-link
ask people [
let my-group other people with [ group-number = [ group-number] of myself ]
create-links-with my-group
]
end
For the dream answer- it might depend on how many breeds of links you need. As far as I know, link breeds must be predefined- you could not programatically generate links as needed for different groups (although I am no expert- there may be a way). If you have your link breeds already defined, for example:
undirected-link-breed [ link-as link-a ]
undirected-link-breed [ link-bs link-b ]
Pretend now that your group-type is either "a" or "b", and you can do something like
to specific-link-breeds
ask people [
let my-group other people with [ group-number = [ group-number] of myself ]
if group-type = "a" [
create-link-as-with my-group
]
if group-type = "b" [
create-link-bs-with my-group
]
]
ask link-as [
set color red
]
ask link-bs [
set color blue
]
end
Edit: Changed turtles to people as it should be- thanks Mattsap.

Related

python color entire pandas dataframe rows based on column values

I have a script that downloads a .csv and does some manipulation and then emails panda dataframes in a nice html format by using df.to_html.
I would like to enhance these tables by highlighting, or coloring, different rows based on their text value in a specific column.
I tried using pandas styler which appears to work however I can not convert that to html using to_html. I get a "AttributeError: 'str' object has no attribute 'to_html"
Is there a another way to do this?
As an example lets say my DF looks like the following and I want to highlight all rows for each manufacturer. i.e Use three different colors for Ford, Chevy, and Dodge:
Year Color Manufacturer
2011 Red Ford
2010 Yellow Ford
2000 Blue Chevy
1983 Orange Dodge
I noticed I can pass formatters into to_html but it appears that it cannot do what I am trying to accomplish by coloring? I would like to be able to do something like:
def colorred():
return ['background-color: red']
def color_row(value):
if value is "Ford":
result = colorred()
return result
df1.to_html("test.html", escape=False, formatters={"Manufacturer": color_row})
Surprised this has never been answered as looking back at it I do not believe this is even possible with to_html formatters. After revisiting this several times I have found a very nice solution I am happy with. I have not seen anything close to this online so I hope this helps someone else.
d = {'Year' : [2011, 2010, 2000, 1983],
'Color' : ['Red', 'Yellow', 'Blue', 'Orange'],
'Manufacturer' : ['Ford', 'Ford', 'Chevy', 'Dodge']}
df =pd.DataFrame(d)
print (df)
def color_rows(s):
df = s.copy()
#Key:Value dictionary of Column Name:Color
color_map = {}
#Unqiue Column values
manufacturers = df['Manufacturer'].unique()
colors_to_use = ['background-color: #ABB2B9', 'background-color: #EDBB99', 'background-color: #ABEBC6',
'background-color: #AED6F1']
#Loop over our column values and associate one color to each
for manufacturer in manufacturers:
color_map[manufacturer] = colors_to_use[0]
colors_to_use.pop(0)
for index, row in df.iterrows():
if row['Manufacturer'] in manufacturers:
manufacturer = row['Manufacturer']
#Get the color to use based on this rows Manufacturers value
my_color = color_map[manufacturer]
#Update the row using loc
df.loc[index,:] = my_color
else:
df.loc[index,:] = 'background-color: '
return df
df.style.apply(color_rows, axis=None)
Output:
Pandas row coloring
Since I do not have the cred to embed images here is how I email it. I convert it to html with the following.
styled = df.style.apply(color_rows, axis=None).set_table_styles(
[{'selector': '.row_heading',
'props': [('display', 'none')]},
{'selector': '.blank.level0',
'props': [('display', 'none')]}])
html = (styled.render())

SSRS If Column X value = "X" then display corresponding column Y value

I am using SSRS 2008 and have a dataset that looks like this: (simplified)
RESULTGUID Question ANSWER
1234 How Old 15
1234 How New 13
1234 How Big 700
1234 How Small 100
etc etc
I'm looking for an IIF statement like:
=iif(fields!Question.value = "How Old",Fields!Answer.value,"N/A")
However, this doesn't work apart from the first row, so if first(fields! etc then works fine, but not for the rows lower down the data-set.
Can anyone offer a solution to this please ?
If you are doing it all in SSRS it could be one big nested Iif statement:
=Iif(fields!Question.value = "How Old",Fields!Answer.value,Iif(fields!Question.value = "How New",Fields!Answer.value,
Iif(fields!Question.value = "How Big",Fields!Answer.value,Iif(fields!Question.value = "How Small",Fields!Answer.value,"N/A"))))
or you could do a SWITCH statement:
=SWITCH(
fields!Question.value = "How Old", Fields!Answer.value
fields!Question.value = "How New", Fields!Answer.value
fields!Question.value = "How Big", Fields!Answer.value
fields!Question.value = "How Small", Fields!Answer.value
)

list() contains one element but it has matrix of string inside, how do I convert this element into matrix?

After converting JSON data into a list using jsonlite, i end up with one of the list looking like following
In this case, 10th element contain a list of 9 columns (always fixed) and 2 rows (varies everytime).
mat <- lset$data$comments$data[10]
mat
[[1]]
id can_remove created_time from.id
1 10152663742099258_10152663749369258 TRUE 2014-07-01T11:10:29+0000 10203711779968366
2 10152663742099258_10152663842204258 TRUE 2014-07-01T12:15:57+0000 706804257
3 10152663742099258_10152663929639258 TRUE 2014-07-01T13:25:28+0000 10152738599744416
4 10152663742099258_10152663976344258 TRUE 2014-07-01T13:59:33+0000 706804257
from.name like_count
1 Aileen Yeow 1
2 Tejas Damania 0
3 Sandeep Kulkarni 1
4 Tejas Damania 0
message
1 Lame statement
2 Don't forget, people like you only because they don't know you! <ed><U+00A0><U+00BD><ed><U+00B8><U+00A1>
3 ...for a second I thought it's Accenture Singapore office with some new theme similar to its brand!
4 This is shanghai and nothing to do with firm I work for <ed><U+00A0><U+00BD><ed><U+00B8><U+008E>
user_likes
1 FALSE
2 FALSE
3 TRUE
4 FALSE
Whole mat shows us as a list of [1]
As you can see, it contains list (within a list?). When i print mat then it shows a structure as seen above.
typeof(mat)
[1] "list"
substring(mat,1,100)
[1] "list(id = c(\"10152663742099258_10152663749369258\", \"10152663742099258_10152663842204258\", \"101526637"
I cant access specific elements (say message) from this. Nor I am able to convert this into a matrix of strings so I can access the elements in structured way.
I changed the fromJSON call parameter to by setting simplifyVector = FALSE (which is default set to true)
lset <- fromJSON(jsonobj, simplifyVector = F, flatten=TRUE, unicode = TRUE)
this changes the way mat is formed, code maintain nesting all the way down to each leaf element. I can keep going deeper using $ and find the string value only at leaf element!
lset$data[[x]]$comments$data[[y]]$from$name
That works for now! thanks for all the help

changing bar fill colours in ssrs chart

SO Post
Current I've got 5 bars in my RS chart - in the future there might be 7 bars or 17 bars or 27 bars!
With a couple of bars I can have an expression like this:
=iif(Fields!Market.Value = "Spain"
,"Gold"
,iif (Fields!Market.Value = "Denmark"
, "Gray"
, iif(Fields!Market.Value = "Italy"
, "Blue"
, "Purple"
)
)
)
If I can't predict how many countries will be included + I'd rather not have to hard code in "Green", "Red" etc how do I change the expression?
I've tried this but it is erroring:
=Switch(Mod(Fields!Rank.Value/CDbl(2))=CDbl(0), "Gold",
Mod(Fields!Rank.Value/CDbl(3))=CDbl(0), "Gray",
Mod(Fields!Rank.Value/CDbl(2))>CDbl(0) "Blue")
Above is the totally incorrect syntax: This works:
=Switch(CDbl(Fields!Rank.Value Mod 2)=CDbl(0), "Gold",
CDbl(Fields!Rank.Value Mod 3)=CDbl(0), "Gray",
CDbl(Fields!Rank.Value Mod 2)>CDbl(0), "Blue")
Ok - the above runs (not sure how!) but the below is based on help from Dominic Goulet and is really easy to follow and nice and expandable to more colours; this is the solution for 5 colours:
=Switch(CDbl(Fields!Rank.Value Mod 5)=CDbl(0), "Gold",
CDbl(Fields!Rank.Value Mod 5)=CDbl(1), "Gray",
CDbl(Fields!Rank.Value Mod 5)=CDbl(2), "Green",
CDbl(Fields!Rank.Value Mod 5)=CDbl(3), "Red",
CDbl(Fields!Rank.Value Mod 5)=CDbl(4), "Pink")
First of all, instead of using many "IIF"s, you should use "Switch", it's leaner that way.
Switch(Fields!Market.Value = "Spain", "Gold",
Fields!Market.Value = "Denmark", "Gray",
Fields!Market.Value = "Italy", "Blue")
Now if you want a color per coutry, you should defenitely store that in your database and pull it out when you need it. That way, a country will always have the same color on every report you have.
It would be better to create a function. For that, go to Report Properties, choose code and type this example :
Public Function Color(ByVal Index as Integer) as String
Select Case Index
Case = 1
return "#a6cee3"
Case = 2
return "#1f78b4"
Case = 3
return "#b2df8a"
Case = 4
return "#33a02c"
Case = 5
return "#fb9a99"
Case = 6
return "#e31a1c"
Case = 7
return "#fdbf6f"
Case = 8
return "#ff7f00"
Case = 9
return "#cab2d6"
Case = 10
return "#6a3d9a"
End Select
End Function
On the Fill option from "Series Properties->Pick color-> Color choose fx
put this code
=Code.Color(rownumber(nothing))
Each bar will have a color.
For the HEX colors I took from the website : http://colorbrewer2.org/#type=qualitative&scheme=Paired&n=10
It shows the best colors that match with each other, so you don't need to think of that. And you can add as many colors as you want

NetLogo two agentsets operations

I have two agentsets. Are there functions for finding:
An agentset of agents that are present in both (intersection)
An agentset of agents that are present in one and not the other
I'm finding it very difficult to implement this by hand, especially when it's needed inside of a triple ask
Ideal use would be similar to with syntax:
let cross set1 and-in set2
let uniq set1 with [color = red] not-in set2
Simple things like "Is agent A in the agentset X?" are problematic
For the first one you use the turtle-set primitive. For the second one you need the member? primitive, which also works on agentsets. As such:
to setup
ca
create-turtles 10 [set color red]
create-turtles 10 [set color blue]
let red-ones turtles with [color = red]
let blue-ones turtles with [color = blue]
;join 2 agent sets
let joinset (turtle-set red-ones blue-ones)
show joinset
let even-ones (turtles with [who mod 2 = 0])
;subtract even-ones from red-ones
let subtractset red-ones with [not member? self even-ones]
show subtractset
end