Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Octave language with its own language distinct from MATLAB #6936

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
11dfcf4
Add Octave to languages.yml
pr0m1th3as Jun 30, 2024
d1be7b0
Update heuristics.yml to distinguish Octave from MATLAB
pr0m1th3as Jun 30, 2024
e64f599
Add sample file for Octave language
pr0m1th3as Jul 9, 2024
0e7bf18
Add sample files for Octave language
pr0m1th3as Jul 9, 2024
163f347
Update test_heuristics.rb for Octave language
pr0m1th3as Jul 9, 2024
873a169
Update test_language.rb for Octave language
pr0m1th3as Jul 9, 2024
8942b54
Update heuristics.yml for Octave language
pr0m1th3as Jul 9, 2024
c8b5dd2
Update heuristics.yml for Octave language
pr0m1th3as Jul 9, 2024
6355389
Update heuristics.yml for Octave language
pr0m1th3as Jul 9, 2024
5f3c3fb
Add language: Octave, as distinct from Matlab
apjanke Jul 9, 2024
9951d79
Merge pull request #1 from apjanke/master
pr0m1th3as Jul 10, 2024
45e3c62
Update patterns for Octave language in heuristics.yml
pr0m1th3as Jul 11, 2024
25b9b87
Delete samples/Octave/BetaDistribution.m
pr0m1th3as Jul 12, 2024
0aa5fed
Delete samples/Octave/accumarray.m
pr0m1th3as Jul 12, 2024
58eb7de
Delete samples/Octave/var.m
pr0m1th3as Jul 12, 2024
29ce7d4
Update test_heuristics.rb
pr0m1th3as Jul 12, 2024
b15123b
add the cached license file for Octave
pr0m1th3as Jul 12, 2024
be1b7e1
update Octave language_id as generated by script/update-ids script
pr0m1th3as Jul 12, 2024
9dd005d
add Octave to grammars.yml
pr0m1th3as Jul 12, 2024
303b6b9
change Octave's ace_mode to text
pr0m1th3as Jul 12, 2024
709c48a
add codemirror code and mime_type for MATLAB and Octave in the langua…
pr0m1th3as Jul 23, 2024
485c978
Update lib/linguist/languages.yml
lildude Aug 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -1401,3 +1401,6 @@
[submodule "vendor/grammars/zephir-sublime"]
path = vendor/grammars/zephir-sublime
url = https://github.com/phalcon/zephir-sublime
[submodule "vendor/grammars/Octave-language-grammar"]
path = vendor/grammars/Octave-language-grammar
url = https://github.com/gnu-octave/Octave-language-grammar
2 changes: 2 additions & 0 deletions grammars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ vendor/grammars/ObjectScript.tmBundle:
- source.objectscript_class
- source.objectscript_csp
- source.objectscript_macros
vendor/grammars/Octave-language-grammar:
- source.octave
vendor/grammars/PHP-Twig.tmbundle:
- text.html.twig
vendor/grammars/PogoScript.tmbundle:
Expand Down
8 changes: 8 additions & 0 deletions lib/linguist/heuristics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,14 @@ disambiguations:
rules:
- language: Objective-C
named_pattern: objectivec
- language: Octave
pattern:
- '^\s*##'
- '^\s*endfunction'
- '^\s*endclassdef'
- '^\s*endif'
- '^\s*endfor'
- '^\s*endwhile'
- language: Mercury
pattern: ':- module'
- language: MUF
Expand Down
20 changes: 12 additions & 8 deletions lib/linguist/languages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -943,11 +943,11 @@ Caddyfile:
type: data
color: "#22b638"
aliases:
- Caddy
- Caddy
extensions:
- ".caddyfile"
- ".caddyfile"
filenames:
- Caddyfile
- Caddyfile
ace_mode: text
tm_scope: source.Caddyfile
language_id: 615465151
Expand Down Expand Up @@ -4020,15 +4020,11 @@ M4Sugar:
MATLAB:
type: programming
color: "#e16737"
aliases:
- octave
extensions:
- ".matlab"
- ".m"
tm_scope: source.matlab
ace_mode: matlab
codemirror_mode: octave
codemirror_mime_type: text/x-octave
lildude marked this conversation as resolved.
Show resolved Hide resolved
language_id: 225
MAXScript:
type: programming
Expand Down Expand Up @@ -4884,6 +4880,14 @@ Objective-J:
tm_scope: source.js.objj
ace_mode: text
language_id: 259
Octave:
type: programming
color: "#2299c4"
extensions:
- ".m"
tm_scope: source.octave
ace_mode: text
language_id: 854759768
lildude marked this conversation as resolved.
Show resolved Hide resolved
Odin:
type: programming
color: "#60AFFE"
Expand Down Expand Up @@ -5359,7 +5363,7 @@ Pkl:
extensions:
- ".pkl"
interpreters:
- pkl
- pkl
tm_scope: source.pkl
ace_mode: text
language_id: 288822799
Expand Down
245 changes: 245 additions & 0 deletions samples/Octave/ClusterCriterion.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
## Copyright (C) 2021 Stefano Guidoni <[email protected]>
## Copyright (C) 2024 Andreas Bertsatos <[email protected]>
##
## This file is part of the statistics package for GNU Octave.
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see <http://www.gnu.org/licenses/>.

classdef ClusterCriterion < handle
## -*- texinfo -*-
## @deftypefn {statistics} {@var{obj} =} ClusterCriterion (@var{x}, @var{clust}, @var{criterion})
##
## A clustering evaluation object as created by @code{evalclusters}.
##
## @code{ClusterCriterion} is a superclass for clustering evaluation objects
## as created by @code{evalclusters}.
##
## List of public properties:
## @table @code
## @item @qcode{ClusteringFunction}
## a valid clustering funtion name or function handle. It can be empty if
## the clustering solutions are passed as an input matric.
##
## @item @qcode{CriterionName}
## a valid criterion name to evaluate the clustering solutions.
##
## @item @qcode{CriterionValues}
## a vector of values as generated by the evaluation criterion for each
## clustering solution.
##
## @item @qcode{InspectedK}
## the list of proposed cluster numbers.
##
## @item @qcode{Missing}
## a logical vector of missing observations. When there are @code{NaN}
## values in the data matrix, the corresponding observation is excluded.
##
## @item @qcode{NumObservations}
## the number of non-missing observations in the data matrix.
##
## @item @qcode{OptimalK}
## the optimal number of clusters.
##
## @item @qcode{OptimalY}
## the clustering solution corresponding to @code{OptimalK}.
##
## @item @qcode{X}
## the data matrix.
##
## @end table
##
## List of public methods:
## @table @code
## @item @qcode{addK}
## add a list of numbers of clusters to evaluate.
##
## @item @qcode{compact}
## return a compact clustering evaluation object. Not implemented
##
## @item @qcode{plot}
## plot the clustering evaluation values against the corresponding number of
## clusters.
##
## @end table
##
## @seealso{evalclusters, CalinskiHarabaszEvaluation, DaviesBouldinEvaluation,
## GapEvaluation, SilhouetteEvaluation}
## @end deftypefn

properties (Access = public)
## public properties
endproperties

properties (GetAccess = public, SetAccess = protected)
ClusteringFunction = "";
CriterionName = "";
CriterionValues = [];
InspectedK = [];
Missing = [];
NumObservations = 0;
OptimalK = 0;
OptimalY = [];
X = [];
endproperties

properties (Access = protected)
N = 0; # number of observations
P = 0; # number of variables
ClusteringSolutions = []; #
OptimalIndex = 0; # index of the optimal K
endproperties

methods (Access = public)

## constructor
function this = ClusterCriterion (x, clust, KList)
## parsing input data
if ((! ismatrix (x)) || (! isnumeric (x)))
error ("ClusterCriterion: 'x' must be a numeric matrix");
endif
this.X = x;
this.N = rows (this.X);
this.P = columns (this.X);
## look for missing values
for iter = 1 : this.N
if (any (find (x(iter, :) == NaN)))
this.Missing(iter) = true;
else
this.Missing(iter) = false;
endif
endfor
## number of usable observations
this.NumObservations = sum (this.Missing == false);

## parsing the clustering algorithm
if (ischar (clust))
if (any (strcmpi (clust, {"kmeans", "linkage", "gmdistribution"})))
this.ClusteringFunction = lower (clust);
else
error ("ClusterCriterion: unknown clustering algorithm '%s'", clust);
endif
elseif (isa (clust, "function_handle"))
this.ClusteringFunction = clust;
elseif (ismatrix (clust))
if (isnumeric (clust) && (length (size (clust)) == 2) && ...
(rows (clust) == this.N))
this.ClusteringFunction = "";
this.ClusteringSolutions = clust(find (this.Missing == false), :);
else
error ("ClusterCriterion: invalid matrix of clustering solutions");
endif
else
error ("ClusterCriterion: invalid argument");
endif

## parsing the list of cluster sizes to inspect
this.InspectedK = parseKList (this, KList);
endfunction

## -*- texinfo -*-
## @deftypefn {ClusterCriterion} {@var{obj} =} addK (@var{obj}, @var{K})
##
## Add a new cluster array to inspect the ClusterCriterion object.
##
## @end deftypefn
function this = addK (this, k)

## if there is not a clustering function, then we are using a predefined
## set of clustering solutions, hence we cannot redefine the number of
## solutions
if (isempty (this.ClusteringFunction))
warning (["ClusterCriterion.addK: cannot redefine the list of cluster"...
"numbers to evaluate when there is not a clustering function"]);
return;
endif

## otherwise go on
newList = this.parseKList ([this.InspectedK k]);

## check if the list has changed
if (length (newList) == length (this.InspectedK))
warning ("ClusterCriterion.addK: the list has not changed");
else
## update ClusteringSolutions and CriterionValues
ClusteringSolutions_tmp = zeros (this.NumObservations, ...
length (newList));
CriterionValues_tmp = zeros (length (newList), 1);
for iter = 1 : length (this.InspectedK)
idx = find (newList == this.InspectedK(iter));

if (! isempty (idx))
ClusteringSolutions_tmp(:, idx) = this.ClusteringSolutions(:, iter);
CriterionValues_tmp(idx) = this.CriterionValues(iter);
endif
endfor
this.ClusteringSolutions = ClusteringSolutions_tmp;
this.CriterionValues = CriterionValues_tmp;

## reset the old results
this.OptimalK = 0;
this.OptimalY = [];
this.OptimalIndex = 0;

## update the list of cluster numbers to evaluate
this.InspectedK = newList;
endif
endfunction

## -*- texinfo -*-
## @deftypefn {ClusterCriterion} {} plot (@var{obj})
## @deftypefnx {ClusterCriterion} {@var{h} =} plot (@var{obj})
##
## Plot the evaluation results.
##
## Plot the CriterionValues against InspectedK from the ClusterCriterion,
## @var{obj}, to the current plot. It can also return a handle to the
## current plot.
##
## @end deftypefn
function h = plot (this)
yLabel = sprintf ("%s value", this.CriterionName);
h = gca ();
hold on;
plot (this.InspectedK, this.CriterionValues, "bo-");
plot (this.OptimalK, this.CriterionValues(this.OptimalIndex), "b*");
xlabel ("number of clusters");
ylabel (yLabel);
hold off;
endfunction

## -*- texinfo -*-
## @deftypefn {ClusterCriterion} {@var{obj} =} compact (@var{obj})
##
## Return a compact ClusterCriterion object (not implemented yet).
##
## @end deftypefn
function this = compact (this)
warning ("ClusterCriterion.compact: this method is not yet implemented.");
endfunction

endmethods

methods (Access = private)
## check if a list of cluster sizes is correct
function retList = parseKList (this, KList)
if (isnumeric (KList) && isvector (KList) && all (find (KList > 0)) && ...
all (floor (KList) == KList))
retList = unique (KList);
else
error (["ClusterCriterion: the list of cluster sizes must be an " ...
"array of positive integer numbers"]);
endif
endfunction
endmethods
endclassdef
Loading