Skip to content

Commit

Permalink
changed environment workflow to user-oriented setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
iancze committed Jun 16, 2020
1 parent f0eecce commit fc10998
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 209 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
NPZ = "15e1cf62-19b3-5cfa-8e77-841668bca605"
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee"
Expand Down
58 changes: 46 additions & 12 deletions docs/src/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,42 @@

## Julia

First, you should to install the [Julia programming language](http://julialang.org/downloads/) on your machine.
First, you should to install the [Julia programming language](http://julialang.org/downloads/) on your machine. Depending on how you choose to install Julia, you may need to take the additional step of adding the Julia executable to your `PATH`.

Depending on how you choose to install Julia, you may need to take the additional step of adding the Julia executable to your `PATH`. [Here](https://en.wikibooks.org/wiki/Introducing_Julia/Getting_started#Running_directly_from_terminal) are some suggestions for OS X if you are experiencing difficulty.
Julia uses "environments" to manage package dependencies. These are similar in concept to Python's virtual environments, and very useful in practice. If you are unfamiliar, it is highly recommended to read through the Julia environment documentation [here](https://docs.julialang.org/en/v1/stdlib/Pkg/), since they are used to install DiskJockey in the recommended configuration.

Julia uses "environments" to manage package dependencies. If you are unfamiliar, it is worth reading through the documentation on how Julia's package manager works with environments [here](https://docs.julialang.org/en/v1/stdlib/Pkg/).
To get started, create a new folder that you will contain your work for a particular project.

## DiskJockey
$ mkdir myproject

`DiskJockey` is not (yet) registered an official Julia package, so it needs to be installed by using the link to the github repository. First, open up a Julia prompt in the REPL, then type `]` to enter the package manager, then use the `add` command to clone the DiskJockey repository.
Then, start Julia, and use the interactive package manager `Pkg` (enter via `]`) to create an environment specific to this project.

julia> ]
(@v1.4) pkg> add https://github.com/iancze/DiskJockey.git
$ cd myproject
$ julia
julia> ]
pkg> activate .
Activating new environment at `~/myproject/Project.toml`
(myproject) pkg>

## DiskJockey Package

Next, we will install the DiskJockey package to this same Julia environment. `DiskJockey` is not (yet) registered an official Julia package, so it needs to be installed by using the link to the github repository.

(myproject) pkg> add https://github.com/iancze/DiskJockey.git

This process may take a few minutes as the relevant packages (including RADMC-3D) are downloaded from the web and installed. If you run into errors in this build process, please file an [issue](https://github.com/iancze/DiskJockey/issues) on the github repository so that we may try to fix this. If you already have RADMC-3D installed on your system, this process won't interfere with that installation, `DiskJockey` will use the version of RADMC-3D it downloaded during the build process. If you anticipate editing the source code, rather than installing the package with the `add` command, you can install using the `develop` command
This process may take a few minutes as the relevant packages (including RADMC-3D) are downloaded from the web and installed. If you run into errors in this build process, please file an [issue](https://github.com/iancze/DiskJockey/issues) on the github repository so that we may try to fix this. If you already have RADMC-3D installed on your system, this process won't interfere with that installation, `DiskJockey` will use the version of RADMC-3D it downloaded during the build process.

julia> ]
(@v1.4) pkg> develop https://github.com/iancze/DiskJockey.git
It is a good idea to set the environment variable `JULIA_PROJECT` to the location for this project

Included within the DiskJockey github repo are also several "driver" command line scripts that are used to perform the actual mass fitting. To utilize these files, you should add the scripts directory to your system PATH. To figure out where the package is installed
export JULIA_PROJECT='/home/ian/myproject'

You can add this line to your `.bashrc` or shell startup, or local directory initialization script. That way, whenever you start Julia, it will already activate the myproject environment. This makes running the scripts in the next section much easier. Advanced users with multiple active Julia projects may wish to explore alternate package activation patterns.

## DiskJockey Scripts

### Julia Scripts

Included within the DiskJockey github repo are several "driver" command line scripts in the `DiskJockey/scripts` directory that you can use to perform the actual mass fitting and analysis plotting. To utilize these files, you should add the `scripts` directory to your system PATH. To figure out where the package is installed

julia> using DiskJockey
julia> dirname(pathof(DiskJockey))
Expand All @@ -32,13 +49,19 @@ Your PATH will vary. The scripts are located inside of the `DiskJockey/scripts`

inside of your `.bashrc` file.

The scripts depend on other Julia packages for reading configuration files (in YAML) and plotting. You can either install these packages to your `myproject` environment just like you did DiskJockey, or you can use the convenience script

$ DJ_initialize_environment.jl


### Python Scripts

Some of the Python scripts also depend on imports from this directory, so you will also need to add

export PYTHONPATH="/home/ian/.julia/dev/DiskJockey/scripts:$PYTHONPATH"

inside of your `.bashrc` file.

For archival purposes, tagged release versions of this package are available [here](https://github.com/iancze/DiskJockey/releases).

## Python scripts

Expand All @@ -63,3 +86,14 @@ If you'd like to run the test suite to make sure everything checks out, start Ju
this may take about 10 minutes or so. If you catch any errors for your specific machine, please report them via an Issue on the github repository.

The code package is designed to interface with visibilities in the UVHDF5 format, described [here](https://github.com/Astrochem/UVHDF5). UVHDF5 also provides scripts to convert to and from UVFITS and CASA measurement sets.


## Development workflow

If you anticipate editing the source code, rather than installing the package with the `add` command, you can install using the `develop` command

julia> ]
(@v1.4) pkg> develop https://github.com/iancze/DiskJockey.git


For archival purposes, tagged release versions of this package are available [here](https://github.com/iancze/DiskJockey/releases).
16 changes: 7 additions & 9 deletions scripts/DJ_initialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
# files into this directory, write out the proper wavelength files, and make sure we can continue
# with the run properly.

using Pkg; Pkg.activate("DiskJockey")

using ArgParse

s = ArgParseSettings(description="Initialize a new project directory with the appropriate files. Can also be used to update RADMC-3D input files after making changes to config.yaml")
s = ArgParseSettings(description = "Initialize a new project directory with the appropriate files. Can also be used to update RADMC-3D input files after making changes to config.yaml")
@add_arg_table s begin
"--version"
help = "Print a the version number and exit."
Expand Down Expand Up @@ -109,10 +107,10 @@ if fit_every != 0
nchan = length(dvarr)
println("Dataset contains a total of $nchan channels.")

to_fit = Int[i for i=1:fit_every:nchan]
to_fit = Int[i for i = 1:fit_every:nchan]

# which channels of the dset to exclude
exclude = filter(x->(!in(x, to_fit)), Int[i for i=1:nchan])
exclude = filter(x->(!in(x, to_fit)), Int[i for i = 1:nchan])

println("To fit only every $(fit_every)-nd/rd/th channel, place the following line in your `config.yaml file.`")
println("exclude : $exclude")
Expand Down Expand Up @@ -185,20 +183,20 @@ if config["gas"]
# and create an evenly spaced array of velocities
vels = linspace(vstart, vend, nvel) # [km/s]

lam0 = lam0s[species*transition]
lam0 = lam0s[species * transition]

# convert velocities to wavelengths
shift_lams = lam0 * (vels/c_kms + 1)
shift_lams = lam0 * (vels / c_kms + 1)
else
# read the wavelengths for all data channels
fid = h5open(config["data_file"], "r")
freqs = read(fid["freqs"]) # [Hz]
# Convert from Hz to wavelengths in μm
lams = cc ./freqs * 1e4 # [μm]
lams = cc ./ freqs * 1e4 # [μm]
close(fid)

# Doppler shift the dataset wavelength according to the velocity in the parameter file
beta = vel/c_kms # relativistic Doppler formula
beta = vel / c_kms # relativistic Doppler formula
shift_lams = lams .* sqrt((1. - beta) / (1. + beta)) # [microns]
end

Expand Down
19 changes: 19 additions & 0 deletions scripts/DJ_initialize_environment.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env julia

using Pkg

# add the packages to the current environment, defined by JULIA_PROJECT
Pkg.add(PackageSpec(url = "https://github.com/iancze/DiskJockey.git"))
Pkg.add("ArgParse")
Pkg.add("HDF5")
Pkg.add("PyPlot")
Pkg.add("LaTeXStrings")
Pkg.add("YAML")
Pkg.add("Printf")
Pkg.add("PyCall")
Pkg.add("Images")
Pkg.add("NaNMath")
Pkg.add("NPZ")
Pkg.add("Statistics")

Pkg.instantiate()
5 changes: 2 additions & 3 deletions scripts/DJ_plot_baselines.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env julia
using Pkg; Pkg.activate("DiskJockey")

# Read the data file and plot the location of all of the baselines, in k\lambda
using ArgParse
Expand Down Expand Up @@ -27,7 +26,7 @@ using LaTeXStrings

species = config["species"]
transition = config["transition"]
lam0 = lam0s[species*transition]
lam0 = lam0s[species * transition]
model = config["model"]

pars = convert_dict(config["parameters"], config["model"])
Expand All @@ -36,7 +35,7 @@ pars = convert_dict(config["parameters"], config["model"])
dvarr = DataVis(config["data_file"])
dv = dvarr[1]

fig,ax = plt[:subplots](figsize=(6,6))
fig, ax = plt[:subplots](figsize = (6, 6))
ax[:plot](dv.uu, dv.vv, ".")
ax[:set_xlabel](L"UU [k$\lambda$]")
ax[:set_ylabel](L"VV [k$\lambda$]")
Expand Down
64 changes: 31 additions & 33 deletions scripts/DJ_plot_chmaps.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/usr/bin/env julia
using Pkg; Pkg.activate("DiskJockey")


# Given some model parameters, plot the channel maps and
# integrated spectrum
Expand Down Expand Up @@ -44,7 +42,7 @@ using LaTeXStrings

species = config["species"]
transition = config["transition"]
lam0 = lam0s[species*transition]
lam0 = lam0s[species * transition]
model = config["model"]

# Read the inclination, position angle, and (grid) radius of the disk. Plot ellipses.
Expand All @@ -57,7 +55,7 @@ vel_sys = pars["vel"]

# from matplotlib.patches import Ellipse
# ax[:add_artist](PyPlot.matplotlib[:patches][:Ellipse](xy=xy, width=BMIN, height=BMAJ, angle=BPA, facecolor="0.8", linewidth=0.2))
width = r_c/dpc # [arcsec]
width = r_c / dpc # [arcsec]
height = width * cosd(incl) # [arcsec]

# cmap = plt[:get_cmap]("viridis")
Expand All @@ -66,7 +64,7 @@ height = width * cosd(incl) # [arcsec]
cmap = plt.get_cmap("Blues")

# First contour is at 3 sigma, and then contours go up (or down) in multiples of spacing
function get_levels(rms::Float64, vmax::Float64, spacing=3)
function get_levels(rms::Float64, vmax::Float64, spacing = 3)
levels = Float64[]

val = 3 * rms
Expand All @@ -81,7 +79,7 @@ end
# Plot the channel maps using sky convention
"""Plot the channel maps using the sky convention. If log is true, plot intensity using
a log scale."""
function plot_chmaps(img::image.SkyImage; log=false, contours=true, fname="channel_maps_sky.png")
function plot_chmaps(img::image.SkyImage; log = false, contours = true, fname = "channel_maps_sky.png")

if log
ldata = log10.(img.data .+ 1e-99)
Expand All @@ -104,17 +102,17 @@ function plot_chmaps(img::image.SkyImage; log=false, contours=true, fname="chann

# Figure out how many plots we'll have.
ncols = 8
nrows = ceil(Int, nlam/ncols)
nrows = ceil(Int, nlam / ncols)

xx = 1.5 * 9
dx = 1.5
yy = (nrows + 1) * 1.5
dy = 1.5

fig, ax = plt.subplots(nrows=nrows, ncols=ncols, figsize=(xx, yy))
fig, ax = plt.subplots(nrows = nrows, ncols = ncols, figsize = (xx, yy))

for row=1:nrows
for col=1:ncols
for row = 1:nrows
for col = 1:ncols
iframe = col + (row - 1) * ncols

if col != 1 || row != nrows
Expand All @@ -127,65 +125,65 @@ function plot_chmaps(img::image.SkyImage; log=false, contours=true, fname="chann

if iframe > nlam
# Plot a blank square if we run out of channels
ax[row, col].imshow(zeros((im_ny, im_nx)), cmap=cmap, vmin=0, vmax=20, extent=ext, origin="lower")
ax[row, col].imshow(zeros((im_ny, im_nx)), cmap = cmap, vmin = 0, vmax = 20, extent = ext, origin = "lower")

else
#Flip the frame for Sky convention
# Flip the frame for Sky convention
# frame = flipdim(img.data[:,:,iframe], 2)
frame = reverse(img.data[:,:,iframe], dims=2)
frame = reverse(img.data[:,:,iframe], dims = 2)

if log
frame += 1e-15 #Add a tiny bit so that we don't have log10(0)
frame += 1e-15 # Add a tiny bit so that we don't have log10(0)
lframe = log10.(frame)
im = ax[row, col].imshow(lframe, extent=ext, interpolation="none", origin="lower", cmap=cmap, norm=norm)
im = ax[row, col].imshow(lframe, extent = ext, interpolation = "none", origin = "lower", cmap = cmap, norm = norm)

else
im = ax[row, col].imshow(frame, extent=ext, interpolation="none", origin="lower", cmap=cmap, norm=norm)
im = ax[row, col].imshow(frame, extent = ext, interpolation = "none", origin = "lower", cmap = cmap, norm = norm)

if contours
ax[row, col].contour(frame, origin="lower", colors="k", levels=levels, extent=ext, linestyles="solid", linewidths=0.2)
ax[row, col].contour(frame, origin = "lower", colors = "k", levels = levels, extent = ext, linestyles = "solid", linewidths = 0.2)
end

end

ax[row, col].add_artist(PyPlot.matplotlib.patches.Ellipse((0,0), width, height, PA, linewidth=0.15, facecolor="none", edgecolor="w"))
ax[row, col].add_artist(PyPlot.matplotlib.patches.Ellipse((0, 0), width, height, PA, linewidth = 0.15, facecolor = "none", edgecolor = "w"))

if iframe==1
if iframe == 1
# Plot the colorbar
cax = fig.add_axes([(xx - 0.35 * dx)/xx, (yy - 1.5 * dy)/yy, (0.1 * dx)/xx, dy/yy])
cbar = fig.colorbar(mappable=im, cax=cax)
cax = fig.add_axes([(xx - 0.35 * dx) / xx, (yy - 1.5 * dy) / yy, (0.1 * dx) / xx, dy / yy])
cbar = fig.colorbar(mappable = im, cax = cax)

cbar.ax.tick_params(labelsize=6)
fig.text(0.99, (yy - 1.7 * dy)/yy, "Jy/beam", size=8, ha="right")
cbar.ax.tick_params(labelsize = 6)
fig.text(0.99, (yy - 1.7 * dy) / yy, "Jy/beam", size = 8, ha = "right")
end

ax[row, col].annotate(@sprintf("%.1f", vels[iframe]), (0.1, 0.8), xycoords="axes fraction", size=8)
ax[row, col].annotate(@sprintf("%.1f", vels_obs[iframe]), (0.1, 0.9), xycoords="axes fraction", size=8)
ax[row, col].annotate(@sprintf("%.1f", vels[iframe]), (0.1, 0.8), xycoords = "axes fraction", size = 8)
ax[row, col].annotate(@sprintf("%.1f", vels_obs[iframe]), (0.1, 0.9), xycoords = "axes fraction", size = 8)
end

end
end

fig.subplots_adjust(hspace=0.00, wspace=0.00, top=(yy - 0.5 * dy)/yy, bottom=(0.5 * dy)/yy, left=(0.5 * dx)/xx, right=(xx - 0.5 * dy)/xx)
fig.subplots_adjust(hspace = 0.00, wspace = 0.00, top = (yy - 0.5 * dy) / yy, bottom = (0.5 * dy) / yy, left = (0.5 * dx) / xx, right = (xx - 0.5 * dy) / xx)

plt.savefig(fname, dpi=600)
plt.savefig(fname, dpi = 600)

end

# Plot the spatially-integrated spectrum
function plot_spectrum(img::image.SkyImage; fname="spectrum.png")
function plot_spectrum(img::image.SkyImage; fname = "spectrum.png")

fig = plt.figure()
ax = fig.add_subplot(111)

spec = imToSpec(img)

ax.plot(vels, spec[:,2], ls="steps-mid")
ax.plot(vels, spec[:,2], ls = "steps-mid")

ax.set_ylabel(L"$f_\nu$ [Jy]")
ax.set_xlabel(L"$v$ [km/s]")

fig.subplots_adjust(left=0.15, bottom=0.15, right=0.85)
fig.subplots_adjust(left = 0.15, bottom = 0.15, right = 0.85)

plt.savefig(fname)

Expand All @@ -203,7 +201,7 @@ skim = imToSky(im, pars.dpc)
# Do the velocity conversion here for plot labels in the disk-frame
global nlam = length(skim.lams)
# convert wavelengths to velocities
global vels = c_kms * (skim.lams .- lam0)/lam0
global vels = c_kms * (skim.lams .- lam0) / lam0

# calculate velocities in the observed frame (whatever the dataset is)
global vels_obs = vels .+ vel_sys
Expand All @@ -217,11 +215,11 @@ if vels[2] < vels[1]
end

if parsed_args["linear"]
plot_chmaps(skim, fname="chmaps_linear.png", contours=false)
plot_chmaps(skim, fname = "chmaps_linear.png", contours = false)
end

if parsed_args["log"]
plot_chmaps(skim, fname="chmaps_log.png", log=true, contours=false)
plot_chmaps(skim, fname = "chmaps_log.png", log = true, contours = false)
end

if parsed_args["spectrum"]
Expand Down
Loading

0 comments on commit fc10998

Please sign in to comment.