topas-tools 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2170b03e20ff05d36be9578bae34c91277c61099
4
+ data.tar.gz: 36c698ba2ec54a4ceb21a8bd4f858a5baa6f24af
5
+ SHA512:
6
+ metadata.gz: 02492c79940bf73d62fde03d6b91aa81e57656d8d85c38852392090ba12d35d2f034180997c04d5aa9afa491215470e51a203eeb4f94f654d232a258bb77d40c
7
+ data.tar.gz: d8be1e2f7db5ce5a8627a809fd472db3d39bf3c699b5b1c0b9bd22721e25d165adb0addc4e3b58f1ff0ea7e0fe0fbd9e5bcfc164915ecd2f6b7382cbb443edb5
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require 'optparse'
5
+ require 'topas-tools'
6
+
7
+
8
+ options = {points:[100.0, 16.0, 0.0], steps:[4.0, 1.0], smart:false, system: :wine}
9
+ OptionParser.new do |opts|
10
+ opts.banner = "Usage: toparun.rb [-p 100,15,0 -s 1,0.25] filename.inp"
11
+ opts.on("-p", "--points LIST", Array ,"Array of points, representing intervals with different stepsizes"){|list|
12
+ options[:points] = list.map(&:to_f)}
13
+ opts.on("-s", "--steps LIST", Array ,"Well, stepsizes for that intervals"){|list|
14
+ options[:steps] = list.map(&:to_f)}
15
+ opts.on("-s", "--[no-]smart", "Gone smart!") do |s|
16
+ options[:smart] = s
17
+ end
18
+ opts.on("--system [SYS]", [:windows, :wine, :dummy],
19
+ "Select your system (windows, wine, dummy)") do |t|
20
+ options[:system] = t
21
+ end
22
+ end.parse!
23
+
24
+ ARGV[0] || raise("Rrrr! Where is my input file??")
25
+
26
+ TopasEngine.system = options[:system]
27
+
28
+
29
+ runner = TopasEngine.new
30
+ input = TopasInput.new IO.read(ARGV[0], :encoding => "UTF-8"),
31
+ ARGV[0].sub(/\..{2,3}$/, '')
32
+
33
+ runner.refine input, options[:points], options[:steps], (options[:smart] ? QuantileAnalyzer.new : BaseAnalyzer.new)
34
+
@@ -0,0 +1,97 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+ require 'stringio'
4
+ require 'green_shoes'
5
+ require 'topas-tools'
6
+
7
+
8
+ Shoes.app :width => 400, :height => 800 do
9
+
10
+ @log = StringIO.new
11
+ @log.write "Hi, log starts here!\n"
12
+
13
+ def log
14
+ @log
15
+ end
16
+
17
+ #base flow
18
+ flow :margin=>10 do
19
+
20
+ title 'ToparunGUI'
21
+
22
+ #big stack
23
+ stack do
24
+ caption 'Your system:'
25
+ @systemlist = list_box items:['wine','windows','dummy'], choose:'wine'
26
+
27
+ caption 'Your Topas4-2 directory:'
28
+
29
+ #small flow
30
+ flow do
31
+ @topasdir = edit_line
32
+ button 'Open' do
33
+ @topasdir.text = ask_open_folder
34
+ end
35
+ end
36
+ #\small flow
37
+
38
+ caption "Points and steps"
39
+ para "TODO объяснить, кто все эти люди"
40
+ flow do
41
+ para "Points:"
42
+ @pointline = edit_line
43
+ @pointline.text = "100,16,0"
44
+ end
45
+
46
+ flow do
47
+ para "Points:"
48
+ @stepsline = edit_line
49
+ @stepsline.text = "4,1"
50
+ end
51
+ caption "Your input file"
52
+ flow do
53
+ @inpline = edit_line
54
+ button 'open' do
55
+ @inpline.text = ask_open_file
56
+ end
57
+ end
58
+
59
+ button 'Mf button!' do
60
+ begin
61
+ points = @pointline.text.split(/[,\s]+/).map(&:to_f)
62
+ steps = @stepsline.text.split(/[,\s]+/).map(&:to_f)
63
+ rescue alert("Блюди формат точек и шагов!!")
64
+ end
65
+
66
+ Thread.new(points, steps, @log ) do |points, steps|
67
+ inp = @inpline.text
68
+ TopasEngine.system = @systemlist.text.to_sym
69
+ runner = TopasEngine.new File.dirname(inp)
70
+ input = TopasInput.new IO.read(inp, :encoding => "UTF-8"), inp
71
+ runner.refine input, points, steps, BaseAnalyzer.new(@log)
72
+ @clear = button 'Clear' do
73
+ @log = StringIO.new
74
+ @clear.clear
75
+ end
76
+ end
77
+ end
78
+
79
+ para self
80
+ caption "Log:"
81
+ stack :width => 300, :margin => 20 do
82
+
83
+
84
+ @paralog = para
85
+ every 1 do |i|
86
+ a = @log.string
87
+ @paralog.text = a unless @paralog.text == a
88
+ flush
89
+ end
90
+ end
91
+
92
+
93
+ end
94
+ end
95
+ end
96
+
97
+
@@ -0,0 +1,3 @@
1
+ require 'topas-tools/TopasEngine.rb'
2
+ require 'topas-tools/TopasInput.rb'
3
+ require 'topas-tools/Analyzers.rb'
@@ -0,0 +1,17 @@
1
+ class BaseAnalyzer
2
+ def initialize output = $stdout
3
+ @out = output
4
+ @count = 0
5
+ end
6
+
7
+ def analyze inp
8
+ @out.print "Analizing #{inp.name}... \n"
9
+ @count = @count + 1
10
+ true
11
+ end
12
+
13
+ def report
14
+ @out.print "Refinement was finished in #{@count} steps.\n"
15
+ true
16
+ end
17
+ end
@@ -0,0 +1,77 @@
1
+ class TopasEngine
2
+
3
+ Systems = [:windows, :wine, :dummy]
4
+
5
+ @@topasdir = "/home/dmitrienka/.wine/drive_c/TOPAS4-2/" #Depends!
6
+ @@system = :dummy #[:wine, : :windows, :dummy]
7
+
8
+
9
+ def self.system=(system)
10
+ if Systems.any?{|sys| sys = system}
11
+ @@system = system
12
+ else
13
+ raise "Unknown system!"
14
+ end
15
+ end
16
+
17
+ def self.topasdir=(topasdir)
18
+ @@topasdir = topasdir
19
+ end
20
+
21
+
22
+ def initialize dir = Dir.getwd
23
+ @base_dir = dir
24
+ Dir.chdir @base_dir
25
+ @enc = "UTF-8"
26
+ end
27
+
28
+ def tc input
29
+ infile = "#{input.name}.inp"
30
+ File.open(infile, mode:'w'){|f| f.write input.text}
31
+ case @@system
32
+ when :wine
33
+ path = `winepath -w #{infile}`.gsub('\\', '\\\\\\').gsub("\n", "")
34
+ command = "wine tc.exe #{path}"
35
+ go_n_do command
36
+ when :windows
37
+ command = "tc.exe #{File.expand_path(infile)}"
38
+ go_n_do command
39
+ when :dummy
40
+ outfile = "#{input.name}.out"
41
+ p "Say hello to #{outfile} with k1 = #{input.k1}"
42
+ File.open(outfile , mode:'w'){|f| f.write input.text}
43
+ end
44
+ TopasInput.new IO.read("#{input.name}.out", :encoding => @enc)
45
+ end
46
+
47
+
48
+ def refine input, ps, ss, analyzer = BaseAnalyzer.new
49
+ k1s = get_k1s ps, ss
50
+ name = input.base_name
51
+ Dir.mkdir(name) unless Dir.exists? name
52
+ @work_dir = File.expand_path(name)
53
+ Dir.chdir @work_dir
54
+ k1s.reduce(input) do |inp, k1|
55
+ out = tc inp.set_k1(k1)
56
+ analyzer.analyze(out) ? out : break
57
+ end
58
+ Dir.chdir @base_dir
59
+ analyzer.report
60
+ end
61
+
62
+ private
63
+
64
+ def go_n_do command
65
+ Dir.chdir @@topasdir
66
+ system(command)
67
+ Dir.chdir @work_dir
68
+ end
69
+
70
+ def get_k1s points, stepsizes
71
+ stepsizes.inject([]){|result, stepsize|
72
+ a = result + points[0].step(points[1] ,-stepsize).to_a
73
+ points.shift
74
+ a}.uniq
75
+ end
76
+
77
+ end
@@ -0,0 +1,95 @@
1
+ class TopasInput
2
+ def initialize text, base_name = nil
3
+ @text = text.dup
4
+ @k1 = get_k1
5
+ @base_name = get_base_name base_name
6
+ @name = get_name
7
+ @restrains = get_restrains
8
+ end
9
+
10
+ attr_reader :k1, :name, :base_name, :restrains, :text
11
+
12
+
13
+ def set_k1!(num)
14
+ @k1 = num.to_f
15
+ @text = @text.sub(/(penalties_weighting_K1\s+)[\d.]+/ , "\\1#{@k1}").
16
+ sub(/[\d.]*(\.cif)/ , "#{@k1}\\1")
17
+ @name = get_name
18
+ self
19
+ end
20
+
21
+ def set_k1(num)
22
+ dup.set_k1!(num)
23
+ end
24
+
25
+ def restrain_names
26
+ @restrains.map{|r| r[:name]}
27
+ end
28
+
29
+ def set_restrain!(name, new)
30
+ if r = @restrains.select{|r| r[:name] == name}[0]
31
+ r[:restrain] = new
32
+ @text = write_restrains
33
+ self
34
+ else
35
+ raise "No restrain with the name #{name}"
36
+ end
37
+ end
38
+
39
+ def set_restrain(name, new)
40
+ dup.set_restrain!(name, new)
41
+ end
42
+
43
+ private
44
+
45
+
46
+
47
+ def get_k1
48
+ @text.scan(/penalties_weighting_K1\s+([\d.]+)/)[0][0].to_f
49
+ end
50
+
51
+ def get_base_name bn
52
+ if bn
53
+ base_name = File.basename(bn, File.extname(bn))
54
+ @text = @text.sub(/phase_name\s+".+"/, %Q[phase_name "#{bn}"])
55
+ base_name
56
+ else
57
+ base_name = @text.match(/phase_name\s+"(.+)"/)[1]
58
+ base_name
59
+ end
60
+ end
61
+
62
+
63
+
64
+
65
+ def get_name
66
+ "#{@base_name}_#{@k1}"
67
+ end
68
+
69
+
70
+ def get_restrains
71
+ restrains_pattern = /(Distance_Restrain(?:_Breakable|_Morse)?)\(\s*(\w+\s+\w+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)[`_\d.]*\s*,\s*([\d.]+\s*,\s*[\d.]+)\s*\)/
72
+ if @text =~ restrains_pattern
73
+ @text.scan(restrains_pattern).reduce([]){|memo, (type ,name, restrain, value, rest)|
74
+ memo << {name:name, type:type, restrain:restrain.to_f, value:value.to_f, rest:rest}
75
+ memo}
76
+ else
77
+ nil
78
+ end
79
+ end
80
+
81
+ def write_restrains
82
+ @restrains.reduce(@text) { |memo, r|
83
+ pattern = /#{r[:type]}\s*\(\s*#{r[:name]}\s*,\s*[\d.]+\s*,\s*[\d.]+[`_\d.]*\s*,\s*[\d.]+\s*,\s*[\d.]+\s*\)/
84
+ memo.sub(pattern, "#{r[:type]}(#{r[:name]}, #{r[:restrain]}, #{r[:value]}, #{r[:rest]})")
85
+ }
86
+ end
87
+
88
+ end
89
+
90
+
91
+ def dumbdiff as, bs
92
+ as = as.split /\n/
93
+ bs = bs.split /\n/
94
+ as.zip(bs).reject{ |a, b| a == b}
95
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: topas-tools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Artem Dmitrienko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: green_shoes
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.1.374
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.1'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.1.374
33
+ description: Gem for repetitive Rietveld refinements with Topas4-2
34
+ email: dmitrienka@gmail.com
35
+ executables:
36
+ - toparunGUI
37
+ - toparun
38
+ extensions: []
39
+ extra_rdoc_files: []
40
+ files:
41
+ - bin/toparun
42
+ - bin/toparunGUI
43
+ - lib/topas-tools.rb
44
+ - lib/topas-tools/Analyzers.rb
45
+ - lib/topas-tools/TopasEngine.rb
46
+ - lib/topas-tools/TopasInput.rb
47
+ homepage:
48
+ licenses:
49
+ - GPL2
50
+ metadata: {}
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 2.2.2
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Rrrrr!
71
+ test_files: []