How do I remove this UndefVar error in my code? - image

I'm using this notebook provided by the Computational Thinking course and am running into an error when I execute this code.
import Pkg
Pkg.add(["Images", "ImageIO", "ImageMagick"])
using Images
philip_file = download("https://i.imgur.com/VGPeJ6s.jpg")
philip = let
original = Images.load(philip_file)
decimate(original, 8)
end
This is the error I see:
[ Info: Precompiling ImageMagick [6218d12a-5da1-5696-b52f-db25d2ecc6d1]
ERROR: UndefVarError: decimate not defined
Stacktrace:
[1] top-level scope at REPL[6]:3
The notebook has not defined decimate anywhere, and looking online, I can't seem to find the function using a Google search. There is no instruction in the exercise that I might have to define the function myself. My understanding is that this function is built into one of the modules. My import of the Images module worked just fine. Can anyone help me understand what I might be doing wrong?
Note: There is a Discord community for the course but the link isn't working, so I have exhausted all those avenues.

The decimate function seems to be defined in that same notebook, by:
decimate(image, ratio=5) = image[1:ratio:end, 1:ratio:end]

Related

Parallel processing in Julia throws errors

My understanding is that parallelization is included by default in a base Julia installation.
However, when I try to use it, I am getting errors that the functions and macros are not defined. For example:
nprocs()
Throws an error:
ERROR: UndefVarError: nprocs not defined
Stacktrace:
[1] top-level scope at none:0
Nowhere in any Julia documentation can I find mention of any packages that need to be included in order to use these functions. Am I missing something here?
I am using Julia version 1.0.5 inside the JuliaPro/Atom IDE
I figured it out. I'll leave this up for anyone else who is having this problem.
The solution is to import the Distributed package using:
using Distributed
Why this is not included in the documentation I do not know.
Once you know that nproc needs to be used, there exist a couple of options to find where it is defined.
A search through the documentation can help: https://docs.julialang.org/en/v1/search/?q=nprocs
Without leaving the Julia REPL, and even before nprocs gets imported in your session, you can use apropos in order to find more about it and determine that it is needed to import the Distributed package:
julia> apropos("nprocs")
Distributed.nprocs
Distributed.addprocs
Distributed.nworkers
An other way of using apropos is via the help REPL mode:
julia> # type `?` when the cursor is right after the prompt to enter help REPL mode
# note the use of double quotes to trigger "apropos" instead of a regular help query
help?> "nprocs"
Distributed.nprocs
Distributed.addprocs
Distributed.nworkers
Previous options work well in the case of nprocs because it is part of the standard library. JuliaHub is another option which allows looking for things more broadly, in the entire Julia ecosystem. As an example, looking for nprocs in JuliaHub's "Doc Search" tool also returns relevant results: https://juliahub.com/ui/Documentation?q=nprocs

Sourcing data into rstudio [duplicate]

This is meant to be a FAQ question, so please be as complete as possible. The answer is a community answer, so feel free to edit if you think something is missing.
This question was discussed and approved on meta.
I am using R and tried some.function but I got following error message:
Error: could not find function "some.function"
This question comes up very regularly. When you get this type of error in R, how can you solve it?
There are a few things you should check :
Did you write the name of your function correctly? Names are case sensitive.
Did you install the package that contains the function? install.packages("thePackage") (this only needs to be done once)
Did you attach that package to the workspace ?
require(thePackage) (and check its return value) or library(thePackage) (this should be done every time you start a new R session)
Are you using an older R version where this function didn't exist yet?
Are you using a different version of the specific package? This could be in either direction: functions are added and removed over time, and it's possible the code you're referencing is expecting a newer or older version of the package than what you have installed.
If you're not sure in which package that function is situated, you can do a few things.
If you're sure you installed and attached/loaded the right package, type help.search("some.function") or ??some.function to get an information box that can tell you in which package it is contained.
find and getAnywhere can also be used to locate functions.
If you have no clue about the package, you can use findFn in the sos package as explained in this answer.
RSiteSearch("some.function") or searching with rdocumentation or rseek are alternative ways to find the function.
Sometimes you need to use an older version of R, but run code created for a newer version. Newly added functions (eg hasName in R 3.4.0) won't be found then. If you use an older R version and want to use a newer function, you can use the package backports to make such functions available. You also find a list of functions that need to be backported on the git repo of backports. Keep in mind that R versions older than R3.0.0 are incompatible with packages built for R3.0.0 and later versions.
Another problem, in the presence of a NAMESPACE, is that you are trying to run an unexported function from package foo.
For example (contrived, I know, but):
> mod <- prcomp(USArrests, scale = TRUE)
> plot.prcomp(mod)
Error: could not find function "plot.prcomp"
Firstly, you shouldn't be calling S3 methods directly, but lets assume plot.prcomp was actually some useful internal function in package foo. To call such function if you know what you are doing requires the use of :::. You also need to know the namespace in which the function is found. Using getAnywhere() we find that the function is in package stats:
> getAnywhere(plot.prcomp)
A single object matching ‘plot.prcomp’ was found
It was found in the following places
registered S3 method for plot from namespace stats
namespace:stats
with value
function (x, main = deparse(substitute(x)), ...)
screeplot.default(x, main = main, ...)
<environment: namespace:stats>
So we can now call it directly using:
> stats:::plot.prcomp(mod)
I've used plot.prcomp just as an example to illustrate the purpose. In normal use you shouldn't be calling S3 methods like this. But as I said, if the function you want to call exists (it might be a hidden utility function for example), but is in a namespace, R will report that it can't find the function unless you tell it which namespace to look in.
Compare this to the following:
stats::plot.prcomp
The above fails because while stats uses plot.prcomp, it is not exported from stats as the error rightly tells us:
Error: 'plot.prcomp' is not an exported object from 'namespace:stats'
This is documented as follows:
pkg::name returns the value of the exported variable name in namespace pkg, whereas pkg:::name returns the value of the internal variable name.
I can usually resolve this problem when a computer is under my control, but it's more of a nuisance when working with a grid. When a grid is not homogenous, not all libraries may be installed, and my experience has often been that a package wasn't installed because a dependency wasn't installed. To address this, I check the following:
Is Fortran installed? (Look for 'gfortran'.) This affects several major packages in R.
Is Java installed? Are the Java class paths correct?
Check that the package was installed by the admin and available for use by the appropriate user. Sometimes users will install packages in the wrong places or run without appropriate access to the right libraries. .libPaths() is a good check.
Check ldd results for R, to be sure about shared libraries
It's good to periodically run a script that just loads every package needed and does some little test. This catches the package issue as early as possible in the workflow. This is akin to build testing or unit testing, except it's more like a smoke test to make sure that the very basic stuff works.
If packages can be stored in a network-accessible location, are they? If they cannot, is there a way to ensure consistent versions across the machines? (This may seem OT, but correct package installation includes availability of the right version.)
Is the package available for the given OS? Unfortunately, not all packages are available across platforms. This goes back to step 5. If possible, try to find a way to handle a different OS by switching to an appropriate flavor of a package or switch off the dependency in certain cases.
Having encountered this quite a bit, some of these steps become fairly routine. Although #7 might seem like a good starting point, these are listed in approximate order of the frequency that I use them.
If this occurs while you check your package (R CMD check), take a look at your NAMESPACE.
You can solve this by adding the following statement to the NAMESPACE:
exportPattern("^[^\\\\.]")
This exports everything that doesn't start with a dot ("."). This allows you to have your hidden functions, starting with a dot:
.myHiddenFunction <- function(x) cat("my hidden function")
I had the error
Error: could not find function some.function
happen when doing R CMD check of a package I was making with RStudio. I found adding
exportPattern(".")
to the NAMESPACE file did the trick. As a sidenote, I had initially configured RStudio to use ROxygen to make the documentation -- and selected the configuration where ROxygen would write my NAMESPACE file for me, which kept erasing my edits. So, in my instance I unchecked NAMESPACE from the Roxygen configuration and added exportPattern(".") to NAMESPACE to solve this error.
This error can occur even if the name of the function is valid if some mandatory arguments are missing (i.e you did not provide enough arguments).
I got this in an Rcpp context, where I wrote a C++ function with optionnal arguments, and did not provided those arguments in R. It appeared that optionnal arguments from the C++ were seen as mandatory by R. As a result, R could not find a matching function for the correct name but an incorrect number of arguments.
Rcpp Function : SEXP RcppFunction(arg1, arg2=0) {}
R Calls :
RcppFunction(0) raises the error
RcppFunction(0, 0) does not
Rdocumentation.org has a very handy search function that - among other things - lets you find functions - from all the packages on CRAN, as well as from packages from Bioconductor and GitHub.
If you are using parallelMap you'll need to export custom functions to the slave jobs, otherwise you get an error "could not find function ".
If you set a non-missing level on parallelStart the same argument should be passed to parallelExport, else you get the same error. So this should be strictly followed:
parallelStart(mode = "<your mode here>", N, level = "<task.level>")
parallelExport("<myfun>", level = "<task.level>")
You may be able to fix this error by name spacing :: the function call
comparison.cloud(colors = c("red", "green"), max.words = 100)
to
wordcloud::comparison.cloud(colors = c("red", "green"), max.words = 100)
I got the same, error, I was running version .99xxx, I checked for updates from help menu and updated My RStudio to 1.0x, then the error did not come
So simple solution, just update your R Studio

confusion in different ways to open a session and executing the graph in tensorflow

I am trying to learn deep learning with tensorflow, so excuse my stupid questions. I have been reading different tutorials as 'https://github.com/Hvass-Labs/TensorFlow-Tutorials' and 'https://github.com/u04617/deeplearning/blob/master/mnist_experiments.ipynb'
unfortunately I have been confused by some difference in their writing. More specifically I have a question regarding opening a session:
1) what is the difference between
session = tf.Session()
session.run(tf.global_variables_initializer())
session.run(optimizer, feed_dict=feed_dict_train)
and
sess = tf.InteractiveSession()
tf.initialize_all_variables().run()
optimizer.run({x: batch_xs, y_: batch_ys, keep_prob: 0.5})
Indeed, I understand the basic idea behind each of these lines (open a session to execute the graph, initialize the variable for
the graph and finally execute the graph given in a dictionary the needed input), but I don't understand the difference from the two above code, especially the last line.
Both are working.
In Jupyter Notebook files: use InteractiveSession
Read here to understand the difference between an InactiveSession and a Session.
But do not even try eval(). Please do yourself a favor and use the only correct and clean way:
init_op = tf.global_variables_initializer()
tf.get_default_graph().finalize()
with tf.Session() as session:
session.run(init_op)
session.run(optimizer, feed_dict=feed_dict_train)
The way of operation.run(), tensor.eval() is spread somewhere in the internet. But there are tons of caveats:
Here are two of them:
Tensorflow subtract strange result
Official ZeroOut gradient example error: AttributeError: 'list' object has no attribute 'eval'
I hope at some point they will deprecate `eval() at some point.

Angular cli error when processing trigonometry function (cosine) on an SCSS transform - Undefined operation: "cos(_) times _px"

I am trying to implement an SCSS component into my AngularCLI project (from Codepen: https://codepen.io/lbebber/pen/LELBEo).
When I run the following SCSS transform, transform:translate3d(cos(0.1)*115px,sin(0.1)*115px,0);
I get the following build error:
Module build failed:
transform:translate3d(cos(0.1)*115px, sin(0.1)*115px, 0);
^
Undefined operation: "cos(0.1) times 115px".
I read up on SASS/SCSS numeric conversions, but found this is not the issue - as I tried replicating this in the Codepen, and his code is working just fine, no flawed conversion logic.
I can only suspect this is an issue with my AngularCLI configuration, something isn't registering correctly, and the cosine is being interpreted as a string instead of its numeric calculation. When hardcoding the numbers for cosine/sine, I get a valid build and see the UI functioning as expected.
Do I need to configure the AngularCLI project in a way that lets the SCSS process the numeric values for cosine/sine before stepping into the equation as a string? If so, how?
Much appreciation for anyone that has the Angular-fu to figure out how to get this to work.
As we have found out it’s because your CodePen example uses the Compass Math Helpers functions which you don’t have/use.
You could instead for example import mathsass. It should cover the same amout of functionality.

Reading in Packages in Mathematica

I've created a package in Mathematica but I can't seem to get Mathematica to read it in. The Package is of the form:
BeginPackage["name`"]
(function[]::usage)
Begin["`Private'"]
(functions)
End[]
EndPackage[]
I saved this file as a .m. The problem is that after I quit the kernel and then try to read in the package using Needs["name`"], I always get a no::cont error. I've tried saving the file in $BaseUserDirectory and $BaseDirectory, but it still give me a no::cont error:
Needs["name"]
Needs::nocont: "Context \!\(\"name\") was not created when Needs was evaluated."
I've also tried using the built-in File->Install function in Mathematica but it still gives me the same error. Is there something that I need to change with regards to the context?
Any help is much appreciated.
Thank you,
jm
Write the definitions in "Initialization Cells" when you create the package notebook. Otherwise they will be ignored. This worked for me with Version 9 on OS X. In previous versions it was probably not necessary, but I don't remember any more... :-)
It's because the directory of your package is not in $Path. Needs only searches the packages in $Path, while Get can search subdirectories.

Resources