Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

""" 

+-------------------------------------------+\ 

--------------+-------------+ 

|Agent class |\ 

Type of agent | Rendering | 

+===========================================+\ 

==============+=============+ 

|:class:`navipy.moving.agent.CyberBeeAgent` |\ 

Close loop |Online | 

+-------------------------------------------+\ 

+-------------+ 

|:class:`navipy.moving.agent.GraphAgent` |\ 

|Pre-rendered | 

+-------------------------------------------+\ 

--------------+ + 

|:class:`navipy.moving.agent.GridAgent` |\ 

Open loop | | 

+-------------------------------------------+\ 

--------------+-------------+ 

 

 

""" 

import numpy as np 

import pandas as pd 

import copy 

import networkx as nx 

import multiprocessing 

from multiprocessing import Queue, JoinableQueue, Process 

import inspect 

from navipy.moving import maths as navimomath 

from navipy.database import DataBase 

import time 

import os 

 

version = float(nx.__version__) 

 

 

def defaultcallback(*args, **kwargs): 

"""default call back""" 

raise NameError('No Callback') 

 

 

class DefaultBrain(): 

def __init__(self): 

pass 

 

def update(self, posorient): 

raise NameError('No Callback') 

 

def velocity(self): 

raise NameError('No Callback') 

 

 

def posorient_columns(convention): 

return [('location', 'x'), 

('location', 'y'), 

('location', 'z'), 

(convention, 'alpha_0'), 

(convention, 'alpha_1'), 

(convention, 'alpha_2')] 

 

 

def velocities_columns(convention): 

return [('location', 'dx'), 

('location', 'dy'), 

('location', 'dz'), 

(convention, 'dalpha_0'), 

(convention, 'dalpha_1'), 

(convention, 'dalpha_2')] 

 

 

class AbstractAgent(): 

def __init__(self, convention='zyx'): 

self._brain = DefaultBrain() 

self._alter_posorientvel = defaultcallback 

tuples = posorient_columns(convention) 

index = pd.MultiIndex.from_tuples(tuples, 

names=['position', 

'orientation']) 

self._posorient_col = index 

 

tuples_vel = velocities_columns(convention) 

index_vel = pd.MultiIndex.from_tuples(tuples_vel, 

names=['position', 

'orientation']) 

tuples_posvel = tuples 

tuples_posvel.extend(tuples_vel) 

index_posvel = pd.MultiIndex.from_tuples(tuples_posvel, 

names=['position', 

'orientation']) 

self._velocity_col = index_vel 

self._posorient_vel_col = index_posvel 

self._posorient_vel = pd.Series( 

index=self._posorient_vel_col, 

data=np.nan) 

 

@property 

def posorient(self): 

return self._posorient_vel.loc[self._posorient_col].copy() 

 

@posorient.setter 

def posorient(self, posorient): 

if isinstance(posorient, pd.Series) is False: 

raise TypeError('posorient should be a pandas Series') 

for col in self._posorient_col: 

if col not in posorient.index: 

raise KeyError( 

'posorient should have {} as index'.format(col)) 

self._posorient_vel.loc[self._posorient_col] = \ 

posorient.loc[self._posorient_col] 

 

@property 

def velocity(self): 

return self._posorient_vel.loc[self._velocity_col].copy() 

 

@velocity.setter 

def velocity(self, velocity): 

if isinstance(velocity, pd.Series) is False: 

raise TypeError('velocity should be a pandas Series') 

for col in self._velocity_col: 

if col not in velocity.index: 

raise KeyError( 

'velocity should have {} as index'.format(col)) 

self._posorient_vel.loc[self._velocity_col] = \ 

velocity.loc[self._velocity_col] 

 

@property 

def posorient_vel(self): 

return self._posorient_vel.copy() 

 

@posorient_vel.setter 

def posorient_vel(self, posorient_vel): 

self.posorient = posorient_vel 

self.velocity = posorient_vel 

 

@property 

def brain(self): 

return inspect.getsourcelines(self._brain) 

 

@brain.setter 

def brain(self, brain): 

self._brain = brain 

 

@property 

def alter_posorientvel(self): 

return inspect.getsourcelines(self._alter_posorientvel) 

 

def move(self): 

self._brain.update(self.posorient) 

self.velocity = self._brain.velocity() 

alteredpos = self._alter_posorientvel(self._posorient_vel) 

self.posorient = alteredpos 

self.velocity = alteredpos 

 

def fly(self, max_nstep, return_tra=False): 

"""move cyberbee until max step has been performed 

""" 

if return_tra: 

trajectory = pd.DataFrame(index=range(0, max_nstep), 

columns=self._posorient_vel_col) 

trajectory.loc[0, :] = self._posorient_vel.copy() 

for stepi in range(1, max_nstep): 

self.move() 

if return_tra: 

trajectory.loc[stepi, :] = self._posorient_vel.copy() 

if return_tra: 

return trajectory 

else: 

return None 

 

 

class CyberBeeAgent(AbstractAgent, Process): 

""" 

A CyberBeeAgent uses the rendering method of cyberbee. \ 

CyberBeeAgent is a close loop agent and need to be run within blender \ 

(see :doc:`rendering`). 

 

Single process 

Here come example of how to use it 

 

Multi process 

CyberBeeAgent inherit from the Process \ 

class of the multiprocessing module of the standard python \ 

library. Thus, several GridAgents can safely be run in parallel. 

 

""" 

 

def __init__(self, brain, convention, 

posorients_queue=None, 

results_queue=None): 

if convention is None: 

raise Exception("a convention must be specified") 

if (posorients_queue is not None) and (results_queue is not None): 

multiprocessing.Process.__init__(self) 

AbstractAgent.__init__(self, convention) 

AbstractAgent._alter_posorientvel = \ 

lambda motion_vec: navimomath.next_pos(motion_vec, 

move_mode='free_run') 

self._alter_posorientvel = \ 

lambda motion_vec: navimomath.next_pos(motion_vec, 

move_mode='free_run') 

self.brain = brain 

self._posorients_queue = posorients_queue 

self._results_queue = results_queue 

 

def run(self): 

""" Only supported when multiprocess""" 

if self._posorients_queue is None or self._results_queue is None: 

raise NameError('Single agent class has not be inititialised ' 

+ 'with multiprocessing suppport') 

proc_name = self.name 

print('Process {} started'.format(proc_name)) 

while True: 

start_posorient = self._posorients_queue.get(timeout=1) 

if start_posorient is None: 

# Poison pill means shutdown) 

break 

common_id = list(set(start_posorient.index).intersection( 

self._posorient_vel.index)) 

self._posorient_vel.loc[common_id] = start_posorient.loc[common_id] 

self._posorient_vel.name = start_posorient.name 

self.move() 

posorient_vel = self._posorient_vel 

self._posorients_queue.task_done() 

self._results_queue.put(posorient_vel) 

self._posorients_queue.task_done() 

print('Process {} done'.format(proc_name)) 

 

 

class GridAgent(AbstractAgent, Process): 

""" 

A GridAgent fetches the scene from a pre-rendered database. \ 

(see :doc:`database`) 

GridAgent is a close loop agent here its position is snap to a grid. 

 

Single process 

Here come example of how to use it 

 

Multi process 

GridAgent inherit from the Process \ 

class of the multiprocessing module of the standard python \ 

library. Thus, several GridAgents can safely be run in parallel. 

 

 

""" 

 

def __init__(self, brain, 

posorients_queue=None, 

results_queue=None): 

if not isinstance(brain.renderer, DataBase): 

msg = 'GridAgent only works with a brain having ' 

msg += 'a renderer of type DataBase' 

raise TypeError(msg) 

convention = brain.renderer.rotation_convention 

if (posorients_queue is not None) and (results_queue is not None): 

multiprocessing.Process.__init__(self) 

AbstractAgent.__init__(self, convention) 

self._alter_posorientvel = self.snap_to_grid 

self.brain = brain 

self._posorients_queue = posorients_queue 

self._results_queue = results_queue 

 

@property 

def mode_of_motion(self): 

""" 

""" 

toreturn = self._mode_move 

toreturn['describe'] = \ 

navimomath.mode_moves_supported()[ 

self._mode_move['mode']]['describe'] 

return toreturn 

 

@mode_of_motion.setter 

def mode_of_motion(self, mode): 

""" 

 

""" 

if not isinstance(mode, dict): 

raise TypeError('Mode is not a dictionary') 

if 'mode' not in mode: 

raise KeyError("'mode' is not a key of mode") 

if 'param' not in mode: 

raise KeyError("'param' is not a key of mode") 

if mode['mode'] in navimomath.mode_moves_supported().keys(): 

for param in navimomath.mode_moves_supported()[ 

mode['mode']]['param']: 

if param not in mode['param']: 

raise KeyError( 

"'{}' is not in mode['param']".format(param)) 

self._mode_move = mode 

else: 

raise ValueError('mode is not supported') 

 

def snap_to_grid(self, posorient_vel): 

posorient_vel = navimomath.next_pos( 

posorient_vel, 

move_mode=self._mode_move['mode'], 

move_param=self._mode_move['param']) 

tmppos = self._brain.posorients 

tmp = navimomath.closest_pos( 

posorient_vel, tmppos) # self._brain.posorients) 

posorient_vel.loc[self._posorient_col] = \ 

tmp.loc[self._posorient_col] 

posorient_vel.name = tmp.name 

return posorient_vel 

 

def move(self): 

if hasattr(self, '_mode_move'): 

AbstractAgent.move(self) 

else: 

raise AttributeError( 

'GridAgent object has no attribute _mode_move\n' + 

'Please set the mode of motion') 

 

def fly(self, max_nstep, return_tra=False): 

if hasattr(self, '_mode_move'): 

return AbstractAgent.fly(self, max_nstep, return_tra) 

else: 

raise AttributeError( 

'GridAgent object has no attribute _mode_move\n' + 

'Please set the mode of motion') 

 

def run(self): 

""" Only supported when multiprocess""" 

if self._posorients_queue is None or self._results_queue is None: 

raise NameError('Single agent class has not be inititialised ' 

+ 'with multiprocessing suppport') 

proc_name = self.name 

print('Process {} started'.format(proc_name)) 

while True: 

start_posorient = self._posorients_queue.get(timeout=1) 

if start_posorient is None: 

# Poison pill means shutdown) 

break 

common_id = list(set(start_posorient.index).intersection( 

self._posorient_vel.index)) 

self._posorient_vel.loc[common_id] = start_posorient.loc[common_id] 

self.move() 

next_posorient = self._posorient_vel 

self._posorients_queue.task_done() 

self._results_queue.put((start_posorient, next_posorient)) 

self._posorients_queue.task_done() 

print('Process {} done'.format(proc_name)) 

 

 

class GraphAgent(): 

""" 

A GraphAgent uses, to build a graph, 

 

1. pre-rendered scene from a database to derive \ 

the agent motion, or 

2. pre-computed agent-motion 

 

 

""" 

 

def __init__(self, brain, mode_of_motion): 

self._brain = copy.copy(brain) 

# Init the graph 

self._graph = nx.DiGraph() 

if not isinstance(self._brain.renderer, DataBase): 

msg = 'GraphAgent only works with a brain having ' 

msg += 'a renderer of type DataBase' 

raise TypeError(msg) 

for row_id, posor in self._brain.posorients.iterrows(): 

posor.name = row_id 

self._graph.add_node(row_id, 

posorient=posor) 

self.mode_of_motion = mode_of_motion 

# Create a dataframe to store the velocities 

convention = self._brain.renderer.rotation_convention 

tuples_posvel = posorient_columns(convention) 

tuples_posvel.extend(velocities_columns(convention)) 

index_posvel = pd.MultiIndex.from_tuples(tuples_posvel, 

names=['position', 

'orientation']) 

self.velocities = pd.DataFrame(columns=index_posvel, 

index=list(self._graph.nodes())) 

 

@property 

def graph(self): 

return self._graph 

 

@graph.setter 

def graph(self, graph): 

if isinstance(graph, nx.DiGraph) is False: 

raise TypeError('graph is not a nx.DiGraph') 

self._graph = graph.copy() 

self.check_graph() 

 

def compute_velocities(self, 

ncpu=5, 

timeout=1, 

filename=None, 

blocksize=100): 

if os.path.exists(filename): 

self.velocities = pd.read_hdf(filename) 

nodes_tocompute = self.velocities.isna().any(axis=1) 

nodes_tocompute = nodes_tocompute[nodes_tocompute].index 

# Build a list of nodes 

posorients_queue = JoinableQueue() 

results_queue = Queue() 

for node in nodes_tocompute: 

posorients_queue.put(self._graph.nodes[node]['posorient']) 

 

# Start ndatabase loader 

convention = self._brain.renderer.rotation_convention 

num_agents = ncpu 

agents = [CyberBeeAgent(copy.copy(self._brain), 

convention=convention, 

posorients_queue=posorients_queue, 

results_queue=results_queue) 

for _ in range(num_agents)] 

for w in agents: 

w.start() 

 

# Add a poison pill for each agent 

for _ in range(num_agents): 

posorients_queue.put(None) 

 

# Wait for all of the tasks to finish 

# posorients_queue.join() 

nline = 0 

prev_nline = nline 

t_start = time.time() 

nbnodes = nodes_tocompute.shape[0] 

for _ in range(nbnodes): 

res = results_queue.get(timeout=timeout) 

self.velocities.loc[res.name, res.index] = res 

if (nline - prev_nline) > blocksize: 

t_elapse = time.time() - t_start 

t_peritem = t_elapse / nline 

remain = nbnodes - nline 

print('Computed {} in {}'.format(nline, t_elapse)) 

print('Remain {}, done in {}'.format( 

remain, remain * t_peritem)) 

if filename is not None: 

self.velocities.to_hdf(filename, key='velocities') 

prev_nline = nline 

nline += 1 

return self.velocities.copy() 

 

def build_graph(self, movemode, moveparam): 

""" 

Connect edges with a given velocity 

""" 

if self.velocities.dropna().shape[0] == 0: 

raise NameError('compute_velocities should be called first') 

edges = pd.Series(data=np.nan, index=self.velocities.index) 

# Make sure that the velocity start at the correct location 

posorients = self._brain.posorients 

myvelocities = self.velocities.copy() 

myvelocities = myvelocities.swaplevel(axis=1) 

myvelocities.x = posorients.x 

myvelocities.y = posorients.y 

myvelocities.z = posorients.z 

myvelocities.alpha_0 = posorients.alpha_0 

myvelocities.alpha_1 = posorients.alpha_1 

myvelocities.alpha_2 = posorients.alpha_2 

myvelocities = myvelocities.swaplevel(axis=1) 

for ii, row in myvelocities.iterrows(): 

if np.any(np.isnan(row)): 

continue 

# Move according to user mode of motion 

nposorient = navimomath.next_pos(row, movemode, moveparam) 

# Snap to the closest point 

nextpos_index = navimomath.closest_pos( 

nposorient, myvelocities) 

edges[ii] = nextpos_index.name 

# Format for graph 

validedges = edges.dropna() 

results_edges = np.vstack( 

[validedges.index, 

validedges.values]).transpose() 

# Add to graph 

self._graph.add_edges_from(results_edges) 

self.check_graph() 

 

def check_graph(self): 

self.check_single_target() 

 

def check_single_target(self): 

if version < 2: 

graph_nodes = list(self._graph.nodes()) 

else: 

graph_nodes = list(self._graph.nodes) 

for node in graph_nodes: 

# not connected -> loop not ran 

for count, _ in enumerate(self._graph.neighbors(node)): 

# count == 0 -> connected to one node 

# count == 1 -> connected to two nodes 

if count > 0: 

raise ValueError( 

'Node {} leads to several locations'.format(node)) 

 

def find_attractors(self): 

"""Return a list of node going to each attractor in a graph 

""" 

attractors = list() 

for attractor in nx.attracting_components(self._graph): 

att = dict() 

att['attractor'] = attractor 

attractors.append(att) 

return attractors 

 

def find_attractors_sources(self, attractors=None): 

"""Find all sources going to each attractors 

""" 

if attractors is None: 

attractors = self.find_attractors() 

 

if isinstance(attractors, list) is False: 

raise TypeError('Attractors should be a list of dict') 

elif len(attractors) == 0: 

raise ValueError('No attractors found') 

 

# Check attractor 

for att in attractors: 

keyatt = att.keys() 

if 'attractor' not in keyatt: 

raise ValueError( 

'Each attractors should contain the key attractor') 

 

# Calculate connection 

for att_i, att in enumerate(attractors): 

 

# [0] because one node of the attractor is enough 

# all other node of the attractor are connected to this one 

target = list(att['attractor'])[0] 

attractors[att_i]['paths'] = nx.shortest_path( 

self.graph, target=target) 

attractors[att_i]['sources'] = list( 

attractors[att_i]['paths'].keys()) 

return attractors 

 

def catchment_area(self, attractors=None): 

"""Return the catchment area for attractors 

""" 

if attractors is None: 

attractors = self.find_attractors_sources() 

 

if isinstance(attractors, list) is False: 

raise TypeError('Attractors should be a list of dict') 

elif len(attractors) == 0: 

raise ValueError('No attractors found') 

 

# Check attractor 

for att in attractors: 

keyatt = att.keys() 

if 'sources' not in keyatt: 

raise ValueError( 

'Each attractors should contains a list of sources') 

 

return [len(att['sources']) for att in attractors] 

 

def reach_goals(self, goals): 

""" Return all paths to the goals """ 

return nx.shortest_path(self._graph, target=goals) 

 

def neighboring_nodes(self, target): 

""" Return the nodes going to the target """ 

# Reverse graph because nx.neighbors give the end node 

# and we want to find the start node going to target 

# not where target goes. 

tmpgraph = self._graph.reverse(copy=True) 

neighbors = tmpgraph.neighbors(target) 

return neighbors