-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathQMPopulationStudy.php
240 lines (239 loc) · 8.21 KB
/
QMPopulationStudy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/*
* GNU General Public License v3.0
* Contributors: ADD YOUR NAME HERE, Mike P. Sinn
*/
namespace App\Studies;
use App\Buttons\StudyButton;
use App\VariableRelationships\QMGlobalVariableRelationship;
use App\VariableRelationships\QMVariableRelationship;
use App\VariableRelationships\QMUserVariableRelationship;
use App\Exceptions\NotEnoughDataException;
use App\Models\GlobalVariableRelationship;
use App\Models\Study;
use App\Properties\Study\StudyIdProperty;
use App\Properties\Study\StudyTypeProperty;
use App\Storage\S3\S3Public;
use App\Traits\HasCorrelationCoefficient;
use App\Traits\HasModel\HasGlobalVariableRelationship;
use App\UI\IonIcon;
use Illuminate\View\View;
use App\Slim\Model\User\QMUser;
/** Class PopulationStudy
* @package app/Studies
*/
class QMPopulationStudy extends QMStudy {
use HasGlobalVariableRelationship;
const TYPE = StudyTypeProperty::TYPE_POPULATION;
public const COLLECTION_NAME = "PopulationStudy";
public const CLASS_PARENT_CATEGORY = Study::CLASS_CATEGORY;
public static function getS3Bucket():string{return S3Public::getBucketName();}
public const UNIQUE_INDEX_COLUMNS = [
self::FIELD_CAUSE_VARIABLE_ID,
self::FIELD_EFFECT_VARIABLE_ID,
self::FIELD_TYPE,
//self::FIELD_USER_ID // Must be unique or we have duplicate study id's
];
/**
* @param Study|null $study
*/
public function __construct(Study $study = null){
if(!$study){return;}
$this->setType(StudyTypeProperty::TYPE_POPULATION);
$study->type = StudyTypeProperty::TYPE_POPULATION;
parent::__construct($study);
}
/**
* @param null $causeNameOrId
* @param null $effectNameOrId
* @param int|null $userId
* @param string|null $type
* @return QMCohortStudy|QMPopulationStudy
*/
public static function findOrCreateQMStudy($causeNameOrId = null,
$effectNameOrId = null,
int $userId = null,
string $type = null): QMStudy {
$study = parent::findOrCreateQMStudy($causeNameOrId, $effectNameOrId, $userId, StudyTypeProperty::TYPE_POPULATION);
return $study;
}
/**
* @param null $causeNameOrId
* @param null $effectNameOrId
* @param int|null $userId
* @param string|null $type
* @return QMCohortStudy|QMPopulationStudy
*/
public static function findOrNewQMStudy($causeNameOrId = null,
$effectNameOrId = null,
int $userId = null,
string $type = null): QMStudy {
$study = parent::findOrNewQMStudy($causeNameOrId, $effectNameOrId, $userId, StudyTypeProperty::TYPE_POPULATION);
return $study;
}
/**
* @param null $causeNameOrId
* @param null $effectNameOrId
* @param int|null $userId
* @param string|null $type
* @return QMCohortStudy|QMPopulationStudy
*/
public static function findInMemoryOrNewQMStudy($causeNameOrId = null,
$effectNameOrId = null,
int $userId = null,
string $type = null): QMStudy {
$study = parent::findInMemoryOrNewQMStudy($causeNameOrId, $effectNameOrId, $userId, StudyTypeProperty::TYPE_POPULATION);
return $study;
}
/**
* @return int
*/
public function getUserId(): ?int {
return $this->userId ?: QMStudy::DEFAULT_PRINCIPAL_INVESTIGATOR_ID;
}
/**
* @param string|int $causeNameOrId
* @param string|int $effectNameOrId
* @param int|null $userId
* @param string|null $type
* @return string
*/
public static function generateStudyId($causeNameOrId, $effectNameOrId, int $userId = null, string $type = null): string{
$clientId = StudyIdProperty::generateStudyIdPrefix($causeNameOrId, $effectNameOrId).
StudyIdProperty::POPULATION_STUDY_ID_SUFFIX;
return $clientId;
}
/**
* @param array $arr
* @param string|null $reason
* @return int
* @deprecated Use Eloquent model save directly
*/
public function updateDbRow(array $arr, string $reason = null): int {
unset($arr[self::FIELD_USER_ID]); // Need to unset user_id this because it tries to overwrite with same user_id which violates index for some reason
return parent::updateDbRow($arr, $reason);
}
/**
* @return QMGlobalVariableRelationship
*/
public function setHasCorrelationCoefficientFromDatabase(): ?QMGlobalVariableRelationship{
return $this->correlationFromDatabase =
QMGlobalVariableRelationship::getByIds($this->getCauseVariableId(), $this->getEffectVariableId());
}
/**
* @return QMGlobalVariableRelationship|QMUserVariableRelationship
* @throws NotEnoughDataException
*/
public function getCreateOrRecalculateStatistics(): QMVariableRelationship{
if($c = $this->getHasCorrelationCoefficientIfSet()){
return $c;
}
$c = $this->getOrCreateGlobalVariableRelationship();
return $this->setStatistics($c);
}
/**
* @return string
*/
public function getCategoryDescription(): string{
return GlobalVariableRelationship::CLASS_DESCRIPTION;
}
/**
* @return QMVariableRelationship
* @throws NotEnoughDataException
*/
public function createStatistics(): QMVariableRelationship{
return $this->createGlobalVariableRelationship();
}
/**
* @return GlobalVariableRelationship|HasCorrelationCoefficient
* @throws \App\Exceptions\DuplicateFailedAnalysisException
* @throws \App\Exceptions\ModelValidationException
* @throws \App\Exceptions\NotEnoughDataException
* @throws \App\Exceptions\StupidVariableNameException
* @throws \App\Exceptions\TooSlowToAnalyzeException
*/
public function getHasCorrelationCoefficient(){
$c = $this->findGlobalVariableRelationship();
if(!$c){
$c = $this->createGlobalVariableRelationship();
}
return $c;
}
/**
* @return string
*/
public function getTitleWithUserName(): string {
return $this->getTitleAttribute()." for Population";
}
public function getQMUser(): QMUser{
return QMUser::population();
}
public function getIonIcon(): string {
return IonIcon::androidGlobe;
}
public static function getIndexPageView(): View{
return view('studies-index', [
'buttons' => static::generateIndexButtons(),
'heading' => "Population Studies"
]);
}
public static function generateIndexButtons(): array{
$correlations = GlobalVariableRelationship::getUpVoted();
return StudyButton::toButtons($correlations);
}
public function getShowContentView(array $params = []): View{
try {
$this->getHasCorrelationCoefficient();
} catch (NotEnoughDataException $e) {
$this->logError($e->getMessage());
}
return view('population-study-content', $this->getShowParams($params));
}
public function getShowPageView(array $params = []): View{
try {
$this->getHasCorrelationCoefficient();
} catch (NotEnoughDataException $e) {
$this->logError($e->getMessage());
}
return view('population-study', $this->getShowParams($params));
}
public function getUrlSubPath(): string{
return $this->getId();
}
public function getStudyType(): string{
return StudyTypeProperty::TYPE_POPULATION;
}
/**
* @param int|null $precision
* @return float|null
*/
public function getCorrelationCoefficient(int $precision = null): ?float{
$c = $this->findGlobalVariableRelationship();
if(!$c){return null;}
return $c->getCorrelationCoefficient($precision);
}
/**
* @return \App\Models\GlobalVariableRelationship
*/
public function findGlobalVariableRelationship():?GlobalVariableRelationship{
if($this->statistics === false){
return null;
}
if($this->statistics instanceof GlobalVariableRelationship){
return $this->statistics;
}
if($this->statistics instanceof QMGlobalVariableRelationship){
return $this->statistics->l();
}
if($this->statistics instanceof NotEnoughDataException){
return null;
}
$c = GlobalVariableRelationship::findByVariableNamesOrIds($this->getCauseVariableId(),
$this->getEffectVariableId());
if(!$c){
$this->statistics = false;
return null;
}
return $this->statistics = $c;
}
}