rbbt-util 5.35.1 → 5.35.3

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
  SHA256:
3
- metadata.gz: 5efc019e19e967e30d83a08aa3ed45debe5036dac45f644ec0c7195100112e08
4
- data.tar.gz: 1b7f34c9cb59d5936762531df4e004728071a90f4fcf3a4dce009e58071d7ffa
3
+ metadata.gz: 8be0e512e0b2cded02b65df8105bb3504297b703792f86b664d06904443660cd
4
+ data.tar.gz: 568516ecd269492e81fb571a8fff377d2d486d30fb37883081f2d2aa8fbe811c
5
5
  SHA512:
6
- metadata.gz: e0282a5eb3e8ec139b2cfc60face60d9bbd83d3597291df4e37fe9b8c4b2bca78a5fac23eb0c2ed4948d7cc61005c0625d992faaa67e265cd12443ee509f3037
7
- data.tar.gz: a1e058254ad80b23409bb8f4618f14949089d6fbb355228fbf7abf82919d55a4ee86f47cdb1684014079a00f65831a5ea1900e63808fe21e863de3c67e121e86
6
+ metadata.gz: '09a9d4a2508ce0bc3cf9dc7b84071ec35f0fdf9e9ebf7566edb4d302b5af87b393b958475f83969a931a5da23aa82d00ca97ab2c6e63667730049558cf553a75'
7
+ data.tar.gz: ea069e4c4c50bf0f71800d45156945485053afe0c2f91378fa58ce9442f222c99ea3f92ef30e6ae38e8ccce1afbef13e1a5b77d67a8ea75743353587be578fb6
@@ -31,4 +31,23 @@ module RbbtPython
31
31
  tsv
32
32
  end
33
33
 
34
+ def self.list2ruby(list)
35
+ return list unless PyCall::List === list
36
+ list.collect do |e|
37
+ list2ruby(e)
38
+ end
39
+ end
40
+
41
+ def self.numpy2ruby(numpy)
42
+ list2ruby(numpy.tolist)
43
+ end
44
+
45
+ def self.obj2hash(obj)
46
+ hash = {}
47
+ RbbtPython.iterate obj.keys do |k|
48
+ hash[k] = obj[k]
49
+ end
50
+ hash
51
+ end
52
+
34
53
  end
@@ -5,6 +5,16 @@ require 'rbbt/util/python/util'
5
5
  module RbbtPython
6
6
  extend PyCall::Import
7
7
 
8
+ class RbbtPythonException < StandardError; end
9
+
10
+ class Binding
11
+ include PyCall::Import
12
+
13
+ def run(*args, &block)
14
+ instance_exec(*args, &block)
15
+ end
16
+ end
17
+
8
18
  def self.script(text, options = {})
9
19
  Log.debug "Running python script:\n#{text.dup}"
10
20
  text = StringIO.new text unless IO === text
@@ -12,8 +22,13 @@ module RbbtPython
12
22
  end
13
23
 
14
24
  def self.add_path(path)
15
- self.run 'sys' do
16
- sys.path.append path
25
+ begin
26
+ self.run 'sys' do
27
+ sys.path.append path
28
+ end
29
+ rescue
30
+ raise RbbtPythonException,
31
+ "Could not add path #{Misc.fingerprint path} to python sys: " + $!.message
17
32
  end
18
33
  end
19
34
 
@@ -26,19 +41,68 @@ module RbbtPython
26
41
  end
27
42
 
28
43
  def self.init_rbbt
29
- if ! defined?(@@__init_rbbt) || ! @@__init_rbbt
44
+ if ! defined?(@@__init_rbbt_python) || ! @@__init_rbbt_python
30
45
  Log.debug "Loading python 'rbbt' module into pycall RbbtPython module"
31
46
  RbbtPython.add_paths(Rbbt.python.find_all)
32
47
  RbbtPython.pyimport("rbbt")
33
- @@__init_rbbt = true
48
+ @@__init_rbbt_python = true
34
49
  end
35
50
  end
36
51
 
52
+ def self.import_method(module_name, method_name, as = nil)
53
+ RbbtPython.pyfrom module_name, import: method_name
54
+ RbbtPython.method(method_name)
55
+ end
56
+
57
+ def self.call_method(module_name, method_name, *args)
58
+ RbbtPython.import_method(module_name, method_name).call(*args)
59
+ end
60
+
37
61
  def self.exec(script)
38
62
  PyCall.exec(script)
39
63
  end
40
64
 
41
- def self.iterate(iterator, options = {})
65
+ def self.iterate_index(elem, options = {})
66
+ iii :interate_index
67
+ bar = options[:bar]
68
+
69
+ len = PyCall.len(elem)
70
+ case bar
71
+ when TrueClass
72
+ bar = Log::ProgressBar.new nil, :desc => "RbbtPython iterate"
73
+ when String
74
+ bar = Log::ProgressBar.new nil, :desc => bar
75
+ end
76
+
77
+ len.times do |i|
78
+ begin
79
+ yield elem[i]
80
+ bar.tick if bar
81
+ rescue PyCall::PyError
82
+ if $!.type.to_s == "<class 'StopIteration'>"
83
+ break
84
+ else
85
+ raise $!
86
+ end
87
+ rescue
88
+ bar.error if bar
89
+ raise $!
90
+ end
91
+ end
92
+
93
+ Log::ProgressBar.remove_bar bar if bar
94
+ nil
95
+ end
96
+
97
+ def self.iterate(iterator, options = {}, &block)
98
+ if ! iterator.respond_to?(:__next__)
99
+ if iterator.respond_to?(:__iter__)
100
+ iterator = iterator.__iter__
101
+ else
102
+ return iterate_index(iterator, options, &block)
103
+ end
104
+ end
105
+
42
106
  bar = options[:bar]
43
107
 
44
108
  case bar
@@ -50,7 +114,8 @@ module RbbtPython
50
114
 
51
115
  while true
52
116
  begin
53
- yield iterator.__next__
117
+ elem = iterator.__next__
118
+ yield elem
54
119
  bar.tick if bar
55
120
  rescue PyCall::PyError
56
121
  if $!.type.to_s == "<class 'StopIteration'>"
@@ -79,12 +144,12 @@ module RbbtPython
79
144
 
80
145
  def self.run(mod = nil, imports = nil, &block)
81
146
  if mod
82
- if Array === imports
83
- pyfrom mod, :import => imports
84
- elsif Hash === imports
147
+ if Hash === imports
85
148
  pyimport mod, **imports
86
- else
149
+ elsif imports.nil?
87
150
  pyimport mod
151
+ else
152
+ pyfrom mod, :import => imports
88
153
  end
89
154
  end
90
155
 
@@ -124,4 +189,13 @@ module RbbtPython
124
189
  module_eval(&block)
125
190
  end
126
191
  end
192
+
193
+ def self.new_binding
194
+ Binding.new
195
+ end
196
+
197
+ def self.binding_run(binding = nil, *args, &block)
198
+ binding = new_binding
199
+ binding.instance_exec *args, &block
200
+ end
127
201
  end
@@ -13,5 +13,13 @@ class TestPythonUtil < Test::Unit::TestCase
13
13
  assert_equal tsv, new_tsv
14
14
  end
15
15
 
16
+ def test_numpy
17
+ ra = RbbtPython.run :numpy, :as => :np do
18
+ na = np.array([[[1,2,3], [4,5,6]]])
19
+ RbbtPython.numpy2ruby na
20
+ end
21
+ assert_equal 6, ra[0][1][2]
22
+ end
23
+
16
24
  end
17
25
 
@@ -81,5 +81,55 @@ def python_print():
81
81
  end
82
82
  assert defined
83
83
  end
84
+
85
+ def test_iterate
86
+ a2, b2 = nil, nil
87
+ RbbtPython.run :numpy, as: :np do
88
+ a = np.array([1,2])
89
+ a2 = RbbtPython.collect a do |e|
90
+ e * 2
91
+ end
92
+ b = PyCall.tuple([1,2])
93
+ b2 = RbbtPython.collect b do |e|
94
+ e * 2
95
+ end
96
+ end
97
+ assert_equal [2,4], a2
98
+ assert_equal [2,4], b2
99
+ end
100
+
101
+ def test_lambda
102
+ l = PyCall.eval "lambda e: e + 2"
103
+ assert_equal 5, l.(3)
104
+ end
105
+
106
+ def test_binding
107
+ raised = false
108
+ RbbtPython.binding_run do
109
+ pyimport :torch
110
+ pyfrom :torch, import: ["nn"]
111
+ begin
112
+ torch
113
+ rescue
114
+ raised = true
115
+ end
116
+ end
117
+ assert ! raised
118
+
119
+ raised = false
120
+ RbbtPython.binding_run do
121
+ begin
122
+ torch
123
+ rescue
124
+ raised = true
125
+ end
126
+ end
127
+ assert raised
128
+ end
129
+
130
+ def test_import_method
131
+ random = RbbtPython.import_method :torch, :rand, :random
132
+ assert random.call(1).numpy.to_f > 0
133
+ end
84
134
  end
85
135
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbbt-util
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.35.1
4
+ version: 5.35.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miguel Vazquez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-23 00:00:00.000000000 Z
11
+ date: 2023-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -613,113 +613,113 @@ required_rubygems_version: !ruby/object:Gem::Requirement
613
613
  - !ruby/object:Gem::Version
614
614
  version: '0'
615
615
  requirements: []
616
- rubygems_version: 3.1.4
616
+ rubygems_version: 3.1.2
617
617
  signing_key:
618
618
  specification_version: 4
619
619
  summary: Utilities for the Ruby Bioinformatics Toolkit (rbbt)
620
620
  test_files:
621
+ - test/test_helper.rb
622
+ - test/rbbt/entity/test_identifiers.rb
623
+ - test/rbbt/test_resource.rb
624
+ - test/rbbt/test_association.rb
625
+ - test/rbbt/hpc/test_orchestrate.rb
626
+ - test/rbbt/hpc/test_slurm.rb
627
+ - test/rbbt/hpc/orchestrate/test_batches.rb
628
+ - test/rbbt/hpc/orchestrate/test_rules.rb
629
+ - test/rbbt/hpc/orchestrate/test_chains.rb
630
+ - test/rbbt/hpc/test_batch.rb
631
+ - test/rbbt/persist/test_tsv.rb
632
+ - test/rbbt/persist/tsv/test_kyotocabinet.rb
633
+ - test/rbbt/persist/tsv/test_lmdb.rb
634
+ - test/rbbt/persist/tsv/test_tokyocabinet.rb
635
+ - test/rbbt/persist/tsv/test_cdb.rb
636
+ - test/rbbt/persist/tsv/test_leveldb.rb
637
+ - test/rbbt/persist/tsv/test_sharder.rb
638
+ - test/rbbt/test_packed_index.rb
621
639
  - test/rbbt/test_entity.rb
640
+ - test/rbbt/test_fix_width_table.rb
622
641
  - test/rbbt/workflow/test_remote_workflow.rb
642
+ - test/rbbt/workflow/test_doc.rb
643
+ - test/rbbt/workflow/step/test_save_load_inputs.rb
644
+ - test/rbbt/workflow/step/test_dependencies.rb
645
+ - test/rbbt/workflow/test_schedule.rb
623
646
  - test/rbbt/workflow/util/test_archive.rb
624
647
  - test/rbbt/workflow/util/test_orchestrator.rb
625
648
  - test/rbbt/workflow/util/test_data.rb
626
- - test/rbbt/workflow/test_doc.rb
627
- - test/rbbt/workflow/test_schedule.rb
628
- - test/rbbt/workflow/test_step.rb
629
- - test/rbbt/workflow/step/test_dependencies.rb
630
- - test/rbbt/workflow/step/test_save_load_inputs.rb
631
649
  - test/rbbt/workflow/test_task.rb
632
- - test/rbbt/resource/test_path.rb
633
- - test/rbbt/util/test_colorize.rb
650
+ - test/rbbt/workflow/test_step.rb
651
+ - test/rbbt/test_tsv.rb
652
+ - test/rbbt/test_annotations.rb
653
+ - test/rbbt/test_knowledge_base.rb
654
+ - test/rbbt/util/test_migrate.rb
655
+ - test/rbbt/util/test_simpleDSL.rb
656
+ - test/rbbt/util/concurrency/processes/test_socket.rb
657
+ - test/rbbt/util/concurrency/test_processes.rb
658
+ - test/rbbt/util/concurrency/test_threads.rb
659
+ - test/rbbt/util/test_filecache.rb
660
+ - test/rbbt/util/simpleopt/test_get.rb
661
+ - test/rbbt/util/simpleopt/test_parse.rb
662
+ - test/rbbt/util/simpleopt/test_setup.rb
663
+ - test/rbbt/util/test_misc.rb
664
+ - test/rbbt/util/test_excel2tsv.rb
665
+ - test/rbbt/util/test_semaphore.rb
634
666
  - test/rbbt/util/test_procpath.rb
667
+ - test/rbbt/util/R/test_model.rb
668
+ - test/rbbt/util/R/test_eval.rb
669
+ - test/rbbt/util/R/test_plot.rb
670
+ - test/rbbt/util/test_open.rb
671
+ - test/rbbt/util/test_tmpfile.rb
672
+ - test/rbbt/util/test_cmd.rb
673
+ - test/rbbt/util/test_concurrency.rb
674
+ - test/rbbt/util/test_colorize.rb
675
+ - test/rbbt/util/test_config.rb
635
676
  - test/rbbt/util/python/test_util.rb
636
- - test/rbbt/util/misc/test_development.rb
677
+ - test/rbbt/util/test_log.rb
678
+ - test/rbbt/util/test_simpleopt.rb
679
+ - test/rbbt/util/test_python.rb
680
+ - test/rbbt/util/test_chain_methods.rb
637
681
  - test/rbbt/util/misc/test_omics.rb
638
- - test/rbbt/util/misc/test_pipes.rb
639
- - test/rbbt/util/misc/test_serialize.rb
640
- - test/rbbt/util/misc/test_format.rb
641
- - test/rbbt/util/misc/test_communication.rb
642
682
  - test/rbbt/util/misc/test_lock.rb
643
683
  - test/rbbt/util/misc/test_multipart_payload.rb
644
684
  - test/rbbt/util/misc/test_bgzf.rb
645
- - test/rbbt/util/test_concurrency.rb
646
- - test/rbbt/util/test_cmd.rb
647
- - test/rbbt/util/R/test_plot.rb
648
- - test/rbbt/util/R/test_eval.rb
649
- - test/rbbt/util/R/test_model.rb
650
- - test/rbbt/util/test_config.rb
651
- - test/rbbt/util/test_log.rb
652
- - test/rbbt/util/test_simpleDSL.rb
685
+ - test/rbbt/util/misc/test_development.rb
686
+ - test/rbbt/util/misc/test_serialize.rb
687
+ - test/rbbt/util/misc/test_format.rb
688
+ - test/rbbt/util/misc/test_communication.rb
689
+ - test/rbbt/util/misc/test_pipes.rb
653
690
  - test/rbbt/util/log/test_progress.rb
654
- - test/rbbt/util/test_tmpfile.rb
655
691
  - test/rbbt/util/test_R.rb
656
- - test/rbbt/util/test_excel2tsv.rb
657
- - test/rbbt/util/test_misc.rb
658
- - test/rbbt/util/test_open.rb
659
- - test/rbbt/util/test_simpleopt.rb
660
- - test/rbbt/util/simpleopt/test_parse.rb
661
- - test/rbbt/util/simpleopt/test_setup.rb
662
- - test/rbbt/util/simpleopt/test_get.rb
663
- - test/rbbt/util/test_python.rb
664
- - test/rbbt/util/test_filecache.rb
665
- - test/rbbt/util/concurrency/test_processes.rb
666
- - test/rbbt/util/concurrency/test_threads.rb
667
- - test/rbbt/util/concurrency/processes/test_socket.rb
668
- - test/rbbt/util/test_semaphore.rb
669
- - test/rbbt/util/test_chain_methods.rb
670
- - test/rbbt/util/test_migrate.rb
671
- - test/rbbt/test_resource.rb
672
- - test/rbbt/test_packed_index.rb
673
- - test/rbbt/tsv/test_change_id.rb
674
- - test/rbbt/tsv/test_attach.rb
675
- - test/rbbt/tsv/test_filter.rb
676
- - test/rbbt/tsv/test_marshal.rb
692
+ - test/rbbt/test_workflow.rb
693
+ - test/rbbt/knowledge_base/test_entity.rb
694
+ - test/rbbt/knowledge_base/test_enrichment.rb
695
+ - test/rbbt/knowledge_base/test_traverse.rb
696
+ - test/rbbt/knowledge_base/test_syndicate.rb
697
+ - test/rbbt/knowledge_base/test_registry.rb
698
+ - test/rbbt/knowledge_base/test_query.rb
699
+ - test/rbbt/association/test_item.rb
700
+ - test/rbbt/association/test_util.rb
701
+ - test/rbbt/association/test_open.rb
702
+ - test/rbbt/association/test_database.rb
703
+ - test/rbbt/association/test_index.rb
704
+ - test/rbbt/annotations/test_util.rb
705
+ - test/rbbt/test_monitor.rb
706
+ - test/rbbt/resource/test_path.rb
707
+ - test/rbbt/test_hpc.rb
708
+ - test/rbbt/test_persist.rb
677
709
  - test/rbbt/tsv/test_parser.rb
678
710
  - test/rbbt/tsv/test_csv.rb
679
- - test/rbbt/tsv/test_accessor.rb
680
- - test/rbbt/tsv/test_matrix.rb
711
+ - test/rbbt/tsv/test_manipulate.rb
681
712
  - test/rbbt/tsv/test_field_index.rb
682
713
  - test/rbbt/tsv/test_util.rb
683
- - test/rbbt/tsv/test_index.rb
714
+ - test/rbbt/tsv/test_accessor.rb
715
+ - test/rbbt/tsv/test_filter.rb
716
+ - test/rbbt/tsv/test_stream.rb
684
717
  - test/rbbt/tsv/test_parallel.rb
685
- - test/rbbt/tsv/test_manipulate.rb
718
+ - test/rbbt/tsv/test_marshal.rb
719
+ - test/rbbt/tsv/test_matrix.rb
720
+ - test/rbbt/tsv/test_attach.rb
686
721
  - test/rbbt/tsv/test_excel.rb
687
- - test/rbbt/tsv/parallel/test_through.rb
722
+ - test/rbbt/tsv/test_change_id.rb
723
+ - test/rbbt/tsv/test_index.rb
688
724
  - test/rbbt/tsv/parallel/test_traverse.rb
689
- - test/rbbt/tsv/test_stream.rb
690
- - test/rbbt/test_association.rb
691
- - test/rbbt/hpc/test_batch.rb
692
- - test/rbbt/hpc/orchestrate/test_chains.rb
693
- - test/rbbt/hpc/orchestrate/test_rules.rb
694
- - test/rbbt/hpc/orchestrate/test_batches.rb
695
- - test/rbbt/hpc/test_slurm.rb
696
- - test/rbbt/hpc/test_orchestrate.rb
697
- - test/rbbt/association/test_database.rb
698
- - test/rbbt/association/test_item.rb
699
- - test/rbbt/association/test_open.rb
700
- - test/rbbt/association/test_util.rb
701
- - test/rbbt/association/test_index.rb
702
- - test/rbbt/test_knowledge_base.rb
703
- - test/rbbt/persist/tsv/test_kyotocabinet.rb
704
- - test/rbbt/persist/tsv/test_cdb.rb
705
- - test/rbbt/persist/tsv/test_lmdb.rb
706
- - test/rbbt/persist/tsv/test_sharder.rb
707
- - test/rbbt/persist/tsv/test_leveldb.rb
708
- - test/rbbt/persist/tsv/test_tokyocabinet.rb
709
- - test/rbbt/persist/test_tsv.rb
710
- - test/rbbt/test_tsv.rb
711
- - test/rbbt/test_annotations.rb
712
- - test/rbbt/test_fix_width_table.rb
713
- - test/rbbt/test_workflow.rb
714
- - test/rbbt/entity/test_identifiers.rb
715
- - test/rbbt/annotations/test_util.rb
716
- - test/rbbt/test_hpc.rb
717
- - test/rbbt/test_monitor.rb
718
- - test/rbbt/test_persist.rb
719
- - test/rbbt/knowledge_base/test_entity.rb
720
- - test/rbbt/knowledge_base/test_registry.rb
721
- - test/rbbt/knowledge_base/test_syndicate.rb
722
- - test/rbbt/knowledge_base/test_query.rb
723
- - test/rbbt/knowledge_base/test_enrichment.rb
724
- - test/rbbt/knowledge_base/test_traverse.rb
725
- - test/test_helper.rb
725
+ - test/rbbt/tsv/parallel/test_through.rb