コード例 #1
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     Eloquent::unguard();
     if (!Config::get('gosmart.integrated_patient_database')) {
         \Log::warning("Can only seed simulations if a patient database is available");
         return;
     }
     $this->clean();
     //$this->deepClean();
     $organs = ['liver', 'kidney'];
     foreach ($organs as $organ) {
         $referenceSimulation[$organ] = [];
         $sim = Simulation::join('ItemSet_Patient AS IS_P', 'IS_P.Id', '=', 'Simulation.Patient_Id')->join('ItemSet AS IS', 'IS.Id', '=', 'IS_P.Id')->join('Simulation_Needle AS SN', 'SN.Simulation_Id', '=', 'Simulation.Id')->where('IS_P.OrganType', '=', ContextEnum::value($organ))->where('IS.IsDeleted', '=', 'FALSE')->select('Simulation.*')->first();
         if ($sim) {
             $referenceSimulation[$organ]["patient"] = DB::table('ItemSet_Patient')->whereId($sim->Patient_Id)->first();
             $referenceNeedle = $sim->SimulationNeedles->first();
             $referenceSimulation[$organ]["target"] = $referenceNeedle->Target;
             $referenceSimulation[$organ]["entry"] = $referenceNeedle->Entry;
         } else {
             $referenceSimulation[$organ]["patient"] = NULL;
             $referenceSimulation[$organ]["target"] = PointSet::fromArray([0, 0, 0]);
             $referenceSimulation[$organ]["entry"] = PointSet::fromArray([1, 1, 1]);
         }
     }
     foreach (['5cm', '4cm', '3cm', '2cm'] as $length) {
         $this->makeSimulation("{$length} RITA RFA", $referenceSimulation["liver"]["patient"], 'liver', 'NUMA RFA Basic SIF', "RITA Starburst {$length} Protocol", [], ['organ' => ["organ.vtp"], 'vessels' => ["vessels1.vtp"], 'tumour' => ["tumour.vtp"]], [["Manufacturer" => "RITA", "Name" => "Starburst MRI", "Parameters" => ['NEEDLE_TIP_LOCATION' => $referenceSimulation["liver"]["target"]->asString, 'NEEDLE_ENTRY_LOCATION' => $referenceSimulation["liver"]["entry"]->asString]]]);
     }
     $this->makeSimulation('Cryoablation', $referenceSimulation["kidney"]["patient"], 'kidney', 'NUMA Cryoablation Basic SIF', 'Empty', ['SETTING_FINAL_TIMESTEP' => '300'], ['organ' => ["organ.vtp"], 'vessels' => ["vessels1.vtp"], 'tumour' => ["tumour.vtp"]], [["Manufacturer" => "Galil Medical", "Name" => "IceROD", "Parameters" => ['NEEDLE_TIP_LOCATION' => $referenceSimulation["kidney"]["target"]->asString, 'NEEDLE_ENTRY_LOCATION' => $referenceSimulation["kidney"]["entry"]->asString]]]);
     $needleDeltas = [[10, 8, -5], [10, 8, 5], [10, -8, -5], [10, -8, 5], [10, 5, 0], [10, -5, 0]];
     $ireTipCentre = $referenceSimulation["liver"]["target"]->asArray;
     $ireEntryCentre = $referenceSimulation["liver"]["entry"]->asArray;
     $parallel = array_map(function ($c) use($ireTipCentre, $ireEntryCentre) {
         return $ireTipCentre[$c] - $ireEntryCentre[$c];
     }, [0, 1, 2]);
     $norm = sqrt($parallel[0] * $parallel[0] + $parallel[1] * $parallel[1] + $parallel[2] * $parallel[2]);
     $parallel = array_map(function ($c) use($norm) {
         return $c / $norm;
     }, $parallel);
     $randVec = [0, 1.2384717624, 3.42878E-6];
     $perp1 = crossProduct($parallel, $randVec);
     $perp2 = crossProduct($parallel, $perp1);
     //$parallel = [1, 0, 0];
     //$perp1 = [0, 1, 0];
     //$perp2 = [0, 0, 1];
     $ireNeedles = [];
     foreach ($needleDeltas as $needleDelta) {
         $needleOffset = array_map(function ($c) use($needleDelta, $parallel, $perp1, $perp2) {
             return $needleDelta[0] * $parallel[$c] + $needleDelta[1] * $perp1[$c] + $needleDelta[2] * $perp2[$c];
         }, [0, 1, 2]);
         $needleTip = array_map(function ($p) {
             return $p[0] + $p[1];
         }, array_map(null, $ireTipCentre, $needleOffset));
         $ireNeedle = ["Manufacturer" => "Angiodynamics", "Name" => "Basic", "Parameters" => ['NEEDLE_TIP_LOCATION' => json_encode($needleTip), 'NEEDLE_ENTRY_LOCATION' => json_encode(array_map(function ($p) {
             return $p[0] + $p[1];
         }, array_map(null, $ireEntryCentre, $needleOffset)))]];
         $ireNeedles[] = $ireNeedle;
     }
     $this->makeSimulation('IRE', $referenceSimulation["liver"]["patient"], 'liver', 'NUMA IRE 3D SIF', 'Empty', ['CONSTANT_IRE_POTENTIAL_DIFFERENCES' => "[1300, 1500, 1300, 1900, 1300, 1300, 1300, 1900, 1300]"], ['organ' => ["organ.vtp"], 'vessels' => ["vessels1.vtp"], 'tumour' => ["tumour.vtp"]], $ireNeedles);
     $this->makeSimulation('Amica MWA', $referenceSimulation["kidney"]["patient"], 'kidney', 'NUMA MWA Nonlinear SIF', 'Generic modifiable power', [], ['organ' => ["organ.vtp"], 'vessels' => ["vessels1.vtp"], 'tumour' => ["tumour.vtp"]], [["Manufacturer" => "HS", "Name" => "APK11150T19V5", "Parameters" => ['NEEDLE_TIP_LOCATION' => $referenceSimulation["kidney"]["target"]->asString, 'NEEDLE_ENTRY_LOCATION' => $referenceSimulation["kidney"]["entry"]->asString]]]);
 }
 protected function process_simulations($session, $connection)
 {
     if (!file_exists('/tmp/lastcheck')) {
         $this->error('Create /tmp/lastcheck');
         return;
     }
     $lastRun = \Carbon\Carbon::createFromTimestamp(filemtime('/tmp/lastcheck'), 'Europe/London');
     $this->info('Time of previous check:');
     $this->info($lastRun);
     $lastRun->setTimezone('Europe/Berlin');
     //$lastRun->subMinutes(30);
     $lastRun->subSeconds(9);
     //RMV accounts for time offset at FIT
     $this->info('In Germany: ' . $lastRun);
     touch('/tmp/lastcheck');
     $simulations = Simulation::join('ItemSet as I', 'I.Id', '=', 'Simulation.Id')->where('I.CreationDate', '>', $lastRun)->select('Simulation.*', 'I.CreationDate')->get();
     $counter = new SessionCounter($simulations->lists('Id'), $connection, function ($i) {
         return $this->info($i);
     });
     $simulations->each(function ($s) {
         $this->info($s->CreationDate . ' :: ' . $s->Caption);
     });
     if ($simulations->isEmpty()) {
         $this->info('No new simulations');
         $connection->close();
         return;
     }
     $simulations->each(function ($s) use($session, $counter) {
         $this->info('SIMULATION ' . $s->Id . ' :: ' . $s->Caption);
         $runHere = $s->State != 2 || !$s->isDevelopment();
         $dev = false;
         if ($s->isDevelopment() && $s->State != 2) {
             $this->info('--development run -- we are responsible for this--');
             $s->State = 2;
             $s->Progress = 0;
             $s->save();
             $dev = true;
         }
         if ($runHere) {
             $xml = $s->buildXml($this->httpTransferrerDownloadBase);
             $this->info('Built XML');
             $finished = function () use($s, $counter) {
                 $counter->leave($s->Id);
             };
             $session->call($this->prefix . '.init', [$s->Id])->then(function ($res) use($session, $s, $xml, $finished) {
                 $session->call($this->prefix . '.update_settings_xml', [$s->Id, $xml->saveXML()])->then(function ($res) use($session, $s, $finished) {
                     $session->call($this->prefix . '.finalize', [$s->Id, '.'])->then(function ($res) use($session, $s, $finished) {
                         $session->call($this->prefix . '.start', [$s->Id])->then(function ($res) use($session, $s, $finished) {
                             $this->info('Started ' . $s->Id);
                             $finished();
                         }, function ($err) use($s, $finished) {
                             $this->error($err);
                             $finished();
                         });
                     }, function ($err) use($s, $finished) {
                         $this->error($err);
                         $finished();
                     });
                 }, function ($err) use($s, $finished) {
                     $this->error($err);
                     $finished();
                 });
             }, function ($err) use($s, $finished) {
                 $this->error($err);
                 $finished();
             });
             $this->info('Sent init call');
         }
         if ($dev) {
             Mail::send('emails.started_sim', ['guid' => $s->Id], function ($message) {
                 $message->to('*****@*****.**', 'Phil Weir')->subject('Development simulation started');
             });
         }
     });
 }