R

时间:2018-10-05 19:37:25

标签: r

我想知道是否有人可以利用我拥有的以下数据将我指向正确的方向。

数据快照如下所示:

    PANID       WEEK brandID mariedstatus educ_male_HH educ_fem_HH
1 1100016 2001-03-19      48            2            2           3
2 1100016 2001-07-30      48            2            2           3
3 1100016 2001-12-17      48            2            2           3
4 1100057 2001-06-11     209            2            5           6
5 1100057 2001-06-18     207            2            5           6
6 1100057 2001-06-18     217            2            5           6

所以我有关于人(PANID),他们购买了特定品牌的WEEKbrandID的数据。然后是一些人口统计学,婚姻状况,男性家庭教育,女性家庭教育等。

我正在尝试计算条件概率,因此鉴于PANIDbrandID-48上购买了WEEK2001-03-19的事实,概率是多少,他们会再次购买同一品牌。

所以我只是在寻找一些有关如何解决该问题的建议/代码。

我曾考虑过要创建一个二进制变量来确定一个人在一年中是否多次购买同一品牌

  df %>%
  group_by(PANID, brandID) %>%
  mutate(user_total_brand_purchases = n()) %>%
  mutate(reordered = ifelse(user_total_brand_purchases > 1, 1, 0))

但是我不确定这是否可以解决我的问题。

数据样本:

     df <- structure(list(PANID = c(1100016L, 1100016L, 1100016L, 1100057L, 
1100057L, 1100057L, 1100057L, 1100057L, 1100057L, 1100057L, 1100057L, 
1100180L, 1100180L, 1100321L, 1100404L, 1100404L, 1100545L, 1100560L, 
1100560L, 1101162L, 1101162L, 1101238L, 1101279L, 1101279L, 1101279L, 
1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 
1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 
1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 
1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 
1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101758L, 1102012L, 
1102046L, 1102384L, 1102384L, 1103069L, 1103101L, 1103101L, 1103101L, 
1103101L, 1103101L, 1103101L, 1103101L, 1103325L, 1103614L, 1103671L, 
1103671L, 1103895L, 1103895L, 1103895L, 1104182L, 1104182L, 1104182L, 
1104182L, 1104182L, 1104182L, 1104182L, 1104182L, 1104182L, 1104182L, 
1104182L, 1104182L, 1104208L, 1104208L, 1104224L, 1104273L, 1104315L, 
1104315L, 1104315L, 1104315L, 1104364L, 1104372L, 1104372L, 1104406L, 
1104869L, 1104968L, 1105171L, 1105171L, 1105239L, 1105239L, 1105239L, 
1105965L, 1105999L, 1106146L, 1106567L, 1106567L, 1106567L, 1106575L, 
1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 
1107425L, 1107425L, 1107581L, 1107581L, 1107847L, 1107847L, 1107862L, 
1107953L, 1108043L, 1108225L, 1108308L, 1108308L, 1108308L, 1108308L, 
1108308L, 1108308L, 1108308L, 1108456L, 1108456L, 1108456L, 1108456L, 
1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 
1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1109165L, 1109165L, 
1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 
1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 
1109306L, 1109306L, 1109447L, 1109447L, 1109447L, 1109470L, 1109470L, 
1109470L, 1109470L, 1109470L, 1109470L, 1109470L, 1109488L, 1109595L, 
1109629L, 1109629L, 1109629L, 1109629L, 1109629L, 1109629L, 1109629L, 
1109629L, 1109629L, 1109652L, 1109652L, 1109652L, 1109652L, 1109652L, 
1109652L, 1109652L, 1109652L, 1109652L, 1109652L, 1109652L, 1109652L, 
1109652L, 1109652L, 1109652L, 1109652L, 1109652L, 1109652L, 1109652L, 
1109652L, 1109652L, 1109652L, 1109652L, 1109652L, 1109652L, 1109652L, 
1109652L, 1109769L, 1109769L, 1109769L, 1109769L, 1109769L, 1109835L, 
1109835L, 1109835L, 1109835L, 1109835L, 1109835L, 1109868L, 1109868L, 
1109892L, 1109892L, 1109892L, 1109892L, 1110072L, 1110072L, 1110072L, 
1110072L, 1110072L, 1110403L, 1110403L, 1110403L, 1110403L, 1110403L, 
1110403L, 1110403L, 1110437L, 1110445L, 1110445L, 1110445L, 1110445L, 
1110445L, 1110445L, 1110650L, 1110759L, 1110833L, 1110908L, 1110908L, 
1110908L, 1110908L, 1110908L, 1110908L, 1110908L, 1110908L, 1110908L, 
1110908L, 1110908L, 1110908L, 1110908L, 1110908L, 1110908L, 1110908L, 
1110908L, 1110940L, 1110940L, 1111138L, 1111138L, 1111187L, 1111229L, 
1111245L, 1111245L, 1111245L, 1111245L, 1111245L, 1111245L, 1111450L, 
1111450L, 1111450L, 1111526L, 1111526L, 1111526L, 1111526L, 1111542L, 
1111542L, 1111559L, 1111617L, 1111971L, 1112052L, 1112052L, 1112052L, 
1112110L, 1112235L, 1112235L, 1112235L, 1112235L, 1112235L, 1112235L, 
1112805L, 1112805L, 1112920L, 1113142L, 1113225L, 1113225L, 1113225L, 
1113225L, 1113225L, 1113282L, 1113282L, 1113282L, 1113282L, 1113282L, 
1113589L, 1113589L, 1113746L, 1113746L, 1113746L, 1113746L, 1113746L, 
1113746L, 1113746L, 1113746L, 1113746L, 1113746L, 1113746L, 1113746L, 
1113886L, 1113902L, 1113902L, 1113902L, 1113902L, 1113902L, 1113902L, 
1114413L, 1114421L, 1114421L, 1114421L, 1114421L, 1114421L, 1114421L, 
1114421L, 1114421L, 1114421L, 1114421L, 1114421L, 1114421L, 1114421L, 
1114421L, 1114421L, 1114421L, 1114421L, 1114421L, 1114421L, 1114421L, 
1114710L, 1114983L, 1114983L, 1114983L, 1114983L, 1114983L, 1114983L, 
1115154L, 1115154L, 1115154L, 1115378L, 1115808L, 1116210L, 1116467L, 
1116467L, 1116467L, 1116467L, 1116467L, 1116467L, 1116467L, 1116467L, 
1116467L, 1116467L, 1116467L, 1116467L, 1116467L, 1116467L, 1116467L, 
1116467L, 1116467L, 1116467L, 1116467L, 1116467L, 1116657L, 1116681L, 
1116681L, 1116681L, 1116681L, 1116681L, 1116699L, 1116699L, 1116699L, 
1116798L, 1116798L, 1116798L, 1117127L, 1117127L, 1117259L, 1117259L, 
1117259L, 1117259L, 1117267L, 1117572L, 1117572L, 1117572L, 1117614L, 
1117614L, 1117747L, 1117846L, 1118356L, 1118356L, 1118430L, 1118430L, 
1118430L, 1118430L, 1118430L, 1118430L, 1118430L, 1118430L, 1118430L, 
1118430L, 1118430L, 1118430L, 1118430L, 1118430L, 1118430L, 1118430L, 
1118430L, 1118430L, 1118430L, 1118430L, 1118430L, 1118588L, 1118588L, 
1118588L, 1118588L, 1118588L, 1118588L, 1118588L, 1118588L, 1118588L, 
1118588L, 1118588L, 1118588L, 1118588L, 1118588L, 1118588L, 1118976L, 
1119008L, 1119008L, 1119008L, 1119008L, 1119008L, 1119008L, 1119008L, 
1119008L, 1119057L, 1119057L, 1119057L, 1119115L, 1119115L, 1119115L, 
1119214L, 1119610L, 1119743L, 1119743L, 1119743L, 1119743L, 1119743L, 
1119784L, 1119784L, 1119784L, 1119909L, 1120600L, 1120618L, 1120618L, 
1120618L, 1120618L, 1120618L, 1120618L, 1120618L, 1120618L, 1120618L, 
1120618L, 1120618L, 1120618L, 1120618L, 1120725L, 1120782L, 1121004L, 
1121004L, 1121723L, 1121723L, 1121723L, 1121723L, 1121723L, 1121723L, 
1121723L, 1121749L, 1121749L, 1121749L, 1121749L, 1121749L, 1121749L, 
1121749L, 1121749L, 1121749L, 1121749L, 1121749L, 1121749L, 1121749L, 
1121962L, 1121962L, 1121962L, 1121962L, 1122226L, 1122325L, 1122325L, 
1122325L, 1122325L, 1122325L, 1122572L, 1122572L, 1122572L, 1122713L, 
1122713L, 1122713L, 1122747L, 1122747L, 1122747L, 1122747L, 1122747L, 
1122754L, 1122762L, 1122861L, 1122861L, 1122861L, 1123430L, 1123448L, 
1123448L, 1123448L, 1123448L, 1123448L, 1123448L, 1123448L, 1123448L, 
1123448L, 1123448L, 1123448L, 1123448L, 1123448L, 1123448L, 1123448L, 
1123448L, 1123448L, 1123448L, 1123448L, 1123448L, 1123448L, 1123448L, 
1123448L, 1123448L, 1123448L, 1123448L, 1123489L, 1123489L, 1123489L, 
1123489L, 1123489L, 1123489L, 1123489L, 1123489L, 1123489L, 1123489L, 
1123489L, 1123489L, 1123810L, 1123810L, 1123810L, 1124024L, 1124024L, 
1124024L), WEEK = structure(c(11400, 11533, 11673, 11484, 11491, 
11491, 11519, 11561, 11568, 11582, 11603, 11344, 11351, 11673, 
11673, 11673, 11533, 11344, 11631, 11442, 11652, 11673, 11323, 
11323, 11344, 11344, 11344, 11372, 11372, 11372, 11393, 11400, 
11400, 11414, 11414, 11428, 11428, 11435, 11435, 11449, 11470, 
11470, 11470, 11526, 11526, 11533, 11533, 11547, 11561, 11568, 
11568, 11568, 11589, 11589, 11610, 11610, 11631, 11652, 11421, 
11463, 11449, 11680, 11680, 11659, 11323, 11351, 11379, 11540, 
11610, 11645, 11659, 11645, 11442, 11498, 11540, 11519, 11540, 
11596, 11337, 11407, 11435, 11456, 11470, 11491, 11526, 11561, 
11589, 11596, 11666, 11666, 11519, 11540, 11554, 11414, 11673, 
11673, 11673, 11673, 11673, 11442, 11673, 11673, 11386, 11673, 
11540, 11673, 11484, 11491, 11596, 11680, 11673, 11505, 11323, 
11344, 11554, 11610, 11463, 11463, 11484, 11498, 11498, 11645, 
11659, 11379, 11484, 11533, 11673, 11414, 11428, 11533, 11631, 
11680, 11547, 11344, 11344, 11344, 11344, 11372, 11442, 11561, 
11372, 11379, 11393, 11421, 11442, 11456, 11463, 11498, 11498, 
11554, 11554, 11575, 11582, 11596, 11610, 11673, 11575, 11666, 
11428, 11449, 11463, 11477, 11498, 11512, 11526, 11540, 11554, 
11561, 11575, 11575, 11589, 11624, 11638, 11666, 11463, 11519, 
11589, 11351, 11365, 11400, 11547, 11547, 11617, 11645, 11498, 
11547, 11337, 11344, 11372, 11414, 11463, 11484, 11498, 11505, 
11673, 11337, 11351, 11358, 11365, 11393, 11400, 11407, 11407, 
11414, 11421, 11449, 11456, 11463, 11463, 11498, 11505, 11519, 
11519, 11526, 11533, 11547, 11568, 11575, 11589, 11617, 11659, 
11666, 11323, 11379, 11484, 11631, 11638, 11540, 11575, 11610, 
11617, 11631, 11659, 11449, 11589, 11519, 11519, 11673, 11673, 
11421, 11582, 11582, 11596, 11666, 11330, 11351, 11379, 11477, 
11526, 11582, 11610, 11442, 11379, 11379, 11477, 11540, 11561, 
11624, 11596, 11505, 11645, 11351, 11358, 11372, 11379, 11400, 
11407, 11421, 11484, 11512, 11526, 11533, 11554, 11561, 11568, 
11631, 11652, 11666, 11519, 11610, 11358, 11379, 11554, 11456, 
11407, 11428, 11470, 11533, 11631, 11673, 11526, 11589, 11603, 
11442, 11498, 11554, 11638, 11505, 11575, 11547, 11519, 11323, 
11477, 11526, 11547, 11673, 11456, 11463, 11463, 11589, 11645, 
11645, 11365, 11498, 11561, 11596, 11519, 11526, 11554, 11624, 
11638, 11505, 11512, 11519, 11568, 11589, 11379, 11540, 11344, 
11407, 11449, 11463, 11477, 11491, 11491, 11505, 11505, 11533, 
11596, 11680, 11554, 11449, 11540, 11568, 11568, 11575, 11673, 
11554, 11351, 11358, 11379, 11407, 11407, 11407, 11428, 11428, 
11435, 11442, 11456, 11470, 11477, 11498, 11505, 11512, 11526, 
11561, 11561, 11610, 11365, 11358, 11449, 11498, 11533, 11540, 
11582, 11344, 11673, 11673, 11575, 11659, 11386, 11379, 11386, 
11393, 11407, 11421, 11498, 11540, 11540, 11547, 11561, 11568, 
11568, 11589, 11603, 11610, 11617, 11631, 11645, 11666, 11666, 
11638, 11512, 11540, 11561, 11582, 11596, 11484, 11498, 11533, 
11344, 11680, 11680, 11358, 11477, 11400, 11561, 11582, 11603, 
11428, 11645, 11645, 11673, 11526, 11638, 11337, 11533, 11365, 
11673, 11358, 11372, 11386, 11407, 11435, 11463, 11477, 11484, 
11505, 11526, 11540, 11554, 11554, 11575, 11589, 11603, 11610, 
11631, 11638, 11652, 11666, 11351, 11414, 11421, 11435, 11449, 
11456, 11477, 11540, 11561, 11596, 11631, 11631, 11659, 11666, 
11673, 11645, 11442, 11463, 11477, 11575, 11589, 11596, 11624, 
11652, 11442, 11533, 11554, 11407, 11407, 11435, 11491, 11673, 
11407, 11477, 11547, 11547, 11680, 11484, 11596, 11659, 11638, 
11519, 11330, 11344, 11365, 11365, 11407, 11428, 11491, 11491, 
11505, 11540, 11554, 11561, 11561, 11568, 11568, 11414, 11610, 
11351, 11484, 11484, 11484, 11484, 11533, 11652, 11477, 11491, 
11498, 11512, 11512, 11533, 11540, 11540, 11540, 11547, 11547, 
11582, 11596, 11393, 11540, 11645, 11673, 11673, 11526, 11526, 
11582, 11603, 11666, 11547, 11547, 11645, 11589, 11589, 11589, 
11421, 11582, 11582, 11680, 11680, 11645, 11526, 11365, 11393, 
11659, 11638, 11330, 11337, 11344, 11358, 11365, 11400, 11407, 
11414, 11512, 11519, 11533, 11547, 11547, 11582, 11596, 11610, 
11617, 11624, 11638, 11645, 11645, 11666, 11673, 11673, 11673, 
11680, 11414, 11442, 11456, 11505, 11519, 11533, 11575, 11603, 
11631, 11638, 11652, 11659, 11512, 11533, 11533, 11323, 11463, 
11589), class = "Date"), brandID = c(48, 48, 48, 209, 207, 217, 
29, 29, 90, 90, 90, 131, 215, 28, 50, 208, 136, 216, 207, 48, 
48, 28, 29, 28, 29, 28, 140, 29, 28, 140, 29, 28, 140, 29, 28, 
29, 28, 29, 28, 28, 29, 28, 140, 29, 28, 28, 140, 33, 28, 29, 
28, 140, 29, 28, 28, 140, 28, 28, 131, 140, 29, 209, 214, 29, 
29, 29, 29, 153, 153, 153, 153, 29, 53, 28, 33, 135, 135, 209, 
211, 128, 225, 211, 211, 62, 211, 156, 62, 211, 211, 62, 217, 
217, 32, 50, 209, 210, 211, 156, 214, 48, 48, 48, 50, 173, 138, 
140, 207, 217, 214, 127, 69, 29, 100, 100, 214, 48, 51, 50, 51, 
131, 51, 51, 51, 230, 246, 32, 32, 50, 50, 28, 100, 131, 135, 
82, 1, 6, 69, 82, 7, 1, 48, 48, 79, 48, 48, 29, 29, 29, 48, 79, 
48, 29, 173, 29, 29, 29, 173, 99, 140, 140, 140, 140, 140, 140, 
140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 250, 28, 131, 
34, 158, 34, 34, 157, 157, 157, 48, 50, 29, 29, 29, 29, 29, 29, 
29, 29, 29, 99, 99, 99, 99, 99, 99, 99, 99, 50, 99, 50, 99, 99, 
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 29, 29, 
29, 29, 29, 230, 230, 230, 135, 135, 230, 228, 138, 207, 135, 
250, 209, 91, 91, 91, 91, 91, 48, 48, 48, 48, 48, 48, 48, 140, 
29, 29, 136, 136, 29, 29, 131, 48, 206, 131, 131, 28, 29, 131, 
131, 131, 131, 131, 131, 131, 131, 131, 48, 29, 131, 127, 250, 
250, 48, 48, 50, 157, 56, 147, 56, 100, 56, 56, 28, 147, 147, 
46, 209, 209, 29, 230, 127, 69, 29, 28, 28, 28, 50, 131, 209, 
79, 25, 209, 82, 209, 99, 230, 127, 197, 82, 1, 82, 82, 131, 
43, 43, 26, 131, 49, 28, 28, 29, 202, 127, 127, 127, 127, 135, 
29, 127, 127, 202, 127, 100, 153, 157, 48, 214, 48, 48, 131, 
29, 29, 29, 29, 29, 140, 29, 50, 29, 29, 29, 29, 29, 29, 29, 
29, 28, 29, 50, 29, 140, 117, 217, 48, 48, 28, 171, 117, 6, 214, 
28, 51, 195, 225, 70, 125, 80, 246, 207, 127, 131, 207, 131, 
127, 147, 209, 209, 214, 214, 1, 80, 209, 209, 209, 158, 48, 
228, 158, 228, 131, 48, 131, 50, 50, 7, 230, 127, 99, 99, 99, 
99, 140, 131, 214, 214, 135, 135, 127, 28, 230, 48, 48, 48, 48, 
48, 48, 48, 28, 158, 28, 48, 48, 48, 48, 48, 48, 48, 28, 48, 
48, 28, 48, 69, 50, 50, 50, 50, 50, 209, 50, 50, 209, 214, 214, 
69, 69, 69, 48, 138, 138, 138, 157, 157, 157, 157, 157, 48, 100, 
100, 157, 28, 209, 230, 148, 123, 29, 29, 230, 157, 209, 209, 
50, 53, 48, 48, 48, 48, 48, 48, 48, 48, 48, 131, 131, 131, 131, 
48, 28, 131, 140, 29, 209, 29, 140, 202, 69, 29, 209, 135, 135, 
230, 28, 230, 61, 138, 230, 135, 48, 50, 48, 61, 140, 131, 127, 
28, 63, 52, 217, 56, 209, 50, 140, 135, 48, 28, 140, 135, 50, 
101, 101, 224, 50, 131, 217, 140, 140, 140, 77, 29, 29, 29, 29, 
29, 29, 29, 29, 29, 29, 29, 29, 6, 29, 29, 29, 29, 29, 29, 29, 
28, 29, 29, 29, 48, 48, 48, 48, 48, 48, 48, 28, 140, 28, 48, 
48, 140, 48, 97, 127, 131, 29, 29, 29), mariedstatus = c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 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, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 4L, 4L, 5L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 5L, 4L, 4L, 4L, 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, 2L, 4L, 2L, 0L, 
0L, 0L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 0L, 0L, 
2L, 2L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L,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L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 8L, 8L, 8L, 6L, 6L, 6L, 6L, 6L, 4L, 4L, 4L, 4L, 4L, 
8L, 8L, 8L, 6L, 9L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 7L, 4L, 6L, 6L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 5L, 
5L, 5L, 5L, 5L, 6L, 6L, 6L, 8L, 8L, 8L, 4L, 4L, 4L, 4L, 4L, 7L, 
6L, 5L, 5L, 5L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 8L, 8L, 
8L)), row.names = c(NA, 600L), class = "data.frame")

编辑:

例如,

PANID: 110005709-03上购买了相同的品牌,然后两周后又在09-17上购买了同一品牌,然后在3周后又在10-08上购买了

    PANID       WEEK brandID mariedstatus educ_male_HH educ_fem_HH
1 1100057 2001-06-11     209            2            5           6
2 1100057 2001-06-18     207            2            5           6
3 1100057 2001-06-18     217            2            5           6
4 1100057 2001-07-16      29            2            5           6
5 1100057 2001-08-27      29            2            5           6
6 1100057 2001-09-03      90            2            5           6
7 1100057 2001-09-17      90            2            5           6
8 1100057 2001-10-08      90            2            5           6

1 个答案:

答案 0 :(得分:1)

也许您可以使用逻辑回归来拟合模型,并根据人口统计,星期和brandID来预测“品牌多次购买”事件

#!/usr/bin/python3
# -*- coding: utf-8 -*-


from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from functools import partial
import datetime,psutil,sys

class playingCard(QPushButton):

    def __init__(self, Text = '', parent = None):
        super(playingCard, self).__init__()
        self.ftop = 10
        self.fleft = 10
        self.fwidth = 87
        self.fheight = 124
        self.initUI()

    def initUI(self):
        self.setGeometry(self.fleft, self.ftop, self.fwidth+2, self.fheight+2)
        self.setText('')
        pixmap = QPixmap('clubs1.png')
        pixmap = pixmap.scaled(self.fwidth,self.fheight, Qt.KeepAspectRatio, Qt.FastTransformation)
        buttonicon = QIcon(pixmap)
        self.setIcon(buttonicon)
        self.setIconSize( QSize(self.fwidth,self.fheight))
        self.setFixedSize( QSize(self.fwidth+2,self.fheight+2))

    def mousePressEvent(self, event):
        self.__mousePressPos = None
        self.__mouseMovePos = None
        if event.button() == Qt.LeftButton:
            self.__mousePressPos = event.globalPos()
            self.__mouseMovePos = event.globalPos()

        super(playingCard, self).mousePressEvent(event)

    def mouseMoveEvent(self, event):
        if event.buttons() == Qt.LeftButton:
            # adjust offset from clicked point to origin of widget
            currPos = self.mapToGlobal(self.pos())
            globalPos = event.globalPos()
            diff = globalPos - self.__mouseMovePos
            newPos = self.mapFromGlobal(currPos + diff)
            self.move(newPos)

            self.__mouseMovePos = globalPos

        super(playingCard, self).mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        if self.__mousePressPos is not None:
            moved = event.globalPos() - self.__mousePressPos 
            if moved.manhattanLength() > 3:
                event.ignore()
                return

        super(playingCard, self).mouseReleaseEvent(event)


class Example(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()

    def addAction(self,name,shortcut,status):
        self.actions[name] = QAction(name, self)
        self.actions[name].setShortcut(shortcut)
        self.actions[name].setStatusTip(status)
        if name == 'Exit':
            self.actions[name].triggered.connect( self.close )
        else:
            self.actions[name].triggered.connect( partial(self.viewEvent,name) )

    def hastab(self,tabname):
        return self.tabWidget.findChild(QWidget, tabname) != None

    def ontab(self,tabname):
        currentIndex = self.tabWidget.currentIndex()
        currentTitle = self.tabWidget.tabText(currentIndex)  
        return tabname == currentTitle


    def gettab(self,tabname):
        page = self.tabWidget.findChild(QWidget, tabname) 
        return self.tabWidget.indexOf(page)

    def initUI(self):               
        self.actions = dict()       # holds the QActions
        self.tabs = dict()

        self.tabWidget = QTabWidget()
        self.tabWidget.setTabsClosable(True)
        self.tabWidget.tabCloseRequested.connect(self.closeTab)
        self.setCentralWidget(self.tabWidget)

        self.addAction('Exit', 'Ctrl+Q','Exit application')
        self.addAction('Game','Ctrl+G','Game')

        self.statusBar()

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')

        fileMenu.addAction( self.actions['Game'] )
        fileMenu.addAction( self.actions['Exit'] )

        self.setWindowTitle('Main window')
        self.showMaximized()

    def closeTab (self, currentIndex):
        currentQWidget = self.tabWidget.widget(currentIndex)
        title=self.tabWidget.tabText(currentIndex)
        currentQWidget.deleteLater()
        self.tabWidget.removeTab(currentIndex)

        del self.tabs[title]
        del self.tables[title]
        self.timers[title].stop()
        del self.timers[title]

    def keyPressEvent(self, e):  

        currentIndex=self.tabWidget.currentIndex()
        title = None
        if currentIndex != -1:
            title=self.tabWidget.tabText(currentIndex)

        if e.key() == Qt.Key_F11:
            if self.isMaximized():
                self.showNormal()
            else:
                self.showMaximized()

    def viewEvent(self, name):
        if name in self.tabs:
            return

        self.tabs[name] = QWidget()

        vbox = QVBoxLayout()
        vbox.addWidget( playingCard() )


        # Add box layout, add table to box layout and add box layout to widget
        self.tabs[name].layout = vbox
        self.tabs[name].setLayout(self.tabs[name].layout) 
        self.tabWidget.addTab(self.tabs[name],name)

    def closeEvent(self, event):

        reply = QMessageBox.question(self, 'Message',
            "Are you sure to quit?", QMessageBox.Yes | 
            QMessageBox.No, QMessageBox.No)

        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()  

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())