pyreml 0.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyreml/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ from .larix import larix
2
+ from .kinships import A_genomic, A_pedigree, D_pedigree
3
+ from .nearPD import nearPD
pyreml/data/Larix.csv ADDED
@@ -0,0 +1,208 @@
1
+ X,Y,ID,BLOC,VARIETY,DAM,SIRE,year,height,circumference,flexuosity
2
+ 16,32,16:32,B9,F0910,104.0,109.0,2000,475.0,150.0,4.0
3
+ 39,39,39:39,B11,F0910,104.0,109.0,2000,500.0,158.0,4.0
4
+ 35,68,35:68,B12,F0910,104.0,109.0,2000,472.0,160.0,5.0
5
+ 25,30,25:30,B15,F0910,104.0,109.0,2000,500.0,186.0,4.0
6
+ 36,55,36:55,B8,F0910,104.0,109.0,2000,549.0,192.0,4.0
7
+ 8,33,8:33,B5,F0910,104.0,109.0,2000,623.0,227.0,4.0
8
+ 31,32,31:32,B16,F0910,104.0,109.0,2000,597.0,238.0,4.0
9
+ 16,56,16:56,B4,F0910,104.0,109.0,2000,530.0,241.0,4.0
10
+ 66,50,66:50,B1,F0910,104.0,109.0,2000,617.0,265.0,4.0
11
+ 27,39,27:39,B7,F0910,104.0,109.0,2000,670.0,285.0,5.0
12
+ 34,70,34:70,B12,F0912,106.0,109.0,2000,348.0,105.0,5.0
13
+ 60,72,60:72,B13,F0912,106.0,109.0,2000,416.0,119.0,5.0
14
+ 8,36,8:36,B5,F0912,106.0,109.0,2000,424.0,135.0,4.0
15
+ 35,6,35:6,B10,F0912,106.0,109.0,2000,448.0,150.0,5.0
16
+ 34,27,34:27,B16,F0912,106.0,109.0,2000,500.0,162.0,4.0
17
+ 37,47,37:47,B11,F0912,106.0,109.0,2000,469.0,165.0,5.0
18
+ 48,31,48:31,B6,F0912,106.0,109.0,2000,456.0,167.0,4.0
19
+ 51,30,51:30,B2,F0912,106.0,109.0,2000,470.0,171.0,4.0
20
+ 27,45,27:45,B7,F0912,106.0,109.0,2000,515.0,178.0,4.0
21
+ 19,56,19:56,B4,F0912,106.0,109.0,2000,480.0,180.0,4.0
22
+ 54,74,54:74,B3,F0912,106.0,109.0,2000,497.0,181.0,3.0
23
+ 35,14,35:14,B14,F0912,106.0,109.0,2000,510.0,198.0,5.0
24
+ 36,57,36:57,B8,F0912,106.0,109.0,2000,603.0,215.0,4.0
25
+ 34,73,34:73,B12,F0914,106.0,166.0,2000,445.0,124.0,5.0
26
+ 62,50,62:50,B1,F0914,106.0,166.0,2000,459.0,143.0,3.0
27
+ 54,73,54:73,B3,F0914,106.0,166.0,2000,502.0,150.0,5.0
28
+ 38,52,38:52,B8,F0914,106.0,166.0,2000,500.0,150.0,5.0
29
+ 28,35,28:35,B15,F0914,106.0,166.0,2000,485.0,154.0,4.0
30
+ 59,72,59:72,B13,F0914,106.0,166.0,2000,508.0,158.0,4.0
31
+ 20,35,20:35,B9,F0914,106.0,166.0,2000,460.0,163.0,4.0
32
+ 36,7,36:7,B10,F0914,106.0,166.0,2000,459.0,166.0,3.0
33
+ 58,33,58:33,B2,F0914,106.0,166.0,2000,527.0,178.0,4.0
34
+ 51,36,51:36,B6,F0914,106.0,166.0,2000,540.0,190.0,3.0
35
+ 8,34,8:34,B5,F0914,106.0,166.0,2000,485.0,205.0,3.0
36
+ 39,48,39:48,B11,F0914,106.0,166.0,2000,555.0,220.0,4.0
37
+ 17,54,17:54,B4,F0914,106.0,166.0,2000,550.0,222.0,4.0
38
+ 58,69,58:69,B13,F0915,106.0,222.0,2000,320.0,120.0,5.0
39
+ 27,40,27:40,B7,F0915,106.0,222.0,2000,460.0,132.0,3.0
40
+ 35,15,35:15,B14,F0915,106.0,222.0,2000,393.0,144.0,5.0
41
+ 56,66,56:66,B3,F0915,106.0,222.0,2000,485.0,164.0,5.0
42
+ 40,51,40:51,B8,F0915,106.0,222.0,2000,500.0,165.0,5.0
43
+ 39,4,39:4,B10,F0915,106.0,222.0,2000,423.0,176.0,5.0
44
+ 59,33,59:33,B2,F0915,106.0,222.0,2000,476.0,190.0,4.0
45
+ 47,38,47:38,B6,F0915,106.0,222.0,2000,534.0,195.0,5.0
46
+ 16,36,16:36,B9,F0915,106.0,222.0,2000,522.0,204.0,5.0
47
+ 8,32,8:32,B5,F0915,106.0,222.0,2000,548.0,205.0,3.0
48
+ 19,55,19:55,B4,F0915,106.0,222.0,2000,567.0,222.0,4.0
49
+ 67,45,67:45,B1,F0915,106.0,222.0,2000,575.0,232.0,3.0
50
+ 37,43,37:43,B11,F0915,106.0,222.0,2000,512.0,232.0,4.0
51
+ 58,72,58:72,B13,F1002,106.0,221.0,2000,328.0,91.0,4.0
52
+ 50,75,50:75,B3,F1002,106.0,221.0,2000,380.0,108.0,5.0
53
+ 36,31,36:31,B16,F1002,106.0,221.0,2000,443.0,122.0,5.0
54
+ 39,38,39:38,B11,F1002,106.0,221.0,2000,355.0,130.0,3.0
55
+ 15,29,15:29,B9,F1002,106.0,221.0,2000,465.0,140.0,4.0
56
+ 41,54,41:54,B8,F1002,106.0,221.0,2000,430.0,166.0,4.0
57
+ 27,48,27:48,B7,F1002,106.0,221.0,2000,495.0,182.0,5.0
58
+ 64,42,64:42,B1,F1002,106.0,221.0,2000,498.0,215.0,5.0
59
+ 50,77,50:77,B3,F1015,242.0,222.0,2000,400.0,105.0,4.0
60
+ 37,4,37:4,B10,F1015,242.0,222.0,2000,452.0,152.0,3.0
61
+ 11,29,11:29,B5,F1015,242.0,222.0,2000,535.0,198.0,4.0
62
+ 64,43,64:43,B1,F1015,242.0,222.0,2000,605.0,218.0,3.0
63
+ 24,40,24:40,B7,F1042,104.0,214.0,2000,510.0,145.0,5.0
64
+ 22,29,22:29,B15,F1042,104.0,214.0,2000,555.0,170.0,5.0
65
+ 33,30,33:30,B16,F1042,104.0,214.0,2000,550.0,170.0,4.0
66
+ 59,31,59:31,B2,F1042,104.0,214.0,2000,547.0,250.0,3.0
67
+ 51,76,51:76,B3,F1118,104.0,106.0,2000,282.0,85.0,4.0
68
+ 40,75,40:75,B12,F1118,104.0,106.0,2000,390.0,136.0,5.0
69
+ 42,41,42:41,B11,F1118,104.0,106.0,2000,422.0,138.0,5.0
70
+ 11,33,11:33,B5,F1118,104.0,106.0,2000,452.0,148.0,4.0
71
+ 30,19,30:19,B14,F1118,104.0,106.0,2000,440.0,150.0,5.0
72
+ 24,47,24:47,B7,F1118,104.0,106.0,2000,470.0,158.0,4.0
73
+ 59,36,59:36,B2,F1118,104.0,106.0,2000,498.0,175.0,4.0
74
+ 68,74,68:74,B13,F1118,104.0,106.0,2000,445.0,178.0,5.0
75
+ 52,37,52:37,B6,F1118,104.0,106.0,2000,502.0,185.0,4.0
76
+ 38,11,38:11,B10,F1118,104.0,106.0,2000,509.0,188.0,5.0
77
+ 37,27,37:27,B16,F1118,104.0,106.0,2000,472.0,200.0,4.0
78
+ 58,50,58:50,B1,F1118,104.0,106.0,2000,543.0,218.0,4.0
79
+ 18,27,18:27,B9,F1118,104.0,106.0,2000,570.0,218.0,4.0
80
+ 13,50,13:50,B4,F1118,104.0,106.0,2000,570.0,221.0,4.0
81
+ 38,55,38:55,B8,F1118,104.0,106.0,2000,571.0,224.0,4.0
82
+ 23,28,23:28,B15,F1118,104.0,106.0,2000,555.0,242.0,4.0
83
+ 50,76,50:76,B3,F1119,104.0,284.0,2000,351.0,105.0,4.0
84
+ 41,71,41:71,B12,F1119,104.0,284.0,2000,466.0,152.0,5.0
85
+ 33,14,33:14,B14,F1119,104.0,284.0,2000,475.0,158.0,5.0
86
+ 56,30,56:30,B2,F1119,104.0,284.0,2000,475.0,160.0,4.0
87
+ 18,31,18:31,B9,F1119,104.0,284.0,2000,540.0,183.0,4.0
88
+ 67,77,67:77,B13,F1119,104.0,284.0,2000,460.0,186.0,4.0
89
+ 38,10,38:10,B10,F1119,104.0,284.0,2000,485.0,192.0,5.0
90
+ 58,40,58:40,B6,F1119,104.0,284.0,2000,580.0,195.0,3.0
91
+ 18,51,18:51,B4,F1119,104.0,284.0,2000,555.0,196.0,3.0
92
+ 23,30,23:30,B15,F1119,104.0,284.0,2000,525.0,216.0,4.0
93
+ 11,37,11:37,B5,F1119,104.0,284.0,2000,545.0,217.0,4.0
94
+ 63,72,63:72,B13,F1124,106.0,104.0,2000,338.0,105.0,5.0
95
+ 43,55,43:55,B8,F1124,106.0,104.0,2000,370.0,135.0,5.0
96
+ 50,30,50:30,B6,F1124,106.0,104.0,2000,474.0,174.0,4.0
97
+ 38,12,38:12,B10,F1124,106.0,104.0,2000,443.0,176.0,5.0
98
+ 56,64,56:64,B3,F1124,106.0,104.0,2000,494.0,183.0,4.0
99
+ 64,49,64:49,B1,F1124,106.0,104.0,2000,442.0,195.0,5.0
100
+ 6,27,6:27,B5,F1124,106.0,104.0,2000,565.0,199.0,5.0
101
+ 21,52,21:52,B4,F1124,106.0,104.0,2000,575.0,202.0,3.0
102
+ 37,30,37:30,B16,F1124,106.0,104.0,2000,520.0,216.0,4.0
103
+ 39,45,39:45,B11,F1124,106.0,104.0,2000,533.0,225.0,5.0
104
+ 18,29,18:29,B9,F1124,106.0,104.0,2000,560.0,238.0,4.0
105
+ 56,31,56:31,B2,F1125,106.0,284.0,2000,450.0,130.0,3.0
106
+ 30,20,30:20,B14,F1125,106.0,284.0,2000,423.0,153.0,4.0
107
+ 41,72,41:72,B12,F1125,106.0,284.0,2000,481.0,185.0,3.0
108
+ 23,27,23:27,B15,F1125,106.0,284.0,2000,515.0,197.0,4.0
109
+ 11,34,11:34,B5,F1125,106.0,284.0,2000,616.0,238.0,3.0
110
+ 32,30,32:30,B16,F1128,109.0,166.0,2000,560.0,207.0,4.0
111
+ 57,64,57:64,B3,F1129,109.0,221.0,2000,392.0,125.0,4.0
112
+ 38,16,38:16,B14,F1129,109.0,221.0,2000,362.0,170.0,4.0
113
+ 41,48,41:48,B11,F1129,109.0,221.0,2000,570.0,191.0,5.0
114
+ 52,73,52:73,B3,F1147,214.0,,2000,375.0,108.0,4.0
115
+ 14,29,14:29,B9,F1147,214.0,,2000,450.0,128.0,5.0
116
+ 35,51,35:51,B8,F1147,214.0,,2000,445.0,138.0,4.0
117
+ 42,38,42:38,B11,F1147,214.0,,2000,420.0,143.0,4.0
118
+ 23,39,23:39,B7,F1147,214.0,,2000,505.0,159.0,4.0
119
+ 22,50,22:50,B4,F1147,214.0,,2000,490.0,166.0,4.0
120
+ 59,49,59:49,B1,F1147,214.0,,2000,484.0,180.0,4.0
121
+ 60,27,60:27,B2,F1147,214.0,,2000,535.0,188.0,4.0
122
+ 31,13,31:13,B14,F1147,214.0,,2000,512.0,195.0,4.0
123
+ 26,27,26:27,B15,F1147,214.0,,2000,580.0,197.0,4.0
124
+ 34,28,34:28,B16,F1152,221.0,109.0,2000,545.0,218.0,4.0
125
+ 14,32,14:32,B9,F1153,221.0,109.0,2000,487.0,144.0,4.0
126
+ 10,34,10:34,B5,F1153,221.0,109.0,2000,545.0,205.0,4.0
127
+ 35,18,35:18,B14,F1153,221.0,109.0,2000,525.0,222.0,4.0
128
+ 40,73,40:73,B12,F1154,221.0,284.0,2000,484.0,140.0,4.0
129
+ 22,47,22:47,B7,F1154,221.0,284.0,2000,490.0,152.0,4.0
130
+ 63,69,63:69,B13,F1154,221.0,284.0,2000,493.0,166.0,5.0
131
+ 44,55,44:55,B8,F1154,221.0,284.0,2000,484.0,168.0,5.0
132
+ 57,65,57:65,B3,F1154,221.0,284.0,2000,505.0,172.0,4.0
133
+ 32,5,32:5,B10,F1154,221.0,284.0,2000,450.0,175.0,3.0
134
+ 37,40,37:40,B11,F1154,221.0,284.0,2000,545.0,190.0,5.0
135
+ 28,17,28:17,B14,F1154,221.0,284.0,2000,505.0,196.0,5.0
136
+ 14,35,14:35,B9,F1154,221.0,284.0,2000,495.0,198.0,4.0
137
+ 25,37,25:37,B15,F1154,221.0,284.0,2000,575.0,212.0,3.0
138
+ 54,39,54:39,B6,F1154,221.0,284.0,2000,487.0,214.0,4.0
139
+ 60,40,60:40,B6,F1160,242.0,106.0,2000,342.0,104.0,3.0
140
+ 38,14,38:14,B14,F1160,242.0,106.0,2000,385.0,130.0,4.0
141
+ 36,28,36:28,B16,F1160,242.0,106.0,2000,480.0,142.0,5.0
142
+ 38,8,38:8,B10,F1160,242.0,106.0,2000,443.0,158.0,4.0
143
+ 22,33,22:33,B15,F1160,242.0,106.0,2000,520.0,180.0,4.0
144
+ 61,50,61:50,B1,F1160,242.0,106.0,2000,556.0,185.0,3.0
145
+ 10,35,10:35,B5,F1160,242.0,106.0,2000,575.0,189.0,3.0
146
+ 68,75,68:75,B13,F1160,242.0,106.0,2000,500.0,195.0,4.0
147
+ 37,46,37:46,B11,F1160,242.0,106.0,2000,542.0,202.0,4.0
148
+ 23,45,23:45,B7,F1160,242.0,106.0,2000,615.0,207.0,3.0
149
+ 32,32,32:32,B16,F1162,242.0,221.0,2000,505.0,174.0,4.0
150
+ 42,43,42:43,B11,F1162,242.0,221.0,2000,550.0,235.0,5.0
151
+ 18,33,18:33,B9,F1164,242.0,,2000,411.0,101.0,3.0
152
+ 30,34,30:34,B16,F1164,242.0,,2000,435.0,132.0,5.0
153
+ 55,72,55:72,B3,F1164,242.0,,2000,465.0,137.0,4.0
154
+ 23,38,23:38,B7,F1164,242.0,,2000,500.0,152.0,3.0
155
+ 37,56,37:56,B8,F1164,242.0,,2000,473.0,154.0,5.0
156
+ 7,31,7:31,B5,F1164,242.0,,2000,527.0,161.0,4.0
157
+ 29,19,29:19,B14,F1164,242.0,,2000,438.0,172.0,4.0
158
+ 30,8,30:8,B10,F1164,242.0,,2000,492.0,180.0,5.0
159
+ 59,27,59:27,B2,F1164,242.0,,2000,578.0,202.0,4.0
160
+ 68,50,68:50,B1,F1164,242.0,,2000,588.0,214.0,4.0
161
+ 20,52,20:52,B4,F1164,242.0,,2000,590.0,220.0,4.0
162
+ 60,46,60:46,B1,F1267,104.0,214.0,2000,520.0,185.0,4.0
163
+ 46,50,46:50,B8,F1268,104.0,221.0,2000,355.0,125.0,5.0
164
+ 21,42,21:42,B7,F1268,104.0,221.0,2000,435.0,135.0,5.0
165
+ 36,68,36:68,B12,F1268,104.0,221.0,2000,460.0,152.0,5.0
166
+ 33,11,33:11,B10,F1268,104.0,221.0,2000,445.0,155.0,4.0
167
+ 68,72,68:72,B13,F1268,104.0,221.0,2000,403.0,171.0,4.0
168
+ 17,31,17:31,B9,F1268,104.0,221.0,2000,595.0,192.0,5.0
169
+ 39,43,39:43,B11,F1268,104.0,221.0,2000,568.0,208.0,5.0
170
+ 36,15,36:15,B14,F1268,104.0,221.0,2000,522.0,212.0,4.0
171
+ 10,29,10:29,B5,F1268,104.0,221.0,2000,602.0,243.0,4.0
172
+ 27,28,27:28,B15,F1268,104.0,221.0,2000,660.0,294.0,4.0
173
+ 20,32,20:32,B9,F1272,109.0,166.0,2000,460.0,130.0,4.0
174
+ 20,51,20:51,B4,F1272,109.0,166.0,2000,445.0,154.0,5.0
175
+ 37,39,37:39,B11,F1272,109.0,166.0,2000,626.0,206.0,4.0
176
+ 65,47,65:47,B1,F1272,109.0,166.0,2000,568.0,210.0,4.0
177
+ 53,28,53:28,B2,F1272,109.0,166.0,2000,560.0,214.0,4.0
178
+ 59,39,59:39,B6,F1275,109.0,222.0,2000,502.0,150.0,4.0
179
+ 58,74,58:74,B13,F1275,109.0,222.0,2000,455.0,155.0,5.0
180
+ 10,32,10:32,B5,F1275,109.0,222.0,2000,536.0,162.0,4.0
181
+ 41,73,KAMIE,B12,KAMIE,,,2000,284.0,90.0,3.0
182
+ 14,52,KAMIE,B4,KAMIE,,,2000,325.0,90.0,4.0
183
+ 21,35,KAMIE,B15,KAMIE,,,2000,405.0,110.0,4.0
184
+ 61,31,KAMIE,B2,KAMIE,,,2000,431.0,118.0,3.0
185
+ 32,28,KAMIE,B16,KAMIE,,,2000,362.0,120.0,3.0
186
+ 12,29,KAMIE,B9,KAMIE,,,2000,435.0,120.0,4.0
187
+ 52,67,KAMIE,B3,KAMIE,,,2000,478.0,131.0,4.0
188
+ 31,14,KAMIE,B14,KAMIE,,,2000,432.0,136.0,4.0
189
+ 54,34,KAMIE,B6,KAMIE,,,2000,410.0,138.0,3.0
190
+ 25,39,KAMIE,B7,KAMIE,,,2000,449.0,160.0,4.0
191
+ 11,35,KAMIE,B5,KAMIE,,,2000,488.0,164.0,3.0
192
+ 35,3,KAMIE,B10,KAMIE,,,2000,448.0,165.0,4.0
193
+ 34,51,KAMIE,B8,KAMIE,,,2000,528.0,226.0,4.0
194
+ 38,39,KAMIE,B11,KAMIE,,,2000,542.0,236.0,5.0
195
+ 34,34,RUDA ,B16,RUDA ,,,2000,335.0,80.0,5.0
196
+ 60,77,RUDA ,B13,RUDA ,,,2000,350.0,113.0,1.0
197
+ 38,70,RUDA ,B12,RUDA ,,,2000,388.0,121.0,5.0
198
+ 9,27,RUDA ,B5,RUDA ,,,2000,429.0,132.0,5.0
199
+ 48,28,RUDA ,B6,RUDA ,,,2000,425.0,132.0,4.0
200
+ 55,30,RUDA ,B2,RUDA ,,,2000,452.0,140.0,4.0
201
+ 35,10,RUDA ,B10,RUDA ,,,2000,410.0,150.0,3.0
202
+ 21,54,RUDA ,B4,RUDA ,,,2000,345.0,156.0,4.0
203
+ 50,67,RUDA ,B3,RUDA ,,,2000,492.0,162.0,5.0
204
+ 39,51,RUDA ,B8,RUDA ,,,2000,510.0,170.0,5.0
205
+ 26,46,RUDA ,B7,RUDA ,,,2000,570.0,180.0,3.0
206
+ 31,15,RUDA ,B14,RUDA ,,,2000,462.0,182.0,3.0
207
+ 59,50,RUDA ,B1,RUDA ,,,2000,502.0,210.0,3.0
208
+ 15,30,RUDA ,B9,RUDA ,,,2000,625.0,338.0,4.0
pyreml/kinships.py ADDED
@@ -0,0 +1,225 @@
1
+ import numpy as np
2
+ import pandas as pd
3
+
4
+ def A_genomic(
5
+ X,
6
+ min_MAF: float = 0.05,
7
+ max_missing: float = 0.1,
8
+ shrink: bool = False
9
+ ) :
10
+ '''
11
+ This is a direct retranscription of the A_mat
12
+ function from the R paclage: rrBLUP
13
+ X is a diploid species SNP matrix encoded: -1, 0, 1
14
+ the following parameters are fixed:
15
+ - impute.method = "mean"
16
+ - shrink.method = "EJ"
17
+ '''
18
+
19
+ n = X.shape[0]
20
+
21
+ frac_missing = np.isnan(X).mean(axis=0)
22
+ freq = np.nanmean(X + 1, axis=0) / 2
23
+ MAF = np.minimum(freq, 1 - freq)
24
+
25
+ markers = np.where((MAF >= min_MAF) & (frac_missing <= max_missing))[0]
26
+ m = len(markers)
27
+ var_A = 2 * np.mean(freq[markers] * (1 - freq[markers]))
28
+ mono = np.where(freq * (1 - freq) == 0)[0]
29
+ X[:, mono] = 2 * freq[mono] - 1
30
+ freq_mat = np.ones((n, 1)) @ freq[markers].reshape(1, -1)
31
+ W = X[:, markers] + 1 - 2 * freq_mat
32
+
33
+ # imputation
34
+ W[np.isnan(W)] = 0
35
+
36
+ if shrink:
37
+ Z = W - W.mean(axis=1, keepdims=True)
38
+ Z2 = Z ** 2
39
+ S = (Z @ Z.T) / m
40
+ target = np.mean(np.diag(S)) * np.eye(n)
41
+ var_S = (Z2 @ Z2.T) / m**2 - S**2 / m
42
+ b2 = var_S.sum()
43
+ d2 = ((S - target) ** 2).sum()
44
+ delta = float(np.clip(b2 / d2, 0, 1))
45
+ print(f"Shrinkage intensity: {delta:.2f}")
46
+ cov_W = target * delta + (1 - delta) * S
47
+ W_mean = W.mean(axis=1)
48
+ K = (cov_W + np.outer(W_mean, W_mean)) / var_A
49
+ else:
50
+ K = (W @ W.T) / var_A / m
51
+
52
+ return K
53
+
54
+ def A_pedigree(pedigree: pd.DataFrame) -> np.ndarray:
55
+ '''
56
+ This is a direct retranscription of the makeD
57
+ function from the R paclage: nadiv
58
+ pedigree is a pandas dataframe with 3 columns:
59
+ - ID: the individual
60
+ - DAM and SIRE: its parents, may be NA
61
+ '''
62
+ ped = pedigree.copy()
63
+ ped.columns = ped.columns.str.lower()
64
+
65
+ # --- checks ---
66
+ if not {"id", "dam", "sire"}.issubset(ped.columns):
67
+ raise ValueError("Pedigree must have columns: id, dam, sire")
68
+
69
+ if ped["id"].duplicated().any():
70
+ dups = ped["id"][ped["id"].duplicated()].unique().tolist()
71
+ raise ValueError(f"Duplicate individuals: {dups}")
72
+
73
+ known_dam = ped["dam"].dropna().unique()
74
+ known_sire = ped["sire"].dropna().unique()
75
+ all_ids = set(ped["id"])
76
+ missing_parents = (set(known_dam) | set(known_sire)) - all_ids
77
+ if missing_parents:
78
+ raise ValueError(f"Parents not in pedigree: {sorted(missing_parents)}")
79
+
80
+ # --- generation assignment + sort ---
81
+ ids = ped["id"].values
82
+ dam = ped["dam"].values
83
+ sire = ped["sire"].values
84
+ n = len(ids)
85
+ id_to_idx = {v: i for i, v in enumerate(ids)}
86
+
87
+ gen = np.zeros(n, dtype=int)
88
+ changed = True
89
+ while changed:
90
+ changed = False
91
+ for i in range(n):
92
+ g = 0
93
+ if pd.notna(dam[i]):
94
+ g = max(g, gen[id_to_idx[dam[i]]] + 1)
95
+ if pd.notna(sire[i]):
96
+ g = max(g, gen[id_to_idx[sire[i]]] + 1)
97
+ if g > gen[i]:
98
+ gen[i] = g
99
+ changed = True
100
+
101
+ # sort by (gen, dam, sire) with NA first (like na.last=FALSE in R)
102
+ dam_key = np.array([id_to_idx[d] + 1 if pd.notna(d) else 0 for d in dam])
103
+ sire_key = np.array([id_to_idx[s] + 1 if pd.notna(s) else 0 for s in sire])
104
+ sort_idx = np.lexsort((sire_key, dam_key, gen))
105
+
106
+ ids_s = ids[sort_idx]
107
+ dam_s = dam[sort_idx]
108
+ sire_s = sire[sort_idx]
109
+
110
+ # numeric pedigree (0-based indices in sorted order)
111
+ id_to_sidx = {v: i for i, v in enumerate(ids_s)}
112
+ dam_idx = np.array([id_to_sidx[d] if pd.notna(d) else n for d in dam_s])
113
+ sire_idx = np.array([id_to_sidx[s] if pd.notna(s) else n for s in sire_s])
114
+
115
+ # --- Meuwissen & Luo: f and dii ---
116
+ f = np.zeros(n + 1)
117
+ f[n] = -1.0
118
+ dii = np.zeros(n)
119
+ AN = np.empty(2 * n, dtype=float)
120
+ li = np.zeros(n)
121
+
122
+ for k in range(n):
123
+ dii[k] = 0.5 - 0.25 * (f[dam_idx[k]] + f[sire_idx[k]])
124
+
125
+ if k > 0 and dam_idx[k] == dam_idx[k-1] and sire_idx[k] == sire_idx[k-1]:
126
+ f[k] = f[k-1]
127
+ else:
128
+ li[k] = 1.0
129
+ ai = 0.0
130
+ j = k
131
+ cnt = 0
132
+ while j >= 0:
133
+ sj = sire_idx[j]
134
+ dj = dam_idx[j]
135
+ if sj < n:
136
+ AN[cnt] = sj
137
+ li[sj] += 0.5 * li[j]
138
+ cnt += 1
139
+ if dj < n:
140
+ AN[cnt] = dj
141
+ li[dj] += 0.5 * li[j]
142
+ cnt += 1
143
+ ai += li[j] * li[j] * dii[j]
144
+ j = -n - 1 # empty sentinel
145
+ for h in range(cnt):
146
+ if AN[h] > j:
147
+ j = int(AN[h])
148
+ for h in range(cnt):
149
+ if AN[h] == j:
150
+ AN[h] = -n - 1
151
+ f[k] = ai - 1.0
152
+ li[:k+1] = 0.0
153
+
154
+ # --- Tinv and assembly ---
155
+ Tinv = np.eye(n)
156
+ for i in range(n):
157
+ if dam_idx[i] < n:
158
+ Tinv[i, dam_idx[i]] = -0.5
159
+ if sire_idx[i] < n:
160
+ Tinv[i, sire_idx[i]] = -0.5
161
+
162
+ sqrt_dii_inv = np.diag(np.sqrt(1.0 / dii))
163
+ L = np.linalg.solve(sqrt_dii_inv @ Tinv, np.eye(n))
164
+ A = L @ L.T
165
+
166
+ # --- reorder to original pedigree order ---
167
+ inv_sort = np.argsort(sort_idx)
168
+ A = A[np.ix_(inv_sort, inv_sort)]
169
+
170
+ return A
171
+
172
+ def D_pedigree(pedigree: pd.DataFrame) -> np.ndarray:
173
+ '''
174
+ This is a direct retranscription of the makeD
175
+ function from the R paclage: nadiv
176
+ pedigree is a pandas dataframe with 3 columns:
177
+ - ID: the individual
178
+ - DAM and SIRE: its parents, may be NA
179
+
180
+ NB1: makeD is an approximation, it is only exact
181
+ in the absence of inbreeding.
182
+
183
+ NB2: whenever one or the other parent is missing,
184
+ the individual is treated as a founder (both
185
+ parents missing)
186
+ '''
187
+ A = A_pedigree(pedigree)
188
+
189
+ ped = pedigree.copy()
190
+ ped.columns = ped.columns.str.lower()
191
+
192
+ ids = ped["id"].values
193
+ dam = ped["dam"].values.copy()
194
+ sire = ped["sire"].values.copy()
195
+
196
+ # force founders when only one parent is known
197
+ only_dam = pd.notna(dam) & pd.isna(sire)
198
+ only_sire = pd.isna(dam) & pd.notna(sire)
199
+ dam[only_dam] = None
200
+ sire[only_sire] = None
201
+
202
+ id_to_idx = {v: i for i, v in enumerate(ids)}
203
+ n = len(ids)
204
+
205
+ dam_idx = np.array([id_to_idx[d] if pd.notna(d) else -1 for d in dam])
206
+ sire_idx = np.array([id_to_idx[s] if pd.notna(s) else -1 for s in sire])
207
+
208
+ D = np.zeros((n, n))
209
+ half_A = A / 2.0
210
+
211
+ for k in range(n):
212
+ if dam_idx[k] == -1:
213
+ continue
214
+ for j in range(k):
215
+ if dam_idx[j] == -1:
216
+ continue
217
+ dk, sk = dam_idx[k], sire_idx[k]
218
+ dj, sj = dam_idx[j], sire_idx[j]
219
+ val = half_A[dk, dj] * half_A[sk, sj] + half_A[dk, sj] * half_A[sk, dj]
220
+ if val != 0.0:
221
+ D[k, j] = val
222
+ D[j, k] = val
223
+
224
+ np.fill_diagonal(D, 2.0 - np.diag(A))
225
+ return D
pyreml/larix.py ADDED
@@ -0,0 +1,7 @@
1
+ import pandas as pd
2
+ import os
3
+
4
+ _pkg_dir = os.path.dirname(__file__)
5
+ _data_dir = os.path.join(_pkg_dir, "data")
6
+
7
+ larix = pd.read_csv(os.path.join(_data_dir, "Larix.csv"))
pyreml/nearPD.py ADDED
@@ -0,0 +1,19 @@
1
+ import torch
2
+ import numpy as np
3
+
4
+ def nearPD(S, eps: float = 1e-4):
5
+ """
6
+ Nearest positive-definite surrogate of a symmetric matrix.
7
+
8
+ Symmetrize, clamp the eigenvalues at `eps`, rebuild, symmetrize again.
9
+ Guarantees strictly positive eigenvalues, which is what the factor-analytic
10
+ initialization relies on (the q dominant eigenvalues feed Lambda > 0).
11
+ """
12
+ was_numpy = isinstance(S, np.ndarray)
13
+ S = torch.as_tensor(S, dtype=torch.double)
14
+ S = (S + S.T) / 2
15
+ eigvals, eigvecs = torch.linalg.eigh(S)
16
+ eigvals = torch.clamp(eigvals, min=eps)
17
+ S = (eigvecs * eigvals) @ eigvecs.T
18
+ S = (S + S.T) / 2
19
+ return S.numpy() if was_numpy else S
@@ -0,0 +1,37 @@
1
+ Metadata-Version: 2.4
2
+ Name: pyreml
3
+ Version: 0.0.0
4
+ Requires-Python: >=3.10
5
+ Description-Content-Type: text/markdown
6
+ Requires-Dist: torch
7
+ Requires-Dist: numpy
8
+ Requires-Dist: scipy
9
+ Requires-Dist: pandas
10
+ Requires-Dist: patsy
11
+ Requires-Dist: pyyaml
12
+ Provides-Extra: test
13
+ Requires-Dist: pytest; extra == "test"
14
+
15
+ # Pyreml
16
+
17
+ **pyreml** is a general purpose mixed model solver under active development.
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ pip install pyreml
23
+ ```
24
+
25
+ ## Documentation
26
+
27
+ Read the manual for further documentation.
28
+
29
+ ## License
30
+
31
+ Copyright 2026 Alexandre Marchal, Daniele Raimondi
32
+
33
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
34
+
35
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
36
+
37
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,9 @@
1
+ pyreml/__init__.py,sha256=onOlg5kSyKO9nUUgN6RN8Og0Nu2dkyRAttNfwPfbXxQ,108
2
+ pyreml/kinships.py,sha256=8kYUTwHjpfSvWskDsbCRHkVL_IuExHCjFSApvdFCNuE,6995
3
+ pyreml/larix.py,sha256=v6FLFHCA2VuumesZ498Zdv5anHKHRjuaodba0xtsgzQ,169
4
+ pyreml/nearPD.py,sha256=nhTi1k7960zEPDVVsXrKalr4NfmQn6h9PplH0i-Pr8w,678
5
+ pyreml/data/Larix.csv,sha256=sB5TL2F0Lj4_orNZOW6mKsypHUP5Jprwp2FriO5OP5c,10914
6
+ pyreml-0.0.0.dist-info/METADATA,sha256=cOVIxsHIVIK8qzU7M1GgT9B5MJPd4m0dMY0E1v93XWg,1597
7
+ pyreml-0.0.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
8
+ pyreml-0.0.0.dist-info/top_level.txt,sha256=JTwN14yYKYIc8XQPqTyuBEPEXJWwM1wiqQ5PyyxIYv8,7
9
+ pyreml-0.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ pyreml