If a method start from capital letter, such as NArray.byte, this means that it is a constructive method. If a method start from lower letter, such as narray.dim or self.dim, these are class methods, so please substitute the first word to your own instance object.
NArray[1,5,10.0] \#=> NArray.float(3):[1.0, 5.0, 10.0] NArray[1..10] \#=> NArray.int(10):[1,2,3,4,5,6,7,8,9,10]
self.dim
:
Return the dimension = the number of indices.
self.rank
:
same as dim
self.shape
:
Returns the array of sizes of each index.
self.total
:
Returns the number of total elements.
Components of the index can be one of the following: Integer, Range, Array, true.
The index order is the FORTRAN type.
a[ 1, 2, 1 ]
:element slicing. If negative, counts backward
from the end. Elementdimensions are
contracted.
a[ 0..3, 4..1 ]
: extract in the range. If the former of the
range is bigger, return elements in reversed
order.
a[ [1,3,2,4] ]
: an array with the elements of the indices.
If `a' has multidimension and single index is
specified in [], `a' is treated as a single
dimension array.
a[ 1, 2..3, [1,3,2,4], true ]
: Compound index. This returns
threedimensional array.
a[]
: Same as a.dup. Dupulicate the array.
a[ 0, true ]
: Sams as a[0,0..1]
. `true' means all.
a[ false, 0 ]
: Same as a[true,true,0], if a
is a 3d array,
`false' means ellipsis dimension.
a[ mask ]
: masking. ``mask'' is a byte NArray with its
length equal to that of ``a''. According to the
value of each element of mask, the corresponding
element in ``a'' is eliminated (when 0) or
retained (when not 0).
a=NArray.float(2,2).indgen! p a[ a.lt 3 ] > [ 0.0, 1.0, 2.0 ](Here,
a.lt 3
gives a byte NArray)
(This is also done by a[ (a.lt 3).where ]
)
A twoormoredimensional array object with only one argument in `[ ]',
is treated as a flat onedimensional array.
e.g.: a[3]
is same as a[0,1]
if a
is 3x3 array.
self.slice(...)
Same as self[...]
but keeps the rank of
original array by not elimiting dimensions
whose length became equal to 1 (which self[]
does). This is not the case with the
onedimensional indexing and masking (same as []).
a[ 1, 2, 3 ] = 1
a[ 0..3, 1..4, 2..5 ] = 2
a[ [1,3,2,4], true ] = 3
a[] = 4
Same as a.fill!(4)
Special cases:
a[0..2] = b[1..5]
Error! due to different num of elements.
a[1,2] = b[0..2,1..3]
Storing elements from index [1,2] ( a[1,2]=b[0,1],a[2,2]=b[1,1],... ). In other words, [1,2] corresponds to the corner of b array.
a[0..2,0..3] = b[0..2,1]
Storing repetitively ( a[0,0]=b[0,1],..,a[0,3]=b[0,1] )
self.indgen!([start[,step]])
Generate index;
Set values from 'start' with 'step' increment
self.fill!(value)
Fill elements with 'value'
self.random!(max)
Set random values between 0<=x<max
self.randomn
Set Normally distributed random values
with mean=0, dispersion=1 (BoxMuller)
+=, =, *=, /=
. A
new useful operator is ternary. By using this methods, you do
not need to use if operation. For example, the methods can be
used as follows:
a = NArray.sfloat(5,5).randomn b = NArray.sfloat(5,5).randomn c = (a > b).ternary(a,b)
This program returns the bigger value either in a or b. For other operators, see Table 5.2.




self.transpose( dim0, dim1, .. )
Transpose array. The dim0th dimension goes to the 0th dimension of new array. Negative number counts backward. transpose(1,1..2,0) is replacement between the first and the last.
self.mirror( dim )
Mirror array along the dimension. This method is not yet
implemented. Please use following script to make a mirror.
Referencing can be used to change NMatrix to NArray.

See Table 5.9.
NMath.sqrt(narray)
.

a_ij => a[j,i]
.
NMatrix[[1,2],[3,4]] * [1,10] == NMatrix[ [[1,2],[3,4]], [[10,20],[30,40]] ]
a/b == b.lu.solve(a)
This function is not 64bit compatible.
NMatrix.affine_shift(x,y, {z})
Returns 2D or 3D Affine transformation matrix. If
is not given,
it returns 2D Affine matrix
(3x3). If
is given, it returns
3D Affine matrix (4x4).
NMatrix.2d_affine_rotation(phi)
NMatrix.2d_affine_scale(scale)
NVector is a subclass of NArray. First dimension is used as Vector. Residual dimensions are treated as Multidimensional array.
v/m == m.lu.solve(v)
NMatrixLU
is created by NMatrix#lu
method.
Including LU (NMatrix) and pivot (NVector).
Example:
language=Ruby, basicstyle=, frame=single
Solve with the result of LU factorization.
The module support multidimensional complex FFT. It supports both double and single float transforms, if FFTW3 is compiled for both single and double float. Note that the values are not normalized as in FFTW.
FFTW3.fftw( narray, dir, [,dim,dim,...] )
FFTW3.fft( narray, dir, [,dim,dim,...] )
Perform complex FFT. The 3rd, 4th, ... arguments are optional.
Note: As in FFTW, return value is not
normalized. Thus, a consecutive forward and backward transform
would multiply the size of data used for transform. You can
normalize, for example, the forward transform
FFTW.fft(narray, 1, 0, 1)
(FFT regarding the first (dim 0)
& second (dim 1) dimensions) by dividing with
(narray.shape[0]*narray.shape[1])
. Likewise, the result of
FFTW.fft(narray, 1) (FFT for all dimensions) can be normalized by
narray.length
.
FFTW3.r2c( narray )
Perform real to complex transform. Works for single float and double. Retern value depends on how you compile rubyhelix. If you compiled with single floatable FFTW3, it will return the same type. If not, always return double.
FFTW3.c2r( narray )
Perform complex to real transform.
FFTW3.export_wisdom_to_file(filename)
FFTW3.import_wisdom_from_file(filename)
Those two modules can save and restore FFTW's wisdom. Following is a quote from FFTW3's manual:
FFTW implements a method for saving plans to disk and restoring them. In fact, what FFTW does is more general than just saving and loading plans. The mechanism is called wisdom. Here, we describe this feature at a high level. See FFTW Reference, for a less casual but more complete discussion of how to use wisdom in FFTW.
NArray.open_mrc(filename), NArray.read_mrc(filename)
:
Read MRC file from the file specified
by filename, e.g. if you have a file test.mrc
, you
can open it with NArray.open_mrc("test.mrc")
.
Byteorder: If the script tries to read a MRC file generated by a computer with different byteorder (e.g. Intel vs PowerPC), the script automatically swap the byteorder, based on a assumption that any dimensions of the array should not be larger than 300,000.
This method can read both 20th and 21st century format of MRC.
self.write_mrc(filename)
:
write various type of MRC
byte,shortint,single float and double, which are written in the
native format of the machine you use. The MRC format is 21st
century new format.
self.write_xplor(filename)
:
write XPLOR test type map to the file specified by the filename.
Tested only for pymol and float type.
NArray.open_uniform(filename)
NArray.read_uniform(filename)
This function is mainly used for read ``uniform'' ascii
format. This format is mainly used for writing and reading spline
curve.
This format is described below:
dimensions dim1 dim2 dim3 data...
NArray.write_uniform(filename)
This function is mainly used for write ``uniform'' ascii
format. This format is mainly used for spline
curve.
NArray.open_tiff(filename)
:
NArray.read_tiff(filename)
:
open tiff format
NArray.write_tiff(filename)
NArray.byte(x,y).write_tiff(filename)
or
NArray.byte(3,x,y).write_tiff(filename)
In this case color.
NArray.xplot(title)
Title is optional. Plot the content of the NArray as one dimensional
array.
If NArray contains (N,2) or (2,N) array, this methods will plot the pair as (x,y).
NArray.write_tiff2(filename)
NArray any types.
Written in Ruby script, but fast.
Automatically scale, and write gray
scale image.
self.complex_to_color_tiff(filename,max=nil)
Save complex value in colored tiff
map. HUE is the phase of complex value
and saturation is the amplitude.
narray.to_ps(out, dot_per_inch, [message])
:
Print out the postscript image to out
(an IO object). The image will be printed out
with dot_per_inch
. A message can also be
printed out, optional.
narray.apodize(width)
:
Gaussian apodization.
(width/3).
narray.background_subtract_along_x(width)
: Background
subtraction along x. The background is fitted with parabollic
function. Left and right width pixels are used as
backgrounds.
narray.background_subtract_along_y(width)
: Background
subtraction along y. The background is fitted with parabollic
function. Left and right width pixels are used as
backgrounds.
narray.correlation(ref)
Returns the correlation between self and ref. self and ref need to be
real array (sfloat or float). The two array also need to have the same
dimension. Requires FFTW3 module.
An example is shown in Figure 5.1.
narray.cut_out(start,end_p,length,width,spline,shift = 0.0)
Cut out along spline curve. The image
will be extracted using bilinear interpolation.
length
and the width
of the
final image should also be specified. If (endstart) is different
from the length, the image will be shrinked or streched.
spline
is an array of size (4,n), where the
spline[0,i]
is the index, spline[1,i]
is
value, spline[2,i]
is
value, and
spline[3,i]
is the angle of the spline curve.
narray.unbend
was written, but not tested
extensively. Therefore removed from the
library. Please use cut_out.
narray.fit_sub(b)
: (stands for fit and subtract)
NArray object b is fitted to self by changing
value c to minimize (self  b * c)^2
. Then return
selfb*c.
These two methods extract data from narray image using coordinate specified by . Those are float array of the same size. Dimensions of need to be the same as the dimension of original image. If are sfloat type, Cwritten version of interplate is used, which is much faster and memory efficient.
background is optional. If not specified, background is set to 0.0.
C implementation of multidimensional bilinear interpolation routine. Currently interpolate2d uses this function when narray is an SFLOAT type array. It works in the same way as na_interpolate_2d_c. 2d version is twice as fast as nd version. xyz is an array of coordinate. For example, if x and y contain the coordinate, you may call this function as :
narray.interpolate_nd_c([x,y],background)}
narray.fourier_synthesis_2d!(bigG,startx,endx,starty,endy,omega=0.0)
:
Fourier synthesis in 2D.
Here is a sample code
narray.fourier_synthesis_3d!(littleG,centerx,centery,centerz,pixel_size)
:
Yet to be described...
narray.mrc_fft
: Apply MRC type fft and returns the
array. It executes an external command fft provided by MRC
package.
narray.mrc_fftw
: Apply MRC type Fast Fourier transform using FFTW3.
Approximately four times faster than mrc_fft because it does not
involve writing and reading temporary file.
The first dimension should be even.
narray.pad(x,y,padvalue=0,bevel=0
: Pad the image to the final
size of (x,y). The original image is located at the center.
narray.put_pdb!(pdb)
:
Put a molecule specified by a PDB object onto the image.
narray.ya_pad(direction,size,position=Pad_center,pad_value = 0,bevel=0)
:
Yet another padding method.
narray.resolution
: Returns the resolution array based on
the cell dimension.
narray.bfactor
: Works for FFT image. Returns the bfactor
based on the cell dimension. What actually returned is
, where
is the bfactor and
is the resolution of each
point.
narray.ctf(def1,def2,angle,hv=200,amp=0.07,cs=3.3)
:
The pixel size of the array should be given in the header of
the array. def1 and def2 is the defocus level in
angstrom. Angle of the stigmatism should be
given by angle. hv is the acceleration voltage
in kV. amp is the amplitude contrast. cs is the
spherical abberration.
narray.extract_layerline(height,rmin,rmax,n,l)
:
Mainly used from verb
narray.hlxfl.
narray.hlxfl(controlFilename, [ctf])
:
Extract layer lines according to the
controlFilename. Optionally, ctf image is
given. The ctf should contain the CTF value at each
points. Returns three Big G objects, near, far and average layer
lines in an array.
narray.limit_resolution!(res)
: Based on the header
information, limit the resolution to specified by res
(Å).
narray.phaseshift!(x_origin,y_origin)
:
self.header
: return the header of MRC
self.add_label
: add label to the header
self.mode
:
self.mode=
:
self.origin
:
self.origin=[x,y]
:
self.spacegroup
:
narray.dimensions
:
narray.dimensions=
:
narray.cellsize
:
narray.cellsize=
: set the cell size. Currently set the
dimension size, but in the future, to conform with MRC format, this
will be changed.
narray.n_label
: returns the number of labels.
narray.reset_label!
: Remove all the labels.
narray.add_label!(arg)
: Add a new label.
LayerLine.new(fileHandle)
:
layerline.apply_r_scale
:
layerline.apply_phi_z_shift(phi,z)
:
phi: radian
z : 2 PI l / repeat_distance
layerline.write(fileHandle)
:
layerline.n
:
layerline.l
:
layerline.n=
:
layerline.l=
:
layerline.r
:
layerline.data
:
layerline.size
:
layerline.is_n_l_same
:
layerline.get_data_at_r(r)
:
layerline.get_ctf_at_r(r)
:
layerline.add(ll_add, weight)
:
layerline.reverse!
: Change the polarity of layer line data.
layerline.fourier_bessel!(pixel_size,dim)
: convert from bigG to little g
vise versa.
In this case, Bessel order can
be float. The function is used for Asymmetric Helical
Reconstruction [5].
layerline.fourier_bessel_matrix(pixel_size,dim)
:
returns matrix used for
Fourier Bessel conversion.
BigG.read(filename)
: read bigG from a file specified
by the filename.
BigG.read_little_g(filename)
: read little g (Toyoshima and Yonekura format)
bigG.write(filename)
: write a bigG to a file.
bigG.write_ky(filename)
: write a bigG to a file in Yonekura and
Toyoshima bigG format.
bigG.apply_r_scale(rscal)
: Apply R scale factor.
bigG.r_mask(rmin,rmax)
: Used for little g. Pick up only the region between
rmin and rmax (including these values).
bigG.apply_phi_z_shift(phi,z)
Apply
shift
(phi): radian,
:
bigG.hk_select(array)
Return a bigG that contains layer
lines specified by array
. A sample of array is like
[[0,0],[1,0]]
.
bigG.reverse!
Reverse the orientation.
bigG.add_layerline(LayerLine a)
:
Add a new layer line file.
bigG.multiply!(scale)
:
scale
bigG.renumbering(n10,l10,n01,l01,new_n10,new_l10,new_n01,new_l01,rmax10,rmax01)
:
bigG.plot(n)
: plot
th layer line with
gnuplot.
bigG.limit_resolution(resolution)
limit the resolution.
Before executing this method,
repeat length should be set.
give angstrom
bigG.fourier_bessel!(pixelsize,dim)
:
The Fourier Bessel conversion. The
conversion works in either directions. (little g > big G,
too)
is given in angstrom, when a big G is converted
to a little g. When a little g is
converted to a big G, the
is
given in [1/Å].
dim is an integer value.
( maximum radius should be
)
This method works in both direction !!! (from little g to big G)
The new fourier Bessel transformation can transform float Bessel orders.
bigG.littleG_to_bigG(littleG)
:
The method convert a little g
(littleG) according to the bigG given by
bigG.
bigG.resolution=(resolution)
:
same as
bigG.limit_resolution.
Therefore, the method
manipulate the data too.
bigG.resolution
:
returns the current resolution
bigG.limit_radius(rmax)
:
mainly used for little G
bigG.repeat=
:
bigG.truepitch=
:
set the truepitch in angstrom.
bigG.repeat
:
bigG.truepitch
:
Returns the truepitch in agnstrom.
Using the basic helices, now we can describe the unit cell as shown in Fig. 5.2.

From this relation, the unit cell vectors have the relation shown with the basic helix vectors as follows. Here we assume and .
Actually this equation is interesting because, the determinant of
(4.3) 
In addition, the determinant of
(4.4) 
To facilitate these calculation, there are several methods.
bigG.nl_determine
: Guess the basic helix from the
layer line data.
bigG.nl_check
: If basic helical vectors are not
given, determine them using
nl_determine.
bigG.n10
:
bigG.n01
:
bigG.l10
:
bigG.l01
:
bigG.n10=
:
bigG.n01=
:
bigG.l10=
:
bigG.l01=
:
The following methods return real space basic vectors.
bigG.phiz_matrix
: returns the
matrix.
If repeat length is not given, assume
repeat =
.
returned matrix is:
(4.5) 
bigG.za
: returns
bigG.zb
: returns
bigG.phia
: returns
bigG.phib
: returns
bigG.phi_unit_cell
: returns the size of unit cell in
direction (radian). Calculate the edgetoedge size. See
figure 5.2.
bigG.z_unit_cell
: returns the size of unit cell in
(radian
or angstrom, depending on whether you gave repeat length). Calculate
the edgetoedge size. See
figure 5.2.
bigG.hk(n,l)
: returns [h,k] pair as an array.
bigG.hk(LayerLine)
: returns [h,k] pair as an
array.
bigG.make_ll(delr,radius)
:
after setting n10,l10,n01,l01,repeat,resolution
make zero valued layer line.
$ irb irb(main):001:0> a = BigG.new irb(main):002:0> a.n10= 15 irb(main):003:0> a.l10= 1 irb(main):004:0> a.n01= 2 irb(main):005:0> a.l01= 17 irb(main):006:0> a.repeat = 1360 irb(main):007:0> p a.phiz_matrix NMatrix.sfloat(2,2): NMatrix.sfloat(2,2): [ [ 0.4156, 0.02445 ], [ 10.58, 79.38 ] ] irb(main):008:0> p a.nl_matrix NMatrix.sfloat(2,2): [ [ 15.0, 1.0 ], [ 2.0, 17.0 ] ] irb(main):009:0> exit
self.correlationPlot(bigG2)
printout correlation plot for
xplot
This class uses x,y swapped axis shown in Figure 5.3.
SplineArray.new(splinearray,startp,endp)
:
The initialization
method. Mainly for internal use.
SplineArray.from_points(yarray,xarray,startp,endp)
:
Generate spline curve using cubic spline curve. yarray:
array of float, xarray: array of float, the dimension
of yarray and xarray should be the
same. startp,andp: start and end point in
, not in
.
SplineArray.read_unbent(filename)
: Read from ``unbent'' file,
which contains uniform format of control points.
splineArray.ydash_to_y(ydash)
:
splineArray.ydash_to_x(ydash)
:
splineArray.y_to_ydash(y)
:
splineArray.y_to_x(y)
:
splineArray.write(filename)
:
splineArray.ydash
:
y
: return NArray that contains
coordinates.
x
: return NArray that contains
coordinates.
theta
: return NArray that contains
in degree.
size
:
+
: second argument (right argument) can be either
SplineArray or Numeric.
If the second argument is an instance of SplineArray, the result is
(4.6) 
(4.7) 
where, are from the left SplineArray, are from the right SplineArray, and are the results.
xplot(title = "", xplot = open("xplot","w") )
Plot the spline curve using xplot.
Serialized big G or little G. Mainly designed for easier manipulation of big G files.
To generate SerialG from BigG
g = BigG.read("filename.avg") sg = g.serialize
Or using template:
g = BigG.read("filename.avg") sg = g.serialize(template_sg)
sg.repeat
:
sg.repeat=
: set or get repeat length in angstrom.
sg.x_shift!(x)
: apply x shift
sg.omega_shift!(x)
: apply omega shift
sg.reverse!
: apply reverse
sg.change_side
: change near to far or far to near.
sg.phase_residual(sg2)
: return the phase residual value between two
serial G.
sg.phase_shift!(phi,z)
: phi: radian
z: radian < recently changed!!
sg.dup
: duplicate
sg.bigG
: convert to BigG data.
sg.resolutionPlot(sg2,truepitch,xplot = nil)
:
plot resolution (x) vs correlation (y) using
xplot.
sg.limit_nl(n,l)
: limit
and
and returns a new
sg
bigG.n
:
returns serialized n as an NArray object.
bigG.l
:
returns serialized l as an NArray array
bigG.r
: returns serialized r array
bigG.data
: returns serialized data
array
bigG.ctf
: returns serialized ctf
array
sg.r
:
sg.data
:
sg.ctf
:
sg.n
:
sg.l
: get individual data
+
: Sum. Here the method assumes two serialBigG have same
(n,l) pairs.
*
: Left hand is the serialBigG and right hand should be a
scalar.
ctf_multiply!
: Inside, it does @data = @data * @ctf
.
ctf_square!
: Inside, it does @ctf = @ctf * @ctf
.
PDB.read(filename)
:
read PDB file
pdb.write(filename)
: write PDB file
pdb.move(x,y=0,z=0)
: translate by (x,y,z)
If x is a NVector object, y and z can be
omitted.
PDB.center
: Retern the center of the gravity
Currently each atom have same weight
return value is a vector(3)
PDB.rotate(matrix)
:
take 3x3 NMatrix to rotate.
Actually this is simply a multiply of the
matrix to the coordinate.
PDB.rotate_z(phi)
:
rotate around z with phi
PDB.dup
: duplicate the content.
only the coordinate (xyz), bfactor and
occupancy are copied.
NArray.put_pdb(pdb)
: project PDB subunit along the Z axis
require "Minimizer.rb"
obj = Minimizer2D.new
: generate a new Minimize.
obj.set_x(xmin,xmax,steps)
: set x range.
obj.set_y(ymin,ymax,steps)
: set x range.
obj.minimize{ array value }
: given parameter array =
and
return value so that the value is minimized.
return the minimized values as
[xmin,ymin,value].
obj.update_range
: update the search range.
Search range
is updated to be
, where
is the number
of steps.
Example code:
obj.minimize3D{ array evaluate value }
: given parameter array =
and
return value so that the value is minimized.
return the minimized values as
[xmin,ymin,zmin, value].
obj.update_range
:
Sample code: