Subset Data based on User Login - security

I have a shiny application with a login interface. I need to subset data based on the user login. Within the data, I have a column which marks the data whether the data belongs to them. The column is called "memberstate" and essentially contains the login username. The code I am tyring to use is borrowed from R Studio and is as follows:
user <- reactive({
session$user
})
### Code to manage row level security
isManager <- reactive({
if (user() == "manager"){
return(TRUE)
} else{
return(FALSE)
}
})
# Based on the logged in user, pull out only the data this user
# should be able to see.
data <- read.csv("data/DHA&PPTabletsClean.csv")
myData <- reactive({
if (isManager()){
# If a manager, show everything.
return(data)
} else{
# If a Member State, only show their own data.
return(data[data$memberstate == user(),])
}
})
I then try and use MyData for plotting graphs. I am getting the following error message
"Error in Mydata: could not find function "Mydata" " I am a newbie to R Shiny. Kindly assist.
Part of the data is as follows:
dput(data)
structure(list(Brand = c("Malaril", "Malaril", "Malaril", "Malaril",
"Malaril", "Malaril", "Malaril", "Malaril", "Malaril", "Malaril",
"Malaril", "Malaril", "Malaril", "Malaril", "Malaril", "Malaril",
"Malaril", "Malaril", "Malaril", "Malaril", "Malaril", "Malaril",
"Malaril", "Malaril", "Malaril", "Malaril", "Malaril", "Malaril",
"Malaril", "Malaril", "Malaril", "Malaril", "Malaril", "Malaril"
), ActiveIngredient = c("Dihydroartemisinin", "Dihydroartemisinin",
"Dihydroartemisinin", "Dihydroartemisinin", "Dihydroartemisinin",
"Dihydroartemisinin", "Piperaquine Phosphate", "Piperaquine Phosphate",
"Piperaquine Phosphate", "Piperaquine Phosphate", "Piperaquine Phosphate",
"Piperaquine Phosphate", "Dihydroartemisinin", "Dihydroartemisinin",
"Dihydroartemisinin", "Dihydroartemisinin", "Dihydroartemisinin",
"Dihydroartemisinin", "Piperaquine Phosphate", "Piperaquine Phosphate",
"Piperaquine Phosphate", "Piperaquine Phosphate", "Piperaquine Phosphate",
"Piperaquine Phosphate", "Dihydroartemisinin", "Dihydroartemisinin",
"Dihydroartemisinin", "Dihydroartemisinin", "Dihydroartemisinin",
"Dihydroartemisinin", "Piperaquine Phosphate", "Piperaquine Phosphate",
"Piperaquine Phosphate", "Piperaquine Phosphate"), Assay = c(94.9,
94.9, 94.9, 94.9, 94.9, 94.9, 101.6, 101.6, 101.6, 101.6, 101.6,
101.6, 95, 95, 95, 95, 95, 95, 100.2, 100.2, 100.2, 100.2, 100.2,
100.2, 96.4, 96.4, 96.4, 96.4, 96.4, 96.4, 100.6, 100.6, 100.6,
100.6), Assayperc = c(0.949, 0.949, 0.949, 0.949, 0.949, 0.949,
1.016, 1.016, 1.016, 1.016, 1.016, 1.016, 0.95, 0.95, 0.95, 0.95,
0.95, 0.95, 1.002, 1.002, 1.002, 1.002, 1.002, 1.002, 0.965,
0.965, 0.965, 0.965, 0.965, 0.965, 1.006, 1.006, 1.006, 1.006
), AssayLL = c(90L, 90L, 90L, 90L, 90L, 90L, 93L, 93L, 93L, 93L,
93L, 93L, 90L, 90L, 90L, 90L, 90L, 90L, 93L, 93L, 93L, 93L, 93L,
93L, 90L, 90L, 90L, 90L, 90L, 90L, 93L, 93L, 93L, 93L), AssaypercLL = c(0.9,
0.9, 0.9, 0.9, 0.9, 0.9, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93,
0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.93, 0.93, 0.93, 0.93, 0.93, 0.93,
0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.93, 0.93, 0.93, 0.93), AssayUL = c(110L,
110L, 110L, 110L, 110L, 110L, 107L, 107L, 107L, 107L, 107L, 107L,
110L, 110L, 110L, 110L, 110L, 110L, 107L, 107L, 107L, 107L, 107L,
107L, 110L, 110L, 110L, 110L, 110L, 110L, 107L, 107L, 107L, 107L
), AssaypercUL = c(1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.07, 1.07,
1.07, 1.07, 1.07, 1.07, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.07, 1.07,
1.07, 1.07, 1.07, 1.07, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.07, 1.07,
1.07, 1.07), DateManufacture = c("1/10/2017", "1/10/2017", "1/10/2017",
"1/10/2017", "1/10/2017", "1/10/2017", "1/10/2017", "1/10/2017",
"1/10/2017", "1/10/2017", "1/10/2017", "1/10/2017", "1/6/2018",
"1/6/2018", "1/6/2018", "1/6/2018", "1/6/2018", "1/6/2018", "1/6/2018",
"1/6/2018", "1/6/2018", "1/6/2018", "1/6/2018", "1/6/2018", "1/8/2018",
"1/8/2018", "1/8/2018", "1/8/2018", "1/8/2018", "1/8/2018", "1/8/2018",
"1/8/2018", "1/8/2018", "1/8/2018"), ExpiryDate = c("1/9/2019",
"1/9/2019", "1/9/2019", "1/9/2019", "1/9/2019", "1/9/2019", "1/9/2019",
"1/9/2019", "1/9/2019", "1/9/2019", "1/9/2019", "1/9/2019", "1/5/2020",
"1/5/2020", "1/5/2020", "1/5/2020", "1/5/2020", "1/5/2020", "1/5/2020",
"1/5/2020", "1/5/2020", "1/5/2020", "1/5/2020", "1/5/2020", "1/7/2020",
"1/7/2020", "1/7/2020", "1/7/2020", "1/7/2020", "1/7/2020", "1/7/2020",
"1/7/2020", "1/7/2020", "1/7/2020"), ShelfLifeYrs = c(2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), ShelfLifeDysRecpt = c(-547L, -547L, -547L, -547L, -547L, -547L,
-547L, -547L, -547L, -547L, -547L, -547L, -304L, -304L, -304L,
-304L, -304L, -304L, -304L, -304L, -304L, -304L, -304L, -304L,
-243L, -243L, -243L, -243L, -243L, -243L, -243L, -243L, -243L,
-243L), DateReceiptSample = c("1/3/2021", "1/3/2021", "1/3/2021",
"1/3/2021", "1/3/2021", "1/3/2021", "1/3/2021", "1/3/2021", "1/3/2021",
"1/3/2021", "1/3/2021", "1/3/2021", "1/3/2021", "1/3/2021", "1/3/2021",
"1/3/2021", "1/3/2021", "1/3/2021", "1/3/2021", "1/3/2021", "1/3/2021",
"1/3/2021", "1/3/2021", "1/3/2021", "1/3/2021", "1/3/2021", "1/3/2021",
"1/3/2021", "1/3/2021", "1/3/2021", "1/3/2021", "1/3/2021", "1/3/2021",
"1/3/2021"), COADateIssue = c("27/5/2021", "27/5/2021", "27/5/2021",
"27/5/2021", "27/5/2021", "27/5/2021", "27/5/2021", "27/5/2021",
"27/5/2021", "27/5/2021", "27/5/2021", "27/5/2021", "27/5/2021",
"27/5/2021", "27/5/2021", "27/5/2021", "27/5/2021", "27/5/2021",
"27/5/2021", "27/5/2021", "27/5/2021", "27/5/2021", "27/5/2021",
"27/5/2021", "27/5/2021", "27/5/2021", "27/5/2021", "27/5/2021",
"27/5/2021", "27/5/2021", "27/5/2021", "27/5/2021", "27/5/2021",
"27/5/2021"), TestingOutcome = c("Pass", "Pass", "Pass", "Pass",
"Pass", "Pass", "Pass", "Pass", "Pass", "Pass", "Pass", "Pass",
"Fail", "Fail", "Fail", "Fail", "Fail", "Fail", "Fail", "Fail",
"Fail", "Fail", "Fail", "Fail", "Pass", "Pass", "Pass", "Pass",
"Pass", "Pass", "Pass", "Pass", "Pass", "Pass"), FailureReason = c("",
"", "", "", "", "", "", "", "", "", "", "", "Dihydroartemisinin Dissolution",
"Dihydroartemisinin Dissolution", "Dihydroartemisinin Dissolution",
"Dihydroartemisinin Dissolution", "Dihydroartemisinin Dissolution",
"Dihydroartemisinin Dissolution", "Dihydroartemisinin Dissolution",
"Dihydroartemisinin Dissolution", "Dihydroartemisinin Dissolution",
"Dihydroartemisinin Dissolution", "Dihydroartemisinin Dissolution",
"Dihydroartemisinin Dissolution", "", "", "", "", "", "", "",
"", "", ""), Dissolution = c(77L, 81L, 84L, 86L, 82L, 81L, 100L,
96L, 98L, 101L, 97L, 102L, 62L, 59L, 62L, 66L, 65L, 61L, 99L,
95L, 97L, 103L, 99L, 102L, 97L, 80L, 81L, 86L, 80L, 80L, 103L,
101L, 101L, 101L), Dissolutionperc = c(0.77, 0.81, 0.84, 0.86,
0.82, 0.81, 1, 0.96, 0.98, 1.01, 0.97, 1.02, 0.62, 0.59, 0.62,
0.66, 0.65, 0.61, 0.99, 0.95, 0.97, 1.03, 0.99, 1.02, 0.97, 0.8,
0.81, 0.86, 0.8, 0.8, 1.03, 1.01, 1.01, 1.01), DissolLL = c(70L,
70L, 70L, 70L, 70L, 70L, 80L, 80L, 80L, 80L, 80L, 80L, 70L, 70L,
70L, 70L, 70L, 70L, 80L, 80L, 80L, 80L, 80L, 80L, 70L, 70L, 70L,
70L, 70L, 70L, 80L, 80L, 80L, 80L), DissolutionpercLL = c(0.7,
0.7, 0.7, 0.7, 0.7, 0.7, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.7, 0.7,
0.7, 0.7, 0.7, 0.7, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0.7, 0.7, 0.7,
0.7, 0.7, 0.7, 0.8, 0.8, 0.8, 0.8), Mass = c(NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), pH = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), Dosageform = c("Tablet", "Tablet", "Tablet", "Tablet", "Tablet",
"Tablet", "Tablet", "Tablet", "Tablet", "Tablet", "Tablet", "Tablet",
"Tablet", "Tablet", "Tablet", "Tablet", "Tablet", "Tablet", "Tablet",
"Tablet", "Tablet", "Tablet", "Tablet", "Tablet", "Tablet", "Tablet",
"Tablet", "Tablet", "Tablet", "Tablet", "Tablet", "Tablet", "Tablet",
"Tablet"), Therapeuticclass = c("Antimalarial", "Antimalarial",
"Antimalarial", "Antimalarial", "Antimalarial", "Antimalarial",
"Antimalarial", "Antimalarial", "Antimalarial", "Antimalarial",
"Antimalarial", "Antimalarial", "Antimalarial", "Antimalarial",
"Antimalarial", "Antimalarial", "Antimalarial", "Antimalarial",
"Antimalarial", "Antimalarial", "Antimalarial", "Antimalarial",
"Antimalarial", "Antimalarial", "Antimalarial", "Antimalarial",
"Antimalarial", "Antimalarial", "Antimalarial", "Antimalarial",
"Antimalarial", "Antimalarial", "Antimalarial", "Antimalarial"
), memberstate = c("", "", "", "", "", "ruvimbo", "ruvimbo",
"ruvimbo", "ruvimbo", "ruvimbo", "ruvimbo", "ruvimbo", "ruvimbo",
"ruvimbo", "ruvimbo", "ruvimbo", "ruvimbo", "ruvimbo", "ruvimbo",
"ruvimbo", "ruvimbo", "ruvimbo", "", "", "", "", "", "", "",
"", "", "", "", "")), class = "data.frame", row.names = c(NA,
-34L))
Regards
Chris

Related

Python Tkinter - Use of multiple comboxes to update labels

As a newbie to Python I am trying to create a football game whereby you as a manager can select players (from comboboxes).
The intent is to have 1 x Goalkeeper, 5 x def, 5 x midfield and 5 x attacker in a 3 x 5 grid formation (plus goalkeeper) - you can select players from your team from the comboboxes, the players selected will have their stats shown in the labels underneath. You can therefore select a number of formations (ie 5-3-2, 2-5-3...etc).
My code works but is really cumbersome - I have copied some of the code below.
I did try using StringVar but due to the number of variables required across all the labels it was very confusing. I would appreciate any help in trying to improve the coding. Can each label be updated when a player is selected.
from tkinter import *
from tkinter import ttk
window = Tk()
window.title("Fantasy Football")
window.geometry("1050x800")
player_dict = [
{'team': 'Madrid', 'position': 'Def', 'name': 'Steve', 'tackling': "9", 'passing': "8", 'shooting': "2", 'heading': "7"},
{'team': 'Madrid', 'position': 'Def', 'name': 'Tony', 'tackling': 10, 'passing': 4, 'shooting': 6, 'heading': 2},
{'team': 'Madrid', 'position': 'Def', 'name': 'Grant', 'tackling': 9, 'passing': 9, 'shooting': 4, 'heading': 6},
{'team': 'Madrid', 'position': 'Def', 'name': 'Sam', 'tackling': 10, 'passing': 8, 'shooting': 5, 'heading': 2},
{'team': 'Madrid', 'position': 'Def', 'name': 'Bob', 'tackling': 4, 'passing': 3, 'shooting': 3, 'heading': 8},
{'team': 'Madrid', 'position': 'Def', 'name': 'Dave', 'tackling': 6, 'passing': 6, 'shooting': 12, 'heading': 2}]
#Create list of players for comboboxes
lb_list = [player['name'] for player in player_dict]
position = ""
tackling = ""
passing = ""
shooting = ""
heading = ""
player_name = ""
def callback(eventObject):
player_name = eventObject.widget.get()
if player_name != -1:
data2 = [d['position'] for d in player_dict if d['name'] == player_name]
data3 = [d['tackling'] for d in player_dict if d['name'] == player_name]
data4 = [d['passing'] for d in player_dict if d['name'] == player_name]
data5 = [d['shooting'] for d in player_dict if d['name'] == player_name]
data6 = [d['heading'] for d in player_dict if d['name'] == player_name]
Label(window, anchor="w", borderwidth=1, relief="solid", width=15,
text=f"position : {data2[0]}\ntackling: {data3[0]}\npassing: {data4[0]}\n shooting: {data5[0]}\n heading: {data6[0]}\n",
bg="light grey").place(x=30, y=350)
# player1
Label(window, anchor="w", borderwidth=1, relief="solid", width=15,
text=f"position :\ntackling:\npassing:\n shooting:\n heading:\n",
bg="light grey").place(x=30, y=350)
comboboxaa = ttk.Combobox(window, width=15, values=lb_list)
comboboxaa.place(x=30, y=328)
comboboxaa.bind('<<ComboboxSelected>>', callback)
# player 2
Label(window, anchor="w", borderwidth=1, relief="solid", width=15,
text=f"position :\ntackling:\npassing:\n shooting:\n heading:\n",
bg="light grey").place(x=140, y=350)
comboboxa = ttk.Combobox(window, width=15, values=lb_list)
comboboxa.place(x=140, y=328)
# player 3
Label(window, anchor="w", borderwidth=1, relief="solid", width=15,
text=f"position :\ntackling:\npassing:\n shooting:\n heading:\n",
bg="light grey").place(x=250, y=350)
comboboxb = ttk.Combobox(window, width=15, values=lb_list)
comboboxb.place(x=250, y=328)
mainloop()

Difficulties with subsetting clustered family data

I am trying to subset the following dataset so that both sons and fathers with resting heart rate values are included in a new data frame. When I use the is.na function, since fathers and sons are on their own individual rows, fathers without a value for sons' resting heart rate are deleted and vice versa.
I have tried the following code but I am not having any luck:
for (i in 1:nrow(datadiss)){
if (datadiss$Fatheridorsonid == "sonid" & isFALSE(is.na(datadiss$sonrhr))){
data2<-datadiss[which(datadiss$sonrhr>0),]
}else if (datadiss$Fatheridorsonid == "Fatherid" & isFALSE(is.na(datadiss$dadrhr))){
data2<-datadiss[which(datadiss$dadrhr>0),]
}
}
An example of my dataset can be found here:
structure(list(fid = c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 6, 6,
6, 7, 7, 8, 8, 9, 9), sonid = c(15576123, NA, 3100866, NA, NA,
13206733, NA, 3098080, 3938697, 7089160, NA, 10580951, 13465844,
NA, 10801693, NA, 10797768, NA, NA, 9670190), sonrhr = c(75,
NA, 89, NA, NA, 100, NA, 65, 62, 56, NA, 78, 85, NA, 67, NA,
66, NA, NA, 87), sonbmi = c(19.66, NA, 19.59, NA, NA, 21.54,
NA, 22.84, 22.88, 29.09, NA, 21.53, 22.03, NA, 23.41, NA, 22.02,
NA, NA, 22.44), sonphysenerg = c(329, NA, 234, NA, NA, NA, NA,
295, 344, 299, NA, 313, 351, NA, NA, NA, NA, NA, NA, NA), violent = c(0,
NA, 0, NA, NA, 0, NA, 0, 0, 1, NA, 1, 0, NA, 0, NA, 0, NA, NA,
0), nonviolent = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), Fatherid = c(NA, 15576243, NA, 3100889, 123456, NA,
3938697, NA, NA, NA, 10580442, NA, NA, 10804692, NA, 10797798,
NA, 12797798, 9670176, NA), dadrhr = c(NA, 76, NA, 87, 49, NA,
67, NA, NA, NA, 57, NA, NA, 78, NA, 58, NA, 100, 89, NA), sonorfather = structure(c(1L,
2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L,
2L, 2L, 1L), .Label = c("Son", "Father"), class = "factor")), class = "data.frame", row.names = c(NA,
-20L), codepage = 65001L)

Subset inside lapply for Pearson correlation

I have tried iterate to explore pearson correlation between 2 variables with 7 more variables.
My df
df<-structure(list(d_peso1_v01 = structure(c(-8, -0.5, -13, -0.7,
-10.2, -9, -4.3, -1.6, 1.8, -11.3, -10.3, -4.6, 1.2, -2.8, -9.2
), format.spss = "F8.2", display_width = 13L), d_cintura1_v01 = structure(c(-6.5,
-3.5, -10, -2, -7, -3, -3, -4, -4.5, -9.5, -15.5, -3, 1, -4,
-12), format.spss = "F8.2", display_width = 16L), d_huglucagon_v01 = structure(c(-106.06,
NA, -75.38, 27.5, -325.38, -26.12, -104.26, 28.66, NA, -11.12,
-60.05, -76.38, -36.21, NA, -270.02), format.spss = "F8.2", display_width = 18L),
d_huinsulin_v01 = structure(c(-26.29, NA, -143.44, -410.55,
84.51, -121.56, -52.36, -151.83, NA, -42, -43.69, -82.96,
-51.27, NA, -163.12), format.spss = "F8.2", display_width = 17L),
d_huvisfatin_v01 = structure(c(-541.93, NA, -750.38, -611.9,
0, 139.61, -343.58, -149.2, NA, -91.54, -212.47, -844.05,
-353.86, NA, -1749.96), format.spss = "F8.2", display_width = 18L),
d_hupai1_v01 = structure(c(-785.4, NA, 115.96, -867.31, -10.84,
-1634, -331.21, 396.05, NA, -424.5, -143.09, 429.39, 799.11,
NA, -633.44), format.spss = "F8.2", display_width = 13L),
d_hucpeptide_v01 = structure(c(-189.33, NA, -612.6, -1250.86,
110.03, -614.69, -119.31, -305.55, NA, -104.55, -38.74, -411.38,
-65.48, NA, -143.75), format.spss = "F8.2", display_width = 18L),
d_huleptin_v01 = structure(c(-3145.34, NA, -5038.03, -2069.79,
-357.79, -1004.4, -1253.38, 365.69, NA, -2102.93, -1454.6,
-3380.95, -760.69, NA, -6078.46), format.spss = "F8.2", display_width = 16L),
d_hughrelin_v01 = structure(c(-290.46, NA, -898.76, -726.4,
-217.49, 41.13, 93.89, 436.93, NA, 12.85, -221.54, -134,
-200.15, NA, 261.3), format.spss = "F8.2", display_width = 18L),
d_hba1c_v01 = structure(c(0.02, NA, -0.26, -0.17, -1.05,
-0.41, -0.47, -0.21, NA, -0.14, -0.14, -0.43, 0.61, NA, -1.33
), format.spss = "F8.2", display_width = 13L), grupo_int_v00 = structure(c(2L,
1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L), .Label = c("A",
"B"), label = "Grupo de intervención", class = "factor")), class = "data.frame", row.names = c(NA,
-15L))
I have performed all the steps to do it for the whole database, but I want to subset according to grupo_int_v00, which is a factor differentiating 2 treatments (A or B)
my_data <- dat[, c("d_peso1_v01", "d_cintura1_v01", "d_huglucagon_v01", "d_huinsulin_v01", "d_huvisfatin_v01", "d_hupai1_v01", "d_hucpeptide_v01", "d_huleptin_v01", "d_hughrelin_v01", "d_hba1c_v01", "grupo_int_v00")]
peso_pearson_01 <- lapply(my_data, function(x) {cor.test(x, my_data$d_peso1_v01, method = "pearson")})
cintura_pearson_01 <- lapply(my_data, function(x) {cor.test(x, my_data$d_cintura1_v01, method = "pearson")})
cintura_peso_01 <- do.call(c, list(peso_pearson_01, cintura_pearson_01))
max <- max(sapply(cintura_peso_01, length))
cintura_peso_01 <- do.call(rbind, lapply(cintura_peso_01, function(z) c(z, rep(NA, max-length(z)))))
How can I insert grupo_int_v00 in the syntax?
peso_pearson_01 <- lapply(my_data, function(x) {subset(dat$grupo_int_v00 == "A"), cor.test(x, my_data$d_peso1_v01, method = "pearson")})
Error: inesperado ',' in "lapply(my_data, function(x) {subset(dat$grupo_int_v00 == "A"),"
Thank you!

Fitting an ellipse to points: why an ellipse fits for some datasets while for others there is an offset of 90 deg?

I have trouble fitting an ellipse to a set of 2D data points. The code seems to work for some datasets, while for others the ellipse is rotated 90 degrees but always has the right shape and center. I am trying to make the SAME code work for all the datasets. Here is the code (from https://github.com/ndvanforeest/fit_ellipse):
I have already looked at Fitting an ellipse to a set of 2-D points but I still have the problem.
import matplotlib.pyplot as plt
import numpy as np
import math
from numpy.linalg import eig, inv
from matplotlib.patches import Ellipse
def fitEllipse(x,y):
x = x[:,np.newaxis]
y = y[:,np.newaxis]
D = np.hstack((x*x, x*y, y*y, x, y, np.ones_like(x)))
S = np.dot(D.T,D)
C = np.zeros([6,6])
C[0,2] = C[2,0] = 2; C[1,1] = -1
E, V = eig(np.dot(inv(S), C))
n = np.argmax(np.abs(E))
a = V[:,n]
return a
def ellipse_center(a):
b,c,d,f,g,a = a[1]/2, a[2], a[3]/2, a[4]/2, a[5], a[0]
num = b*b-a*c
x0=(c*d-b*f)/num
y0=(a*f-b*d)/num
return np.array([x0,y0])
def ellipse_angle_of_rotation( a ):
b,c,d,f,g,a = a[1]/2, a[2], a[3]/2, a[4]/2, a[5], a[0]
return 0.5*np.arctan(2*b/(a-c))
def ellipse_axis_length( a ):
b,c,d,f,g,a = a[1]/2, a[2], a[3]/2, a[4]/2, a[5], a[0]
up = 2*(a*f*f+c*d*d+g*b*b-2*b*d*f-a*c*g)
down1=(b*b-a*c)*( (c-a)*np.sqrt(1+4*b*b/((a-c)*(a-c)))-(c+a))
down2=(b*b-a*c)*( (a-c)*np.sqrt(1+4*b*b/((a-c)*(a-c)))-(c+a))
res1=np.sqrt(up/down1)
res2=np.sqrt(up/down2)
#Assert res1 is the major axis and res2 is the minor axis
if(res2 > res1):
tmp = res2
res2 = res1
res1 = tmp
return np.array([res1, res2])
def ellipse_angle_of_rotation2( a ):
b,c,d,f,g,a = a[1]/2, a[2], a[3]/2, a[4]/2, a[5], a[0]
if a > c:
return np.arctan(2*b/(a-c))/2
else:
return np.pi/2 + np.arctan(2*b/(a-c))/2
Here is an example of a dataset that does NOT work as ellipse is rotated 90 deg. You can see this in this plot https://i.stack.imgur.com/khpel.png:
# --------------------------------------------------------------------------
x = np.array([40.5, 40.5, 40.5, 40.5, 40.5, 40.5, 40.5, 40.5, 40.5, 40.5, 40.5, 40.5, 41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 41.5, 42.5, 42.5, 42.5, 42.5, 42.5, 42.5, 42.5, 42.5, 42.5, 42.5, 43.5, 43.5, 43.5, 43.5, 43.5, 43.5, 44.5, 44.5, 44.5, 44.5, 44.5, 44.5, 44.5, 44.5, 45.5, 45.5, 45.5, 45.5, 45.5, 46.5, 46.5, 46.5, 46.5, 46.5, 46.5, 47.5, 47.5, 47.5, 47.5, 47.5, 47.5, 47.5, 48.5, 48.5, 48.5, 48.5, 49.5, 49.5, 49.5, 50.5, 50.5, 50.5, 50.5, 51.5, 51.5, 51.5, 51.5, 52.5, 52.5, 52.5, 52.5, 52.5, 53.5, 53.5, 53.5, 53.5, 53.5, 53.5, 54.5, 54.5, 54.5, 54.5, 54.5, 54.5, 55.5, 55.5, 55.5, 55.5, 55.5, 55.5, 55.5, 55.5, 56.5, 56.5, 56.5, 56.5, 56.5, 57.5, 57.5, 57.5, 57.5, 58.5, 58.5, 58.5, 58.5, 58.5, 58.5, 59.5, 59.5, 59.5, 59.5, 59.5, 60.5, 60.5, 60.5, 60.5, 60.5, 60.5, 60.5, 60.5, 60.5, 60.5, 60.5, 60.5, 60.5, 60.5, 60.5, 61.5])
y = np.array([42.5, 43.5, 46.5, 48.5, 49.5, 50.5, 51.5, 52.5, 54.5, 56.5, 57.5, 60.5, 41.5, 44.5, 45.5, 47.5, 53.5, 55.5, 57.5, 59.5, 60.5, 33.5, 37.5, 38.5, 39.5, 40.5, 58.5, 60.5, 62.5, 63.5, 64.5, 32.5, 34.5, 35.5, 36.5, 61.5, 64.5, 28.5, 29.5, 31.5, 65.5, 66.5, 67.5, 68.5, 71.5, 27.5, 30.5, 69.5, 70.5, 72.5, 24.5, 25.5, 26.5, 73.5, 74.5, 75.5, 20.5, 21.5, 22.5, 23.5, 76.5, 78.5, 79.5, 18.5, 19.5, 77.5, 79.5, 16.5, 17.5, 80.5, 16.5, 17.5, 81.5, 82.5, 18.5, 79.5, 80.5, 81.5, 19.5, 20.5, 21.5, 23.5, 78.5, 22.5, 24.5, 25.5, 75.5, 76.5, 77.5, 24.5, 26.5, 27.5, 28.5, 73.5, 74.5, 29.5, 30.5, 31.5, 32.5, 69.5, 70.5, 71.5, 72.5, 33.5, 65.5, 66.5, 67.5, 68.5, 34.5, 35.5, 36.5, 64.5, 37.5, 38.5, 59.5, 60.5, 61.5, 63.5, 39.5, 41.5, 57.5, 58.5, 62.5, 40.5, 42.5, 43.5, 44.5, 45.5, 46.5, 47.5, 48.5, 49.5, 51.5, 52.5, 53.5, 54.5, 55.5, 56.5, 50.5])
And here is an example of a dataset for which the code works as you can see in this plot https://i.stack.imgur.com/aYTrS.png:
# -----------------------------------------------------------------------------
x = np.array([25.5, 25.5, 25.5, 26.5, 26.5, 26.5, 26.5, 27.5, 27.5, 27.5, 27.5, 28.5, 28.5, 29.5, 29.5, 30.5, 30.5, 31.5, 31.5, 32.5, 32.5, 33.5, 33.5, 34.5, 34.5, 35.5, 35.5, 36.5, 36.5, 37.5, 37.5, 38.5, 38.5, 39.5, 39.5, 40.5, 40.5, 41.5, 41.5, 42.5, 42.5, 43.5, 43.5, 44.5, 44.5, 45.5, 45.5, 46.5, 46.5, 47.5, 47.5, 48.5, 48.5, 49.5, 49.5, 49.5, 50.5, 50.5, 51.5, 51.5, 52.5, 52.5, 53.5, 53.5, 54.5, 54.5, 55.5, 55.5, 56.5, 56.5, 57.5, 57.5, 58.5, 58.5, 59.5, 59.5, 60.5, 60.5, 61.5, 61.5, 62.5, 62.5, 62.5, 63.5, 63.5, 64.5, 64.5, 65.5, 65.5, 66.5, 66.5, 66.5, 66.5, 67.5, 67.5, 67.5, 68.5, 68.5, 68.5, 69.5, 69.5, 70.5, 70.5, 70.5, 71.5, 71.5, 71.5, 71.5, 71.5, 71.5, 72.5, 72.5])
y = np.array([54.5, 55.5, 57.5, 50.5, 53.5, 56.5, 58.5, 50.5, 51.5, 52.5, 58.5, 49.5, 58.5, 48.5, 58.5, 48.5, 59.5, 47.5, 60.5, 46.5, 61.5, 45.5, 61.5, 44.5, 60.5, 44.5, 60.5, 43.5, 61.5, 42.5, 60.5, 42.5, 60.5, 42.5, 61.5, 41.5, 61.5, 40.5, 61.5, 40.5, 61.5, 39.5, 61.5, 39.5, 61.5, 39.5, 62.5, 38.5, 61.5, 38.5, 61.5, 38.5, 61.5, 37.5, 61.5, 62.5, 37.5, 60.5, 37.5, 61.5, 38.5, 60.5, 37.5, 60.5, 37.5, 60.5, 37.5, 60.5, 37.5, 59.5, 37.5, 59.5, 36.5, 58.5, 37.5, 57.5, 36.5, 58.5, 37.5, 57.5, 36.5, 37.5, 56.5, 38.5, 55.5, 38.5, 55.5, 37.5, 54.5, 38.5, 39.5, 53.5, 54.5, 38.5, 40.5, 52.5, 39.5, 40.5, 52.5, 41.5, 51.5, 40.5, 49.5, 50.5, 41.5, 42.5, 43.5, 44.5, 47.5, 48.5, 45.5, 46.5])
Here is the rest of the code:
s = fitEllipse(x,y)
center = ellipse_center(s)
phi = ellipse_angle_of_rotation2(s)
axes = ellipse_axis_length(s)
print("The angle of rotation is", (phi*180/np.pi))
# get the individual axes
a, b = axes
ell = Ellipse(center, 2*a, 2*b, phi* (180 / np.pi), facecolor='none', edgecolor='black' )
fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})
ax.add_artist(ell)
ell.set_clip_box(ax.bbox)
ax.set_xlim(0, 100)
ax.set_ylim(0, 100)
scat = plt.scatter(x, y, c = "r", s = 1)
plt.show()
I want to make the SAME code work for both datasets.
A very robust method is shown below (Valid any angle of rotation).
From the coefficients of the above equation it is easy to compute the coordinates of the center, the length of axes, the angle of rotation, the foci, ... of the ellipse. See Eqs. (15-21) in : https://mathworld.wolfram.com/Ellipse.html
More general information in : https://fr.scribd.com/doc/14819165/Regressions-coniques-quadriques-circulaire-spherique (Edited in French).
Note that this method is valid to fit hyperbola as well. Of course the method is far to be new. This can be found in general litterature about fitting polynomial functions.

tuple of dictionary to list of tuples python

i want to convert tuple of dictionary to list of tuple or list of list
and in my final list i only want values of dictionary not keys
tuple of dictionary =
({'date_format': '01-04-2019', 'type': 'attendence', 'username': 'MCCuser0036', 'time_in': datetime.datetime(2019, 4, 1, 15, 29, 8), 'address_in': '736, Aala Hazrat Rd, Jagruti Nagar, Nehru Nagar, Kurla East, Mumbai, Maharashtra 400071, India', 'time_out': None, 'address_out': ''}, {'date_format': '01-04-2019', 'type': 'visit', 'username': 'MCCuser0036', 'time_in': datetime.datetime(2019, 4, 1, 15, 55, 45), 'address_in': '736, Aala Hazrat Rd, Jagruti Nagar, Nehru Nagar, Kurla East, Mumbai, Maharashtra 400071, India', 'time_out': None, 'address_out': ''}, {'date_format': '11-04-2019', 'type': 'attendence', 'username': 'MCCuser0036', 'time_in': datetime.datetime(2019, 4, 11, 15, 17, 56), 'address_in': 'Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India', 'time_out': None, 'address_out': ''}, {'date_format': '23-04-2019', 'type': 'attendence', 'username': 'MCCuser0036', 'time_in': datetime.datetime(2019, 4, 23, 17, 41, 5), 'address_in': 'Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India', 'time_out': datetime.datetime(2019, 4, 23, 17, 42, 54), 'address_out': 'Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India'})
import datetime
data =({'date_format': '01-04-2019', 'type': 'attendence', 'username': 'MCCuser0036', 'time_in': datetime.datetime(2019, 4, 1, 15, 29, 8), 'address_in': '736, Aala Hazrat Rd, Jagruti Nagar, Nehru Nagar, Kurla East, Mumbai, Maharashtra 400071, India', 'time_out': None, 'address_out': ''}, {'date_format': '01-04-2019', 'type': 'visit', 'username': 'MCCuser0036', 'time_in': datetime.datetime(2019, 4, 1, 15, 55, 45), 'address_in': '736, Aala Hazrat Rd, Jagruti Nagar, Nehru Nagar, Kurla East, Mumbai, Maharashtra 400071, India', 'time_out': None, 'address_out': ''}, {'date_format': '11-04-2019', 'type': 'attendence', 'username': 'MCCuser0036', 'time_in': datetime.datetime(2019, 4, 11, 15, 17, 56), 'address_in': 'Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India', 'time_out': None, 'address_out': ''}, {'date_format': '23-04-2019', 'type': 'attendence', 'username': 'MCCuser0036', 'time_in': datetime.datetime(2019, 4, 23, 17, 41, 5), 'address_in': 'Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India', 'time_out': datetime.datetime(2019, 4, 23, 17, 42, 54), 'address_out': 'Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India'})
result = []
for item in data:
result.append(list(item.values()))
print ((result))
what is equivalent to:
print([list(item.values()) for item in data]) # List comprehension
output:
[['01-04-2019', 'attendence', 'MCCuser0036', datetime.datetime(2019, 4, 1, 15, 29, 8), '736, Aala Hazrat Rd, Jagruti Nagar, Nehru Nagar, Kurla East, Mumbai, Maharashtra 400071, India', None, ''], ['01-04-2019', 'visit', 'MCCuser0036', datetime.datetime(2019, 4, 1, 15, 55, 45), '736, Aala Hazrat Rd, Jagruti Nagar, Nehru Nagar, Kurla East, Mumbai, Maharashtra 400071, India', None, ''], ['11-04-2019', 'attendence', 'MCCuser0036', datetime.datetime(2019, 4, 11, 15, 17, 56), 'Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India', None, ''], ['23-04-2019', 'attendence', 'MCCuser0036', datetime.datetime(2019, 4, 23, 17, 41, 5), 'Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India', datetime.datetime(2019, 4, 23, 17, 42, 54), 'Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India']]
import datetime
data = ({'date_format': '01-04-2019', 'type': 'attendence', 'username': 'MCCuser0036', 'time_in': datetime.datetime(2019, 4, 1, 15, 29, 8), 'address_in': '736, Aala Hazrat Rd, Jagruti Nagar, Nehru Nagar, Kurla East, Mumbai, Maharashtra 400071, India', 'time_out': None, 'address_out': ''}, {'date_format': '01-04-2019', 'type': 'visit', 'username': 'MCCuser0036', 'time_in': datetime.datetime(2019, 4, 1, 15, 55, 45), 'address_in': '736, Aala Hazrat Rd, Jagruti Nagar, Nehru Nagar, Kurla East, Mumbai, Maharashtra 400071, India', 'time_out': None, 'address_out': ''}, {'date_format': '11-04-2019', 'type': 'attendence', 'username': 'MCCuser0036', 'time_in': datetime.datetime(2019, 4, 11, 15, 17, 56), 'address_in': 'Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India', 'time_out': None, 'address_out': ''}, {'date_format': '23-04-2019', 'type': 'attendence', 'username': 'MCCuser0036', 'time_in': datetime.datetime(2019, 4, 23, 17, 41, 5), 'address_in': 'Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India', 'time_out': datetime.datetime(2019, 4, 23, 17, 42, 54), 'address_out': 'Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India'})
print([list(i.values()) for i in data])

Resources