y_petri 2.1.9 → 2.1.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 70791aeefd6b43e56ccdbf4fb980d51768e4ecec
4
- data.tar.gz: d3b852100353e1df976a7c8f3ffd84e0cd2c6dce
3
+ metadata.gz: 1cac00b355a03f2d444b9a4937721d9ad895666d
4
+ data.tar.gz: 473f087960a3a4ed4b93cb2bef5262f0a4e8268e
5
5
  SHA512:
6
- metadata.gz: a03c5f1c57429be512c5e023e99a92a9973c7ad94df76f75d04cdca96e02fa530d91383ea360c06a4e354d3a8824b8b6885d3ff76bb4d3a553e395813aa321ba
7
- data.tar.gz: 844311666d998f34bec07216f7ea0cd37233b0f4309ed88860dcda7f4dfc44e61931c08e246f024f4fee33741dbbe32ac443cca0829e7bdb9893e66fbfa127a4
6
+ metadata.gz: 3c453dbdc36a679375cd5b0439a19ba12c0346847632997fd82c2bffca63827b41d4703454bdef91df863749ba1b933fa7b2987db7f9ec3c4ba8ae71559c5da8
7
+ data.tar.gz: d2e5603eb81a1633813beef6938fcccacf4eba4d7b1b8ce55ccda511763e1a414b1b5be671de54a3480add0d2f6a260d612aedafbb6a0943ca8330db1eb8fc80
@@ -280,26 +280,68 @@ module YPetri::Agent::SimulationRelated
280
280
  title: "Selected features plot", ylabel: "Marking" )
281
281
  end
282
282
 
283
- # Plot the recorded samples (system state history).
283
+ # Plot system state history.
284
284
  #
285
- def plot_state( place_ids=nil, except: [] )
286
- sim = simulation or return nil
287
- rec = sim.recording
288
- pp = sim.net.pp( place_ids.nil? ? net.pp : place_ids ) -
289
- sim.net.pp( except )
290
- dataset = sim.recording.reduce_features( marking: pp )
291
- dataset.plot
285
+ def plot_state( place_ids=nil, except: [],
286
+ title: "State plot", ylabel: "Marking [µM]",
287
+ **options )
288
+ rec = simulation.recording
289
+ pp = simulation.pp( place_ids ) - simulation.pp( except )
290
+ rec.marking( pp ).plot( title: title, ylabel: ylabel, **options )
292
291
  end
293
292
  alias plot_marking plot_state
294
293
 
295
- # Plot the recorded flux (computed flux history at the sampling points).
294
+ # Plot flux history of TS transitions.
296
295
  #
297
- def plot_flux( transition_ids=nil, except: [], **options )
298
- sim = simulation or return nil
299
- rec = sim.recording
300
- tt = sim.net.tt( transition_ids.nil? ? net.tt : transition_ids ) -
301
- sim.net.tt( except )
302
- dataset = sim.recording.reduce_features( flux: tt )
303
- dataset.plot( title: "Flux plot", ylabel: "Flux [µMⁿ.s⁻¹]" )
296
+ def plot_flux( transition_ids=nil, except: [],
297
+ title: "Flux plot", ylabel: "Flux [µM.s⁻¹]",
298
+ **options )
299
+ rec = simulation.recording
300
+ tt = transition_ids.nil? ? simulation.TS_tt : transition_ids
301
+ tt = simulation.TS_tt( tt )
302
+ tt -= simulation.tt( except )
303
+ rec.flux( tt ).plot( title: title, ylabel: ylabel, **options )
304
+ end
305
+
306
+ # Plot firing history of tS transitions.
307
+ #
308
+ def plot_firing( transition_ids=nil, except: [],
309
+ title: "Firing plot", ylabel: "Firing [µM]",
310
+ **options )
311
+ rec = simulation.recording
312
+ tt = transition_ids.nil? ? simulation.tS_tt : transition_ids
313
+ tt = simulation.tS_tt( tt )
314
+ tt -= simulation.tt( except )
315
+ rec.firing( tt ).plot( title: title, ylabel: ylabel, **options )
316
+ end
317
+
318
+ # Plot gradient history of selected places with respect to a set of T
319
+ # transitions.
320
+ #
321
+ def plot_gradient( place_ids=nil, except: [], transitions: nil,
322
+ title: "Gradient plot", ylabel: "Gradient [µM.s⁻¹]",
323
+ **options )
324
+ rec = simulation.recording
325
+ pp = simulation.pp( place_ids ) - simulation.ee( place_ids )
326
+ tt = transitions.nil? ? simulation.T_tt : transitions
327
+ tt = simulation.T_tt( tt )
328
+ tt -= simulation.ee( except )
329
+ rec.gradient( pp, transitions: tt )
330
+ .plot( title: title, ylabel: ylabel, **options )
331
+ end
332
+
333
+ # Plot delta history of selected places with respect to a set of transitions.
334
+ #
335
+ def plot_delta( place_ids=nil, except: [], transitions: nil,
336
+ title: "Delta plot", ylabel: "Delta [µM]",
337
+ **options )
338
+ options.may_have :delta_time, syn!: :Δt
339
+ rec = simulation.recording
340
+ pp = simulation.pp( place_ids ) - simulation.ee( except )
341
+ tt = transitions.nil? ? simulation.tt : tranisitions
342
+ tt = simulation.tt( tt )
343
+ tt -= simulation.ee( except )
344
+ rec.delta( pp, transitions: tt, delta_time: options[:delta_time] )
345
+ .plot( title: title, ylabel: ylabel, **options )
304
346
  end
305
347
  end # module YPetri::Agent::SimulationRelated
@@ -198,7 +198,7 @@ class YPetri::Net::DataSet < Hash
198
198
  else
199
199
  return reduce_features net.State.delta if args.empty?
200
200
  reduce_features delta: args
201
- end
201
+ end
202
202
  end
203
203
 
204
204
  # Outputs the current recording in CSV format.
@@ -32,7 +32,7 @@ module YPetri::Net::ElementAccess
32
32
  fail TypeError, "No place #{id} in the net!" unless places.include? p
33
33
  end
34
34
  end
35
-
35
+
36
36
  # Returns the net's transition identified by the argument.
37
37
  #
38
38
  def transition id
@@ -11,12 +11,12 @@ class YPetri::Net::State::Feature::Delta < YPetri::Net::State::Feature
11
11
  # First, prepare the hash of instances.
12
12
  hsh = Hash.new do |ꜧ, id|
13
13
  if id.is_a? self then # missing key "id" is a Delta instance
14
- ꜧ[ [ id.place, transitions: id.transitions.sort( &:object_id ) ] ]
14
+ ꜧ[ [ id.place, transitions: id.transitions.sort_by( &:object_id ) ] ]
15
15
  else
16
16
  p = id.fetch( 0 )
17
17
  tt = id.fetch( 1 ).fetch( :transitions ) # value of :transitions key
18
18
  if p.is_a? ç.net.Place and tt.all? { |t| t.is_a? ç.net.Transition }
19
- if tt == tt.sort then
19
+ if tt == tt.sort_by( &:object_id ) then
20
20
  # Cache the instance.
21
21
  ꜧ[ id ] = if tt.all? &:timed? then
22
22
  ç.timed( *id )
@@ -28,7 +28,7 @@ class YPetri::Net::State::Feature::Delta < YPetri::Net::State::Feature
28
28
  "all timed, or all timeless!"
29
29
  end
30
30
  else
31
- ꜧ[ [ p, transitions: tt.sort ] ]
31
+ ꜧ[ [ p, transitions: tt.sort_by( &:object_id ) ] ]
32
32
  end
33
33
  else # convert place and transition ids to places and transitions
34
34
  ꜧ[ [ ç.net.place( p ), transitions: ç.net.transitions( tt ) ] ]
@@ -48,7 +48,14 @@ class YPetri::Net::State::Feature::Delta < YPetri::Net::State::Feature
48
48
  # transition identifiers supplied as +:transitions: parameter.
49
49
  #
50
50
  def timed place, transitions: net.T_tt
51
- __new__( place, transitions: net.T_tt( transitions ) )
51
+ tt = begin
52
+ net.T_tt( transitions )
53
+ rescue TypeError => err
54
+ msg = "Transitions #{transitions} not recognized as timed " +
55
+ "transitions in #{net}! (%s)"
56
+ raise TypeError, msg % err
57
+ end
58
+ __new__( place, transitions: tt )
52
59
  .tap { |inst| inst.instance_variable_set :@timed, true }
53
60
  end
54
61
 
@@ -56,6 +63,13 @@ class YPetri::Net::State::Feature::Delta < YPetri::Net::State::Feature
56
63
  # timeless transition identifiers as +:transitions: parameter.
57
64
  #
58
65
  def timeless place, transitions: net.t_tt
66
+ tt = begin
67
+ net.t_tt( transitions )
68
+ rescue TypeError => err
69
+ msg = "Transitions #{transitions} not recognized as timed " +
70
+ "transitions in #{net}! (%s)"
71
+ raise TypeError, msg % err
72
+ end
59
73
  __new__( place, transitions: net.t_tt( transitions ) )
60
74
  .tap { |inst| inst.instance_variable_set :@timed, false }
61
75
  end
@@ -14,7 +14,15 @@ class YPetri::Net::State::Feature::Firing < YPetri::Net::State::Feature
14
14
  when self then
15
15
  hsh[ id.transition ]
16
16
  when ç.net.Transition then
17
- hsh[ id ] = ç.__new__( id )
17
+ t = begin
18
+ ç.net.tS_transitions( [ id ] ).first
19
+ rescue TypeError => err
20
+ msg = "Transition #{id} not " +
21
+ "recognized as tS transition in " +
22
+ "net #{ç.net}! (%s)"
23
+ raise TypeError, msg % err
24
+ end
25
+ hsh[ id ] = ç.__new__( t )
18
26
  else
19
27
  hsh[ ç.net.transition( id ) ]
20
28
  end
@@ -42,7 +50,7 @@ class YPetri::Net::State::Feature::Firing < YPetri::Net::State::Feature
42
50
  def extract_from arg, **nn
43
51
  case arg
44
52
  when YPetri::Simulation then
45
- arg.send( :tS_transitions, [ transition ] ).firing.first
53
+ arg.send( :tS_transitions, [ transition ] ).first.firing
46
54
  else
47
55
  fail TypeError, "Argument type not supported!"
48
56
  end
@@ -14,7 +14,15 @@ class YPetri::Net::State::Feature::Flux < YPetri::Net::State::Feature
14
14
  when self then
15
15
  hsh[ id.transition ]
16
16
  when ç.net.Transition then
17
- hsh[ id ] = ç.__new__( id )
17
+ t = begin
18
+ ç.net.TS_transitions( [ id ] ).first
19
+ rescue TypeError => err
20
+ msg = "Transition #{id} not " +
21
+ "recognized as TS transition in " +
22
+ "net #{ç.net}! (%s)"
23
+ raise TypeError, msg % err
24
+ end
25
+ hsh[ id ] = ç.__new__( t )
18
26
  else
19
27
  hsh[ ç.net.transition( id ) ]
20
28
  end
@@ -42,7 +50,7 @@ class YPetri::Net::State::Feature::Flux < YPetri::Net::State::Feature
42
50
  def extract_from arg, **nn
43
51
  case arg
44
52
  when YPetri::Simulation then
45
- arg.send( :TS_transitions, [ transition ] ).flux.first
53
+ arg.send( :TS_transitions, [ transition ] ).first.flux
46
54
  else
47
55
  fail TypeError, "Argument type not supported!"
48
56
  end
@@ -11,17 +11,25 @@ class YPetri::Net::State::Feature::Gradient < YPetri::Net::State::Feature
11
11
  # First, prepare the hash of instances.
12
12
  hsh = Hash.new do |ꜧ, id|
13
13
  if id.is_a? self then
14
- ꜧ[ [ id.place, transitions: id.transitions.sort( &:object_id ) ] ]
14
+ ꜧ[ [ id.place, transitions: id.transitions.sort_by( &:object_id ) ] ]
15
15
  else
16
16
  p = id.fetch( 0 )
17
17
  tt = id
18
18
  .fetch( 1 )
19
19
  .fetch( :transitions )
20
20
  if p.is_a? ç.net.Place and tt.all? { |t| t.is_a? ç.net.Transition }
21
- if tt == tt.sort then
21
+ tt_sorted = tt.sort_by &:object_id
22
+ if tt == tt_sorted then
23
+ tt = begin
24
+ ç.net.T_transitions( tt )
25
+ rescue TypeError => err
26
+ msg = "Transitions #{tt} not recognized as T " +
27
+ "transitions in net #{ç.net}! (%s)"
28
+ raise TypeError, msg % err
29
+ end
22
30
  ꜧ[ id ] = ç.__new__( *id )
23
31
  else
24
- ꜧ[ [ p, transitions: tt.sort ] ]
32
+ ꜧ[ [ p, transitions: tt.sort_by( &:object_id ) ] ]
25
33
  end
26
34
  else
27
35
  ꜧ[ [ ç.net.place( p ), transitions: ç.net.transitions( tt ) ] ]
@@ -14,6 +14,13 @@ class YPetri::Net::State::Feature::Marking < YPetri::Net::State::Feature
14
14
  when self then
15
15
  hsh[ id.place ]
16
16
  when ç.net.Place then
17
+ p = begin
18
+ ç.net.place id
19
+ rescue TypeError => err
20
+ msg = "Place #{id} not recognized " +
21
+ "a place in net #{ç.net}! (%s)"
22
+ raise TypeError, msg % err
23
+ end
17
24
  hsh[ id ] = ç.__new__( id )
18
25
  else
19
26
  hsh[ ç.net.place( id ) ]
@@ -49,7 +49,9 @@ class YPetri::Net::State::Feature
49
49
  when Gradient() then id
50
50
  when Gradient then
51
51
  Gradient().of( id.place, transitions: id.transitions )
52
- else Gradient().of( id, transitions: transitions ) end # assume it's a place
52
+ else
53
+ Gradient().of( id, transitions: transitions )
54
+ end # assume it's a place
53
55
  end
54
56
 
55
57
  def Flux id=L!
@@ -57,7 +59,9 @@ class YPetri::Net::State::Feature
57
59
  case id
58
60
  when Flux() then id
59
61
  when Flux then Flux().of( id.transition )
60
- else Flux().of( id ) end # assume it's a place
62
+ else
63
+ Flux().of( id )
64
+ end # assume it's a place
61
65
  end
62
66
 
63
67
  def Delta id=L!, transitions: net.tt
data/lib/y_petri/net.rb CHANGED
@@ -1,4 +1,4 @@
1
- #encoding: utf-8
1
+ # encoding: utf-8
2
2
 
3
3
  require_relative 'net/element_access'
4
4
  require_relative 'net/visualization'
@@ -22,7 +22,7 @@ class YPetri::Simulation::Elements
22
22
  # Elements of the simulation (belonging to the net).
23
23
  #
24
24
  def ee( ids=nil )
25
- elements( ids ).sources
25
+ elements( ids ).map &:source
26
26
  end
27
27
 
28
28
  # Names of the simulation's elements. Arguments, if any, are treated
@@ -1,4 +1,4 @@
1
- #encoding: utf-8
1
+ # encoding: utf-8
2
2
 
3
3
  # A mixin for TS transition representations.
4
4
  #
@@ -6,16 +6,16 @@ class YPetri::Simulation::TransitionRepresentation
6
6
  module Type_TS
7
7
  include Type_T
8
8
  include Type_S
9
-
9
+
10
10
  attr_reader :rate_closure
11
-
11
+
12
12
  # Initialization subroutine.
13
13
  #
14
14
  def init
15
15
  super
16
16
  @rate_closure = to_rate_closure
17
17
  end
18
-
18
+
19
19
  # Transition's rate, given the current system state.
20
20
  #
21
21
  def rate
@@ -23,20 +23,20 @@ class YPetri::Simulation::TransitionRepresentation
23
23
  end
24
24
  alias flux rate
25
25
  alias propensity rate
26
-
26
+
27
27
  # Firing of the transition (rate * Δtime).
28
28
  #
29
29
  def firing Δt
30
30
  rate * Δt
31
31
  end
32
-
32
+
33
33
  # Gradient contribution of the transition to all places.
34
34
  #
35
35
  def ∇
36
36
  codomain >> stoichiometry.map { |coeff| rate * coeff }
37
37
  end
38
38
  alias gradient_all ∇
39
-
39
+
40
40
  # Builds a flux closure.
41
41
  #
42
42
  def to_rate_closure
@@ -1,4 +1,4 @@
1
- #encoding: utf-8
1
+ # encoding: utf-8
2
2
 
3
3
  # Mixin for collections of TS transitions.
4
4
  #
@@ -1,4 +1,4 @@
1
1
  module YPetri
2
- VERSION = "2.1.9"
2
+ VERSION = "2.1.10"
3
3
  DEBUG = false
4
4
  end
data/lib/y_petri.rb CHANGED
@@ -1,4 +1,4 @@
1
- #encoding: utf-8
1
+ # encoding: utf-8
2
2
 
3
3
  require 'active_support/core_ext/module/delegation'
4
4
  require 'active_support/core_ext/array/extract_options'
data/test/agent_test.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #! /usr/bin/ruby
2
- # -*- coding: utf-8 -*-
2
+ # encoding: utf-8
3
3
 
4
4
  gem 'minitest', '=4.7.4'
5
5
  require 'minitest/autorun'
@@ -11,7 +11,7 @@ describe YPetri::Agent do
11
11
  before do
12
12
  @m = YPetri::Agent.new
13
13
  end
14
-
14
+
15
15
  it "has net basic points" do
16
16
  # --- net point related assets ---
17
17
  @m.net_point_reset
@@ -68,7 +68,7 @@ describe YPetri::Agent do
68
68
  @m.tn.must_equal []
69
69
  @m.nn.must_equal [ :Top ] # ie. :Top net spanning whole workspace
70
70
  end
71
-
71
+
72
72
  describe "slightly more complicated case" do
73
73
  before do
74
74
  @p = @m.Place ɴ: "P", default_marking: 1
@@ -81,7 +81,7 @@ describe YPetri::Agent do
81
81
  @m.set_sampling 1
82
82
  @m.set_time 0..30
83
83
  end
84
-
84
+
85
85
  it "works" do
86
86
  @m.run!
87
87
  @m.simulation.send( :places ).map( &:source )
@@ -97,6 +97,15 @@ describe YPetri::Agent do
97
97
  .stoichiometry_matrix.row_size.must_equal 2
98
98
  @m.simulation.flux_vector.row_size.must_equal 2
99
99
  # @m.plot_recording
100
+ rec = @m.simulation.recording
101
+ rec.marking.plot
102
+ rec.flux.plot
103
+ rec.gradient.plot
104
+ rec.delta( Δt: 1 ).plot
105
+ @m.plot_marking
106
+ @m.plot_flux
107
+ @m.plot_gradient
108
+ @m.plot_delta( Δt: 1 )
100
109
  end
101
110
  end
102
111
  end
@@ -288,9 +288,9 @@ describe "timeless simulation" do
288
288
  ds.firing.slice( 1..2 ).series
289
289
  .must_equal [[1, 1]]
290
290
  ds.series( firing: [:U2V] )
291
- .must_equal [[1, 1, 1, 1, 1, 1]]
291
+ .must_equal [ [ 1, 1, 1, 1, 1, 1 ] ]
292
292
  ds.delta( [:U], transitions: [:Uplus] ).series
293
- .must_equal [[1.0, 1.0, 1.0, 1.0, 1.0, 1.0]]
293
+ .must_equal [ [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] ]
294
294
  tmp = ds.reduce_features( marking: [:U], firing: [:U2V] )
295
295
  tmp.features
296
296
  .must_equal( ds.net.State.features marking: [:U], firing: [:U2V] )
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: y_petri
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.9
4
+ version: 2.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - boris
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-10 00:00:00.000000000 Z
11
+ date: 2013-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: y_support